Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use FastJson, a high performance JSON development kit

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to use FastJson, a high-performance JSON development kit, this article introduces in detail the corresponding analysis and solutions to this problem, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Fastjson introduction

Fastjson is a JSON processor written in Java language, developed by Alibaba Company.

1. Follow the http://json.org standard and be one of the reference implementations included in its official website.

2. Function qiang, which supports various types of JDK, including basic JavaBean, Collection, Map, Date, Enum, and generics.

3. No dependence, no need to exception additional jar, can run directly on the JDK.

4. Open source, open source using Apache License 2.0 protocol. Http://code.alibabatech.com/wiki/display/FastJSON/Home

5, with ultra-high performance, there is no other json library in the java world that can be compared with fastjson.

If you get Fastjson?

SVN: http://code.alibabatech.com/svn/fastjson/trunk/

WIKI: http://code.alibabatech.com/wiki/display/FastJSON/Home

Issue Tracking: http://code.alibabatech.com/jira/browse/FASTJSON

If you use Maven,maven repository, the configuration is as follows:

Opensesame Alibaba OpenSource Repsoitory http://code.alibabatech.com/mvn/releases/ false

Add dependency dependency to the pom.xml file:

Com.alibaba fastjson 1.0.4

If you are not using maven, you can download it directly:

Binary: http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.1/fastjson-1.1.1.jar

Source: http://code.alibabatech.com/mvn/releases/com/alibaba/fastjson/1.1.1/fastjson-1.1.1-sources.jar

Subversion: http://code.alibabatech.com/svn/fastjson/

Introduction to use:

The main entry for Fastjson is com.alibaba.fastjson.JSON.

Import com.alibaba.fastjson.JSON;public static final Object parse (String text); / / parse the JSON text to JSONObject or JSONArraypublic static final JSONObject parseObject (String text) / parse JSON text into JSONObjectpublic static final T parseObject (String text, Class clazz); / / parse JSON text to JavaBeanpublic static final JSONArray parseArray (String text); / / parse JSON text into JSONArraypublic static final List parseArray (String text, Class clazz); / / parse JSON text into JavaBean collection public static final String toJSONString (Object object); / / serialize JavaBean into JSON text public static final String toJSONString (Object object, boolean prettyFormat) / / serialize JavaBean into formatted JSON text public static final Object toJSON (Object javaObject); convert JavaBean to JSONObject or JSONArray.

Code example:

The code example uses the classes User and Group:

Public class User {private Long id; private String name; public Long getId () {return id;} public void setId (Long id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;}} public class Group {private Long id; private String name; private List users = new ArrayList () Public Long getId () {return id;} public void setId (Long id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} public List getUsers () {return users;} public void setUsers (List users) {this.users = users;}}

Encode code example:

Import com.alibaba.fastjson.JSON;Group group = new Group (); group.setId (0L); group.setName ("admin"); User guestUser = new User (); guestUser.setId (2L); guestUser.setName ("guest"); User rootUser = new User (); rootUser.setId (3L); rootUser.setName ("root"); group.getUsers (). Add (guestUser); group.getUsers (). Add (rootUser); String jsonString = JSON.toJSONString (group); System.out.println (jsonString)

Decode code example:

Group group2 = JSON.parseObject (jsonString, Group.class)

The previous version was 1.1.0, 1.1.0, which used asm and SortFastMatch algorithms to improve performance, but was released without rigorous testing because it was in such a hurry to show its superior performance.

1.1.1 compared with 1.1.0, this is a relatively stable version, with line test coverage re-increased to more than 90%, build verify testcase 983.

This version further improves the asm and SortFieldFastMatch algorithms, further improves performance, adds a lot of testcase, and improves stability. I recommend this version, and you will get amazing performance with this version.

Version 1.1.1 of asm comes from objectweb's asm project and is tailored to meet the needs of fastjson to ensure that the introduction of asm does not cause excessive package size.

In order to better use the sort field martch optimization algorithm to improve the performance of parser, the SerializerFeature.SortField feature is turned on by default when fastjson serialization. The SortFeidFastMatch option is also turned on by default when deserialization. In this way, if you serialize the text with fastjson, the output is sorted by fieldName, and parser can also use this order to optimize reading. In this case, parser can achieve very good performance.

I use the program provided by github.com/eishay/jvm-serializers/ for testing, and the performance data are as follows:

Serialization time deserialization time size compressed size java serialization 854643199889541hessian664310043501313protobuf30081694239149thrift31821951349197avro35752095221133json-lib45734149741485263jackson32452986503271fastjson22921499468251

The script for the test run is:

. / run-chart-include= `cat serializers.txt | tr "\ n", "`cat"

Judging from the above data, the performance of fastjson has surpassed the binary protocols such as protobuf, thrift, and avro. It is difficult for a text protocol to outperform a binary protocol, and I am pleased to announce that I have done it!

In view of the superior performance of fastjson, I recommend doing the following

1, replace all other json libraries, there is no other json library in the java world that can be compared with fastjson.

2. Using serialization and deserialization of fastjson to replace Java serialize,java serialize is not only slow, but also has a large system.

3. Using fastjson to replace hessian,json protocol does not need the large size of hessian, and the performance of fastjson is superior to that of hessian.

4. Use fastjson to cache object data in memached.

How to get it?

If you are a Maven user, you can simply use our Maven repository (http://code.alibabatech.com/mvn/releases/) has subsequent dependencies

Com.alibaba fastjson 1.1.1 this is the answer to the question about how to use the high-performance JSON development package FastJson. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 287

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report