json-iterator / java

jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
http://jsoniter.com/
MIT License
1.51k stars 519 forks source link

Concerns regarding the way jsoniter is implemented for high performance parsing #342

Open ahrooran-r opened 1 year ago

ahrooran-r commented 1 year ago

So my pom.xml has following dependencies:

<dependency>
    <groupId>com.jsoniter</groupId>
    <artifactId>jsoniter</artifactId>
    <version>0.9.23</version>
</dependency>

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.29.2-GA</version>
</dependency>

Following is a class I wrote to do a simple test with javaassist enabled

public class Json {
    public static void main(String[] args) {

        System.out.println(JsoniterSpi.getDefaultConfig().decodingMode()); // REFLECTION_MODE
        System.out.println(JsoniterSpi.getDefaultConfig().encodingMode()); // REFLECTION_MODE

        JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
        JsonStream.setMode(EncodingMode.DYNAMIC_MODE);

        String json = "{\"id\":1,\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}";

        Student student = JsonIterator.deserialize(json, Student.class); // decoding to Student.class

        Any any = JsonIterator.deserialize(json); // decoding to any

        System.out.println(student);
        System.out.println(any);
    }

    @ToString
    public static class Name {
        private String firstName;
        public String surname;
    }

    @ToString
    public static class Student {
        public int id;
        public Name name;
    }
}

I got the following answer:

REFLECTION_MODE
REFLECTION_MODE
Json.Student(id=1, name=Json.Name(firstName=null, surname=Blogg))
{"id":1,"name":{"firstName":"Joe","surname":"Blogg"}}

My key takeaways:

  1. By default REFLECTION_MODE is used
  2. When javaassist is added, I have to enable flags to open java modules. For example, --add-opens java.base/java.lang=ALL-UNNAMED
  3. The attributes of a class should be public if we serialize/deserialize using javaassist

Question 1: I need to verify above points and whether or not I understand them correctly. I need the absolute fastest possible encoding/decoding speeds. Is the way I utilized above libraries correct?

Question 2: Suppose I just want to use simple lazy parsing with Any like this: Any any = JsonIterator.deserialize(json);, does javaassist still make a difference in speed here? Provided I do not map to any of my clsses. Just keep deserializing to Any object and use it like a Hashmap. In simple terms I only want to use Any and Iterator APIs. Do I still need javaassist to get the performance boost?

Sources:

  1. https://www.sitepoint.com/php-style-json-parsing-in-java-with-jsoniter/
  2. https://www.baeldung.com/java-jsoniter