Alibaba FastJSON usage example

 

1. What is fastjson?

fastjson is Alibaba's open source JSON parsing library. It can parse strings in JSON format, and supports serialization of Java Beans to JSON strings, as well as deserialization from JSON strings to JavaBeans.

Fastjson usage scenarios

 

Fastjson has been widely used in various scenarios, including cache storage, RPC communication, MQ communication, network protocol communication, Android client, Ajax server handler, etc.

2. Advantages of fastjson

2.1 Fast

Compared with other JSON libraries, fastjson is fast. Since fastjson released version 1.1.x in 2011, its performance has never been surpassed by other JSON libraries implemented in Java.

2.2 Widely used

Fastjson is widely used in Alibaba and deployed on tens of thousands of servers. Fastjson is widely accepted in the industry. In 2012, it was selected as one of the most popular domestic open source software by Open Source China.

2.3 Complete testing

fastjson has a lot of testcases. In version 1.2.11, there are more than 3321 testcases. Regression testing is performed on each release to ensure consistent quality.

2.4 Easy to use

The API of fastjson is very concise.

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

2.5 Fully functional

Supports generics, supports stream processing of very large text, supports enumeration, supports serialization and deserialization extensions.

The following example tests two important methods, JSON.toJSONString and JSON.parseObject:

 

Fruit entity class:

1  package com.led.daorumysql;
 2  
3  /** 
4  * Define fruit entity class
 5  * @author 86157
 6  *
 7   */ 
8  public  class Fruit {
 9      
10      // Fruit name 
11      private String name;
 12      // Fruit origin 
13      private String origin;
 14      // fruit price 
15      private Integer price;
 16      
17      public String getName() {
 18          return name;
 19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     public String getOrigin() {
24         return origin;
25     }
26     public void setOrigin(String origin) {
27         this.origin = origin;
28     }
29     public Integer getPrice() {
30         return price;
31     }
32     public void setPrice(Integer price) {
33         this.price = price;
34     }
35     @Override
36     public String toString() {
37         return "Fruit [name=" + name + ", origin=" + origin + ", price=" + price + "]";
38     }
39     
40     public Fruit(String name, String origin, Integer price) {
41         super();
42         this.name = name;
43         this.origin = origin;
44         this.price = price;
45     }
46     
47     public Fruit() {
48         super();
49     }
50     
51     
52     
53     
54 }

Main class:

1  package com.led.daorumysql;
 2  
3  import java.util.ArrayList;
 4  import java.util.List;
 5  
6  import com.alibaba.fastjson.JSON;
 7  
8  /** 
9  * FastJSON test class
 10  * @author 86157
 11  *
 12   */ 
13  public  class FastJSONTest {
 14  
15      public  static  void main(String[] args) {
 16          // Instantiate Fruit class 
17          Fruit fruitOne = newFruit("apple", "America", 13 );
 18          Fruit fruitTwo = new Fruit("orange", "China", 15 );
 19          // Create fruitList object 
20          List<Fruit> fruitList = new ArrayList<Fruit> ( );
 21          fruitList.add(fruitOne);
 22          fruitList.add(fruitTwo);
 23          
24          // Convert the java object (Fruit) into a json string 
25          System.out.println("========== Convert java object (Fruit) to json string ==========" );
 26          String fruitOne_jsonString = JSON.toJSONString(fruitOne);
 27         System.out.println("fruitOne_jsonString: " + fruitOne_jsonString);
 28          System.out.println();
 29          
30          // Convert json string to java object (Fruit) 
31          System.out.println("==== ===== Convert json string to java object (Fruit) ==========" );
 32          Fruit fruitOne_parseObject = JSON.parseObject(fruitOne_jsonString, Fruit. class );
 33          System.out. println("fruit object: " + fruitOne_parseObject);
 34          System.out.println("fruit name: " + fruitOne_parseObject.getName());
 35          System.out.println("fruit origin: " + fruitOne_parseObject.
getOrigin ());36          System.out.println("fruit price: " + fruitOne_parseObject.getPrice());
 37          System.out.println();
 38          
39          // Convert java object (fruitList) into json string 
40          System.out.println ("========Convert java object (fruitList) to json string========" );
 41          String fruitList_jsonString = JSON.toJSONString(fruitList);
 42          System.out.println ("fruitList_jsonString: " + fruitList_jsonString);
 43          System.out.println();
 44          
45          // Convert json string to java object (fruitList) 
46          System.out.println("========== Convert json string to java object (fruitList)========");
47         List<Fruit> parseArray = JSON.parseArray(fruitList_jsonString, Fruit.class);
48         System.out.println("fruitOne object: " + parseArray.get(0).toString());
49         System.out.println("fruitTwo object: " + parseArray.get(1).toString());
50         System.out.println("fruitTwo object name: " + parseArray.get(1).getName());
51         
52     }
53 }

Console output:

1 ========= Convert java object (Fruit) to json string ===========
 2 fruitOne_jsonString: {"name":"apple","origin":"America ","price":13 }
 3  
4 =========Convert json string to java object (Fruit) ===========
 5 fruit object: Fruit [name=apple , origin=America, price=13 ]
 6  fruit name: apple
 7  fruit origin: America
 8 fruit price: 13
 9  
10 ========= Convert java object (fruitList) to json string==== ====
 11 fruitList_jsonString: [{"name":"apple","origin":"America","price":13},{"name":"orange","origin":"China"," price":15 }]
 12  
13 ========= Convert json string to java object (fruitList) ========
14 fruitOne object: Fruit [name=apple, origin=America, price=13]
15 fruitTwo object: Fruit [name=orange, origin=China, price=15]
16 fruitTwo object name: orange

Notice:

A constructor must be created in the entity class, otherwise parseObject will report the following error:

Exception in thread "main" com.alibaba.fastjson.JSONException: default constructor not found. class com.led.daorumysql.Fruit
    at com.alibaba.fastjson.util.JavaBeanInfo.build(JavaBeanInfo.java:197)
    at com.alibaba.fastjson.parser.ParserConfig.createJavaBeanDeserializer(ParserConfig.java:465)
    at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:412)
    at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:324)
    at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:594)
    at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:569)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:257)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:227)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:186)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:310)
    at com.led.daorumysql.FastJSONTest.main(FastJSONTest.java:27)

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325683593&siteId=291194637