-
Notifications
You must be signed in to change notification settings - Fork 6.5k
FAQ(English Version)
you can download fastjson:
- maven central repo: http://central.maven.org/maven2/com/alibaba/fastjson/
- Sourceforge.net: https://sourceforge.net/projects/fastjson/files/
- maven the newest version of fastjson will be published to central maven repo, you can add the dependency.
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
for android
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.52.android</version>
</dependency>
the entrance Class of fastjson is com.alibaba.fastjson.JSON,the main API are 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);
}
serialize:
String jsonString = JSON.toJSONString(obj);
deserialize:
VO vo = JSON.parseObject("...", VO.class);
vector deserialize:
import com.alibaba.fastjson.TypeReference;
List<VO> list = JSON.parseObject("...", new TypeReference<List<VO>>() {});
fastjson examples:https://github.com/alibaba/fastjson/wiki/Samples-DataBind
Until now, fastjson is the fastest json lib for Java, faster than the self-announced fastest jackson. The link is the third-party test result: https://github.com/eishay/jvm-serializers/wiki/Staging-Results
When doing yourself testing, disable circular reference detect.
JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect)
VO vo = JSON.parseObject("...", VO.class, Feature.DisableCircularReferenceDetect)
Here is the comment about fastjon of the author of jackson cowtowncoder: https://groups.google.com/forum/#!topic/java-serialization-benchmarking/8eS1KOquAhw
fastjson is 6 times faster than gson, here is the testing result: https://github.com/eishay/jvm-serializers/wiki/Staging-Results 。gson的g可能是“龟”拼音的缩写,龟速的json库。
fastjson has specific version for android, whose unusually function is removed. the size of jar is smaller. git branch address: https://github.com/alibaba/fastjson/tree/android
no, the only need is that the class is a java bean specification satisfied class.
so easy, for example:
JSON.toJSONStringWithDateFormat(date, "yyyy-MM-dd HH:mm:ss.SSS")
with ISO-8601 date format
JSON.toJSONString(obj, SerializerFeature.UseISO8601DateFormat);
global setting of date format
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
auto recognize date format as below when deserialize:
- ISO-8601 format
- yyyy-MM-dd
- yyyy-MM-dd HH:mm:ss
- yyyy-MM-dd HH:mm:ss.SSS
- miliseconds
- miliseconds string
- .NET JSON format
- new Date(198293238)
use SimplePrePropertyFilter to filter the property,more info here:https://github.com/alibaba/fastjson/wiki/%E4%BD%BF%E7%94%A8SimplePropertyPreFilter%E8%BF%87%E6%BB%A4%E5%B1%9E%E6%80%A7
here is the detailed introduction about customizing serialize: https://github.com/alibaba/fastjson/wiki/%E5%AE%9A%E5%88%B6%E5%BA%8F%E5%88%97%E5%8C%96
use SerializerFeature.DisableCircularReferenceDetect to close the refrence check and generate:
String jsonString = JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect);
fastjson has BrowserCompatible configuration,after opened,all Chinese will be serialized to the format with \uXXXX.
String jsonString = JSON.toJSONString(obj, SerializerFeature.BrowserCompatible);
fastjson offers Stream API: https://github.com/alibaba/fastjson/wiki/Stream-api
fastjson offers Annotation to customize the serialize and deserialize: https://github.com/alibaba/fastjson/wiki/JSONField
如有需要修改本注脚,请联系阿里巴巴,
© Alibaba Fastjson Develop Team
注明: 版权所有阿里巴巴,请注明版权所有者
If you need to amend this footnote, please contact Alibaba.
© Alibaba Fastjson Develop Team
Note: Copyright Alibaba, please indicate the copyright owner