Fastjson the processing of date __java

Source: Internet
Author: User

Serialization of dates:

One way is by annotating

Java code

@JSONField (format= "Yyyy-mm-dd HH:mm:ss") public  
Date birthday;


The other is through Serializeconfig:

Java code

private static Serializeconfig mapping = new Serializeconfig ();  
private static String DateFormat;  
static {  
    DateFormat = "Yyyy-mm-dd HH:mm:ss";  
    Mapping.put (Date.class, New Simpledateformatserializer (DateFormat));  
}


Use single quotes in JSON strings:

String Text = Json.tojsonstring (object, serializerfeature.usesinglequotes);

field displays different key:

public class User {  
    @JSONField (name= "ID") public  
    int getId () {...}}  
    
User user = ...;  
json.tojsonstring (user); {"ID": 001}


Custom Serialization code example:

public class Jsonutil {  
    private static serializeconfig mapping = new Serializeconfig ();  
    private static String DateFormat;  
    static {  
        DateFormat = "Yyyy-mm-dd HH:mm:ss";  
    }  
   
    /** 
     * Default processing Time 
     * *  
     @param jsontext 
     * @return 
     /public  
    static String Tojson (Object jsontext) { Return  
        json.tojsonstring (Jsontext,  
                serializerfeature.writedateusedateformat);  
    }  
   
    /** 
     * Custom Time format 
     * *  
     @param jsontext 
     * @return 
     /public  
    static string Tojson (string DateFormat, String jsontext) {  
        mapping.put (Date.class, New Simpledateformatserializer (DateFormat));  
        Return json.tojsonstring (jsontext, mapping);  
    }  


Custom Deserialization Example:

First customize a date resolution class:

Java code

public class Mydateformatdeserializer extends Dateformatdeserializer {private String myformat;  
            Public Mydateformatdeserializer (String MyFormat) {super ();  
        This.myformat = MyFormat; @Override protected <Date> Date cast (defaultjsonparser parser, Type clazz, Object Fieldnam  
            E, Object val) {if (MyFormat = null) {return null;  
                } if (val instanceof string) {string strval = (string) val;  
                if (strval.length () = = 0) {return null;  
                try {return (Date) new SimpleDateFormat (MyFormat). Parse ((String) val);  
                catch (ParseException e) {throw new Jsonexception ("Parse error");  
        } throw new Jsonexception ("Parse error"); }  
    }

Turn from: http://my.oschina.net/u/1444624/blog/375740

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.