Fastjson serialize objects of complex

First, what is fastjson

fastjson Alibaba open source JSON parsing library that can parse JSON-formatted string, will support Java Bean is serialized to JSON string, you can deserialize from JSON string to the JavaBean.

Two, fastjson advantages

2.1 speed

fastjson relative to other JSON library is characterized by fast, from 2011 onwards fastjson release 1.1.x version, its performance has never been surpassed other JSON library implemented in Java.

Extensive use 2.2

fastjson large-scale use in Alibaba, deployed on tens of thousands of servers, fastjson been widely accepted in the industry. It was named one of the most popular open-source Chinese-made open-source software in 2012.

2.3 Test Complete

fastjson have a lot of testcase, in 1.2.11 version, testcase over 3321. Each release will conduct regression testing to ensure quality and stability.

2.4 easy to use

fastjson the API is very simple.

String text = JSON.toJSONString(obj); //序列化
VO vo = JSON.parseobject("{...}", VO.class); //反序列化

2.5 full-featured
support for generics, support for streaming large text, enumeration support, support serialization and de-serialization of the extension.

Third, download and use

你可以在maven中央仓库中直接下载:
Http://repo1.maven.org/maven2/com/alibaba/fastjson/
或者配置maven依赖
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>x.x.x</version>
</dependency>

Fourth, the project uses

4.1 Serialization

项目使用的是前后端分离架构,前面有提到,这里还是决定把对JSON的处理单独拿出来说一下
SpringMVC的@ResponseBody虽然可以支持对象转json,但显得有些力不从心。当对象的结构复杂数据量变大就处理不了了
使用fastjson就可以快速高效的处理
 JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect);
Fastjson 循环引用
fastjson支持循环引用,并且是缺省打开的。
当序列化后的JSON传输到浏览器或者其他语言中,这些json解析器不支持循环引用,从而导致数据丢失。你可以关闭fastjson的循环引用支持。        关闭引用检测,还能够提升序列化时的性能。

Global Close

 JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.getMask();

4.2 Serialization

VO vo = JSON.parseobject("{...}", VO.class); //反序列化
更多详情可以在<a href="http://www.dba.cn/book/fastjson/">这里</a>看

Guess you like

Origin www.cnblogs.com/shaoyu/p/11848025.html