Fastjson FAQ


Fastjson FAQ list

1. How to obtain fastjson?


You can download fastjson from the following place:

  • maven central repository: http://central.maven.org/maven2/com/alibaba/fastjson/
  • Sourceforge.net : https://sourceforge.net/projects/fastjson /files/
  • How to configure fastjson dependencies in maven. The latest version of fastjson will be released to the maven central warehouse, and you can depend on it directly.
<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.21</version>
</dependency>

android version

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.1.55.android</version>
</dependency>

2. What are the main APIs of fastjson?

The fastjson entry class is com.alibaba.fastjson.JSON, and the main API is JSON.toJSONString, and parseObject.

package com.alibaba.fastjson;
public abstract class JSON {
      public static final String toJSONString(Object object);
      public static final <T> T parseObject(String text, Class<T> clazz, Feature... features);
}


Serialization:

String jsonString = JSON.toJSONString(obj);

Deserialization:

VO vo = JSON.parseObject("...", VO.class);

Pan Type deserialization:

import com.alibaba.fastjson.TypeReference;List<VO> list = JSON.parseObject("...", new TypeReference<List<VO>>() {});

3. Where to find fastjson usage examples

Look here for fastjson usage examples: Samples-DataBind

4. What is the performance of fastjson?

Fastjson is currently the fastest json library in the Java language, faster than Jackson, which claims to be the fastest. Check the third-party independent test results here: fastjson performance comparison.

When doing performance testing by yourself, turn off the circular reference detection function.

JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect)VO vo = JSON.parseObject("...", VO.class, Feature.DisableCircularReferenceDetect)

Here is the performance evaluation of fastjson by jackson author cowtowncoder and others: https://groups.google.com/forum/#!topic/java-serialization-benchmarking/8eS1KOquAhw

5. How is the performance of fastjson compared to gson?

fastjson is about 6 times faster than gson. The test results are here: Performance test comparison. The g of gson may be the abbreviation of "turtle" pinyin, a turtle-speed json library.

6. Can fastjson run on android?

fastjson has a special version for android, removing uncommon functions. The number of bytes occupied by jar is smaller. The git branch address is: https://github.com/alibaba/fastjson/tree/android.

7. Does fastjson serialization require configuring java bean serialization like json-lib?

No, no special configuration is required for fastjson serialization and deserialization. The only requirement is that the class you serialize conforms to the Java bean specification.

8. How fastjson handles dates

The API for fastjson to handle dates is very simple, for example:

JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd HH:mm:ss.SSS")

Use ISO- 8601Date format

JSON.toJSONString(obj, SerializerFeature.UseISO8601DateFormat);

Global modification date format

JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);

Deserialization can automatically recognize the following date format:

  • ISO-8601 date format
  • yyyy-MM-dd
  • yyyy-MM-dd HH :mm:ss
  • yyyy-MM-dd HH:mm:ss.SSS
  • Millisecond number
  • Millisecond number string
  • .NET JSON date Format
  • new Date(198293238)

9. How to customize serialization?

You can use SimplePrePropertyFilter to filter fields, see here for details: https://github.com/alibaba/fastjson/wiki/Use SimplePropertyPreFilter to filter properties

For a detailed introduction to custom serialization, see Here: Fastjson custom serialization

10. When the object has a reference, the serialized result is not supported by the browser. What should I do?

Use the SerializerFeature.DisableCircularReferenceDetect property to turn off reference detection and generation. For example:

String  jsonString = JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect);

11. IE 6 does not support JSON with Chinese strings, what should I do?

fastjson provides the BrowserCompatible configuration. After opening it, all Chinese will be serialized into the \uXXXX format. The number of bytes will be more, but it is compatible with IE 6.

String  jsonString = JSON.toJSONString(obj, SerializerFeature.BrowserCompatible);

12. How fastjson handles oversized objects and oversized JSON text

fastjson provides a Stream API, see here for details Fastjson Stream api

13. Use @JSONField to customize serialization

fastjson provides the function of customizing serialization and deserialization using Annotation. Fastjson JSONField