SoFunction
Updated on 2025-04-11

A brief analysis of the simple use of fastjson2 time serialization and deserialization

After upgrading fastjson to fastjson2 in the project, we encountered some problems that were not fully compatible with fastjson. This article will explore the simple use of time serialization and deserialization of fastjson2.

Time serialization

existfastjson2In order to serialize the time attribute in a Java object into a JSON string, you need to use@JSONFieldAnnotation specifies the time format, or pass it into the format through method parameters during serialization.

// Use annotations to specify the format@JSONField(format = "yyyy-MM-dd")
private Date dateField;
 
// Or pass the format through method parameters during serializationString jsonString = (bean, "yyyy-MM-dd");

Time deserialization

Similar to serialization, when deserializing a date string into a time attribute in a Java object, it can also be passed@JSONFieldAnnotation or method parameters specify the time format.

// Use annotations to specify the format@JSONField(format = "yyyy-MM-dd")
private Date dateField;
 
// Or pass the format through method parameters during deserializationBean bean = (jsonString, , "yyyy-MM-dd");

Fastjson2's default time format

When serializing time, the default format isyyyy-MM-dd HH:mm:, which is different from the default 13-bit millisecond value timestamp format for Jackson and fastjson v1.

fastjson2Supports configuration of global default time format.

  • Serialization default time format can be passed(format)set up.
  • Deserialization of the default time format can be passed(format)set up.

informatThe value can be referred to the format description below.

Fastjson2's hidden function

Compatible by defaultJacksonGsonas well asfastjsonThe new version supports closure through configurationJacksonandGsonThe annotation is compatible, fastjson does not support closing (and maybe it will support it later, after allJacksonandGsonIt is also the later version that has been added to the support configuration that is turned off and compatible).

Flippedfastjson2The source code of Jackson is basically supported by more than 30 annotations, but this is not explained in the official documentation.

Format priority

In fastjson2, the priority of the time format is:> Method Parameters format > Global Configuration

format description

  • iso8601: Follow the ISO 8601 standard format, such as2024-12-27T11:22:48.404+08:00
  • millis: 13-bit timestamp.
  • unixtime: 10-bit timestamp.
  • Other formats: Follow JavaSimpleDateFormatStandard, such asyyyy-MM-dd HH:mm:ss

Time formatting component

The following lists commonly used components and their meanings in Java date and time formatting:

Note that this is case-sensitive, for exampleyandYThere are different meanings in date formatting.yIndicates the current date's year of attribution, andYIndicates the year at which the current week belongs, which may cause a date to display error at the end of the year.

Components describe Format Example
G Year indicator text AD (AD)
y years years 1996;96
Y Anniversary (based on weekly calculation) years 2009;09
M Mid-year month (context sensitive) month July;Jul;07
L Mid-year month (independent form) month July;Jul;07
w Weeks in the year number 27
W Weeks in the month number 2
D Days in the year number 189
d Days in the month number 10
F Number of Sundays in the month number 2
E The number of days of the week text Tuesday;Tue
u Number of days of the week (1=Monday,..., 7=Sunday) number 1
a Morning/afternoon mark text PM
H Hour of the day (0-23) number 0
k Hour of the day (1-24) number 24
K Hours in the afternoon (0-11) number 0
h Hours in the afternoon (1-12) number 12
m Minutes of hours number 30
s Seconds in minutes number 55
S millisecond number 978
z Time zone General time zone Pacific Standard Time;PST;GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08;-0800;-08:00

Time format component description

Code Example

Below is a sample code for time serialization and deserialization using fastjson2.

The fastjson version used in the code is 1.2.83, and the fastjson2 version is 2.0.53.

fastjson2 Maven dependencies:

<dependency>  
    <groupId>.fastjson2</groupId>  
    <artifactId>fastjson2</artifactId>  
    <version>2.0.53</version>  
</dependency>

fastjson Maven dependencies:

<dependency>  
    <groupId></groupId>  
    <artifactId>fastjson</artifactId>  
    <version>1.2.83</version>  
</dependency>

Test class

public class DemoTest {  
  
    public static void main(String[] args) {  
        //Serialization        testToJSONString();  
        // Fastjson serialization and 2 are compared        testFastJson1ToJSON();  
        //Resolve as a java object        testParseObject();  
    }  
  
    private static void testToJSONString() {  
        Date now = new Date();  
        Bean bean = new Bean();  
        (now);  
        bean.setIso8601Date(now);  
        (now);  
        (now);  
        (now);  
        ("Fastjson2 global configuration format pre-serialization result");  
        ((bean));  
  
        String format = "millis";  
        (format);  
        ("Fastjson2 global configuration format after serialization result");  
        ((bean));  
  
        ("Fastjson2 method passes in format serialization result");  
        ((bean,"yyyyMMdd"));  
    }  
  
    private static void testFastJson1ToJSON() {  
        Date now = new Date();  
        Bean1 bean1 = new Bean1();  
        (now);  
        bean1.setIso8601Date(now);  
        (now);  
        (now);  
        ("fastjson serialization result");  
        ((bean1));  
    }  
  
    private static void testParseObject() {  
        String json = "{\n" +  
                "    \"dataFormatDate\": \"2024-12-27 10:50:31\",\n" +  
                "    \"defaultDate\": \"2024-12-27 10:50:31.555\",\n" +  
                "    \"iso8601Date\": \"2024-12-27T10:50:31.555+08:00\",\n" +  
                "    \"millisDate\": 1735267831555,\n" +  
                "    \"unixTimeDate\": 1735267831\n" +  
                "}";  
        Bean yyyyMMdd = (json, );  
        ("fastjson2 deserialization result: " + ());  
    }  
}

Fastjson2's test bean class

public class Bean {  
  
    @JSONField(format = "millis")  
    private Date millisDate;  
    @JSONField(format = "unixtime")  
    private Date unixTimeDate;  
    @JSONField(format = "iso8601")  
    private Date iso8601Date;  
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")  
    private Date dataFormatDate;  
  
    private Date defaultDate;  
  
    public Date getDataFormatDate() {  
        return dataFormatDate;  
    }  
  
    public void setDataFormatDate(Date dataFormatDate) {  
         = dataFormatDate;  
    }  
  
    public Date getMillisDate() {  
        return millisDate;  
    }  
  
    public void setMillisDate(Date millisDate) {  
         = millisDate;  
    }  
  
    public Date getUnixTimeDate() {  
        return unixTimeDate;  
    }  
  
    public void setUnixTimeDate(Date unixTimeDate) {  
         = unixTimeDate;  
    }  
  
    public Date getIso8601Date() {  
        return iso8601Date;  
    }  
  
    public void setIso8601Date(Date iso8601Date) {  
        this.iso8601Date = iso8601Date;  
    }  
  
    public Date getDefaultDate() {  
        return defaultDate;  
    }  
  
    public void setDefaultDate(Date defaultDate) {  
         = defaultDate;  
    }  
  
    @Override  
    public String toString() {  
        return "Bean{" +  
                "millisDate=" + millisDate +  
                ", unixTimeDate=" + unixTimeDate +  
                ", iso8601Date=" + iso8601Date +  
                ", dataFormatDate=" + dataFormatDate +  
                ", defaultDate=" + defaultDate +  
                '}';  
    }  
}

Fastjson test class

public class Bean1 {  
  
    @JSONField(format = "millis")  
    private Date millisDate;  
    @JSONField(format = "unixtime")  
    private Date unixTimeDate;  
//    @JSONField(format = "iso8601")  
    @JSONField(serialzeFeatures = SerializerFeature.UseISO8601DateFormat)  
    private Date iso8601Date;  
  
    private Date defaultDate;  
  
    public Date getMillisDate() {  
        return millisDate;  
    }  
  
    public void setMillisDate(Date millisDate) {  
         = millisDate;  
    }  
  
    public Date getUnixTimeDate() {  
        return unixTimeDate;  
    }  
  
    public void setUnixTimeDate(Date unixTimeDate) {  
         = unixTimeDate;  
    }  
  
    public Date getIso8601Date() {  
        return iso8601Date;  
    }  
  
    public void setIso8601Date(Date iso8601Date) {  
        this.iso8601Date = iso8601Date;  
    }  
  
    public Date getDefaultDate() {  
        return defaultDate;  
    }  
  
    public void setDefaultDate(Date defaultDate) {  
         = defaultDate;  
    }  
  
    @Override  
    public String toString() {  
        return "Bean1{" +  
                "millisDate=" + millisDate +  
                ", unixTimeDate=" + unixTimeDate +  
                ", iso8601Date=" + iso8601Date +  
                ", defaultDate=" + defaultDate +  
                '}';  
    }  
}

Running results

fastjson2 global configuration format pre-serialization result
{"dataFormatDate":"2024-12-27 11:22:48","defaultDate":"2024-12-27 11:22:48.404","iso8601Date":"2024-12-27T11:22:48.404+08:00","millisDate":1735269768404,"unixTimeDate":1735269768}

fastjson2 global configuration format after serialization results
{"dataFormatDate":"2024-12-27 11:22:48","defaultDate":1735269768404,"iso8601Date":"2024-12-27T11:22:48.404+08:00","millisDate":1735269768404,"unixTimeDate":1735269768}

Fastjson2 method passes in format serialization result
{"dataFormatDate":"2024-12-27 11:22:48","defaultDate":"20241227","iso8601Date":"2024-12-27T11:22:48.404+08:00","millisDate":1735269768404,"unixTimeDate":1735269768}

fastjson serialization results
{"defaultDate":1735269768823,"iso8601Date":1735269768823,"millisDate":1735269768823,"unixTimeDate":1735269768}
fastjson2 deserialization result: Bean{millisDate=Fri Dec 27 10:50:31 CST 2024, unixTimeDate=Fri Dec 27 10:50:31 CST 2024, iso8601Date=Fri Dec 27 10:50:31 CST 2024, dataFormatDate=Fri Dec 27 10:50:31 CST 2024}

This is the article about the simple use of fastjson2 time serialization and deserialization. For more related fastjson2 time serialization and deserialization content, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!