JSON Foundation: Instructions for using fastjson

Keywords: JSON github Maven


Fastjson has been a popular issue recently, but as a widely used library of Json, it is still an important choice for many developers along with Gson and Jackson. Even from a security point of view, it is necessary to understand how fastjson is used in order to replace it. This article is a simple summary and summary.

maven reference

Using version 1.2.70 is currently the official solution to the 0-day vulnerability found in 2020, and this version or an updated version is recommended.

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.70</version>
</dependency>

Three classes

There are three main categories in fastjson:

  • JSON
  • JSONObject
  • JSONArray

Description: Both JSONObject and JSONArray inherit from the JSON class. The JSON class itself is an abstract class. JSONObject is used for processing JSON objects. JSONArray is used for processing JSON object arrays. Since JSON itself is an abstract class, it cannot be instantiated to perform the corresponding operations, mainly through its static functions, related parse transformation operations.

Use examples

Overall description: Of the three main classes, JSON is mainly converted by using its static functions, such as from strings to Json objects and from Json objects to strings.JSONObject is used to process Json classes, and JSONArray is used to process Json arrays, just as JSONArray and JSONObject can be combined to form complex Json structures.

  • JSON Definition
public abstract class JSON implements JSONStreamAware, JSONAware {
  • JSONObject Definition
public class JSONObject extends JSON implements Map<String, Object>, Cloneable, Serializable, InvocationHandler {
  • JSONArray Definition
public class JSONArray extends JSON implements List<Object>, Cloneable, RandomAccess, Serializable {

Example 1: Json object and string

  • Sample Code
        final String JSON_PERSON_STRING = "{\"name\":\"liumiao\",\"id\":1001}";
        final JSONObject json = JSON.parseObject(JSON_PERSON_STRING);
        System.out.println("name: " + json.get("name") + " id:" + json.get("id"));
        System.out.println(JSON.toJSONString(json));
  • results of enforcement
name: liumiao id:1001
{"name":"liumiao","id":1001}

Example 2: Json Arrays and Strings

  • Sample Code
        final String JSON_PERSONS_STRING = "[{\"name\":\"liumiao\",\"id\":1001},{\"name\":\"miaoliu\",\"id\":1002}]";
        final JSONArray jsons = JSON.parseArray(JSON_PERSONS_STRING);
        System.out.println(JSON.toJSONString(jsons));

        System.out.println("loop using size()");
        for(int i=0; i<jsons.size(); i++){
            JSONObject json = jsons.getJSONObject(i);
            System.out.println("name: " + json.get("name") + " id:" + json.get("id"));
        }

        System.out.println("loop using iterator");
        Iterator<Object> iterator = jsons.iterator();
        while(iterator.hasNext()) {
            JSONObject json = (JSONObject) iterator.next();
            System.out.println("name: " + json.get("name") + " id:" + json.get("id"));
        }
  • results of enforcement
[{"name":"liumiao","id":1001},{"name":"miaoliu","id":1002}]
loop using size()
name: liumiao id:1001
name: miaoliu id:1002
loop using iterator
name: liumiao id:1001
name: miaoliu id:1002

Example 3: Json composite structure and string

  • Sample Code
        final String JSON_PERSONS_STRING = "[{\"name\":\"liumiao\",\"id\":1001,\"courses\":" +
                "[{\"course\":\"math\",\"score\":90},{\"course\":\"english\",\"score\":80}]}," +
                "{\"name\":\"miaoliu\",\"id\":1002,,\"courses\":" +
                "[{\"course\":\"math\",\"score\":80},{\"course\":\"english\",\"score\":90}]}]";

        final JSONArray jsons = JSON.parseArray(JSON_PERSONS_STRING);
        System.out.println(JSON.toJSONString(jsons));

        System.out.println("loop using size()");
        for (int i = 0; i < jsons.size(); i++) {
            JSONObject json = jsons.getJSONObject(i);
            System.out.println("name: " + json.get("name") + " id:" + json.get("id"));
            JSONArray courses = json.getJSONArray("courses");
            for(int j=0; j<courses.size(); j++) {
                JSONObject course = courses.getJSONObject(j);
                System.out.println("course: " + course.get("course") + " score:" + course.get("score"));
            }
        }

        System.out.println("loop using iterator");
        Iterator<Object> iterator = jsons.iterator();
        while (iterator.hasNext()) {
            JSONObject json = (JSONObject) iterator.next();
            System.out.println("name: " + json.get("name") + " id:" + json.get("id"));

            JSONArray courses = json.getJSONArray("courses");
            Iterator<Object> courseIterator = courses.iterator();
            while(courseIterator.hasNext()){
                JSONObject course = (JSONObject) courseIterator.next();
                System.out.println("course: " + course.get("course") + " score:" + course.get("score"));
            }
        }
  • results of enforcement
[{"courses":[{"score":90,"course":"math"},{"score":80,"course":"english"}],"name":"liumiao","id":1001},{"courses":[{"score":80,"course":"math"},{"score":90,"course":"english"}],"name":"miaoliu","id":1002}]
loop using size()
name: liumiao id:1001
course: math score:90
course: english score:80
name: miaoliu id:1002
course: math score:80
course: english score:90
loop using iterator
name: liumiao id:1001
course: math score:90
course: english score:80
name: miaoliu id:1002
course: math score:80
course: english score:90

Example 4: Json object and JavaBean

  • Sample Code
public class Person {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person(int id, String name) {
        this.id = id;
        this.name= name;
    }
}

Transform Sample Code

        final String JSON_PERSON_STRING = "{\"name\":\"liumiao\",\"id\":1001}";
        final JSONObject json = JSON.parseObject(JSON_PERSON_STRING);
        String name = json.getString("name");

        System.out.println("name: " + json.get("name") + " id:" + json.get("id"));
        // using new
        Person person1 = new Person(json.getInteger("id"),json.getString("name"));
        System.out.println(JSON.toJSONString(person1));

        // using JSON.parsObject
        Person person2 = JSON.parseObject(JSON_PERSON_STRING, Person.class);
        System.out.println(JSON.toJSONString(person2));

        // using JSON.parsObject
        Person person3 = JSON.parseObject(JSON_PERSON_STRING, new TypeReference<Person>() {});
        System.out.println(JSON.toJSONString(person3));
  • results of enforcement
name: liumiao id:1001
{"id":1001,"name":"liumiao"}
{"id":1001,"name":"liumiao"}
{"id":1001,"name":"liumiao"}

Example 5: Json Array and JavaBean

  • Sample Code
        final String JSON_PERSONS_STRING = "[{\"name\":\"liumiao\",\"id\":1001},{\"name\":\"miaoliu\",\"id\":1002}]";
        final JSONArray jsons = JSON.parseArray(JSON_PERSONS_STRING);
        System.out.println(JSON.toJSONString(jsons));

        List<Person> personList1 = new ArrayList<Person>();
        Iterator<Object> iterator = jsons.iterator();

        while(iterator.hasNext()) {
            JSONObject json = (JSONObject) iterator.next();
            personList1.add(new Person(json.getInteger("id"),json.getString("name")));
        }
        System.out.println(JSON.toJSONString(personList1));

        List<Person> personList2 = JSON.parseArray(JSON_PERSONS_STRING,Person.class);
        System.out.println(JSON.toJSONString(personList2));

        List<Person> personList3 = JSON.parseObject(JSON_PERSONS_STRING, new TypeReference<ArrayList<Person>>() {});
        System.out.println(JSON.toJSONString(personList3));

        List<Person> personList4 = new ArrayList<Person>();
        personList4.add(new Person(2001,"liu"));
        personList4.add(new Person(2002,"miao"));
        personList4.add(new Person(2003,"liumiao"));
        System.out.println(JSON.toJSONString(personList4));
  • results of enforcement
[{"name":"liumiao","id":1001},{"name":"miaoliu","id":1002}]
[{"id":1001,"name":"liumiao"},{"id":1002,"name":"miaoliu"}]
[{"id":1001,"name":"liumiao"},{"id":1002,"name":"miaoliu"}]
[{"id":1001,"name":"liumiao"},{"id":1002,"name":"miaoliu"}]
[{"id":2001,"name":"liu"},{"id":2002,"name":"miao"},{"id":2003,"name":"liumiao"}]

Example 6: Json Composite Structure and JavaBean

  • Sample Code
public class Person {
    private int id;
    private String name;
    private List<Course> courses;

    public List<Course> getCourses() {
        return courses;
    }

    public void setCourses(List<Course> courses) {
        this.courses = courses;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person(int id, String name, List<Course> courses) {
        this.id = id;
        this.name= name;
        this.courses=courses;
    }
}

public class Course {
    private String course;
    private int score;

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}
        final String JSON_PERSON_STRING = "{\"name\":\"liumiao\",\"id\":1001,\"courses\":" +
                "[{\"course\":\"math\",\"score\":90},{\"course\":\"english\",\"score\":80}]}";

        final JSONObject json = JSON.parseObject(JSON_PERSON_STRING);
        System.out.println(JSON.toJSONString(json));

        Person person1 = JSON.parseObject(JSON_PERSON_STRING, Person.class);
        System.out.println(JSON.toJSONString(person1));

        Person person2 = JSON.parseObject(JSON_PERSON_STRING,new TypeReference<Person>() {});
        System.out.println(JSON.toJSONString(person2));
  • results of enforcement
{"courses":[{"score":90,"course":"math"},{"score":80,"course":"english"}],"name":"liumiao","id":1001}
{"courses":[{"course":"math","score":90},{"course":"english","score":80}],"id":1001,"name":"liumiao"}
{"courses":[{"course":"math","score":90},{"course":"english","score":80}],"id":1001,"name":"liumiao"}

Reference Content

https://github.com/alibaba/fastjson
https://github.com/alibaba/fastjson/wiki/Quick-Start-CN
https://mvnrepository.com/artifact/com.alibaba/fastjson/

Posted by storyboo on Thu, 11 Jun 2020 17:42:21 -0700