Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimizing Gson deserialization

Tags:

java

json

gson

What is the best way to optimize deserialization?

I am currently using the standard Gson.toJson and Gson.fromJson methods for serialization and deserialization of some complex objects and I am looking to reduce deserialization time if its possible.

The most complex of my object contains 43 variables if that matters.

like image 496
HandlerExploit Avatar asked Mar 19 '13 20:03

HandlerExploit


People also ask

How do you deserialize with GSON?

Deserialization in the context of Gson means converting a JSON string to an equivalent Java object. In order to do the deserialization, we need a Gson object and call the function fromJson() and pass two parameters i.e. JSON string and expected java type after parsing is finished. Program output.

Is GSON faster than Jackson?

The tally for fastest library on number of files won is: GSON – 14. JSONP – 5. Jackson – 1.

Is GSON multithreaded?

Gson instances are Thread-safe so you can reuse them freely across multiple threads. You can create a Gson instance by invoking new Gson() if the default configuration is all you need.

Is GSON fast?

Easy to use − Gson API provides a high-level facade to simplify commonly used use-cases. No need to create mapping − Gson API provides default mapping for most of the objects to be serialized. Performance − Gson is quite fast and is of low memory footprint. It is suitable for large object graphs or systems.


3 Answers

I'd switch to Jackson, and consider other solutions much faster than Gson in benchmarks.

like image 176
Programmer Bruce Avatar answered Oct 17 '22 19:10

Programmer Bruce


There is no way to improve the Gson library serialization and deserialization time.

As Programmer Bruce said, if the execution time really matters to you, take a look at the Jackson library. It is, in my opinion, a little bit more "complicated" to use but it has been proven much faster than any other json libraries in benchmarks.

Here are some best practices to improve performances with Jackson.

like image 5
Padrus Avatar answered Oct 17 '22 19:10

Padrus


Gson is known and use for it's ease of use. If you want speed you will have to look at the super popular Jackson Json.

I have tested and benchmarked both Gson and Jackson and I can tell you that in some use cases Jackson is 15 times faster on both serialization and deserialization, even on very big objects.

To get similar behavior as Json I use the following settings

public enum JsonMapper {
    INSTANCE;

    private final ObjectMapper mapper;

    private JsonMapper() {
        mapper = new ObjectMapper();
        VisibilityChecker<?> visibilityChecker = mapper.getSerializationConfig().getDefaultVisibilityChecker();
        mapper.setVisibilityChecker(visibilityChecker
                .withFieldVisibility(Visibility.ANY)
                .withCreatorVisibility(Visibility.NONE)
                .withGetterVisibility(Visibility.NONE)
                .withSetterVisibility(Visibility.NONE)
                .withIsGetterVisibility(Visibility.NONE));
    }

    public ObjectMapper mapper() {
        return mapper;
    }
}

This will turn out to give the exact same json strings as Gson for the same objects. You can set it to use only getters and setters if you want to. I'd advise you to read about all the jackson json annotations for handling subtypes (useful for RPC-style systems).

My use cases : I use jackson as serialization when I need to save blobs to storage systems (Redis, Cassandra, Mongo, ... sometime mysql too). I also use it as the serialization for my RPC-style APIs for fairly high traffic systems, although I prefer using Protobuf for those when possible. In this last case I use Jackson to directly serialize to byte[] or stream for better performance.

One last note : The object mapper is thread safe and having one static instance like in the example I just submitted will prevent you from having the slight instantiation overhead.

EDIT : I am aware that my example doesn't follow a lot of the best practices pointed out in the jackson page but it allows me to have simple to understand code that looks like the Gson.toJson and Gson.fromJson (which I started with too and switched on later to Jackson)

Gson.toJson(object) => JsonMapper.INSTANCE.mapper().writeValueAsString(object) Gson.fromJson(object, clazz) => JsonMapper.INSTANCE.mapper().readValue(jsonString, clazz);

like image 3
le-doude Avatar answered Oct 17 '22 17:10

le-doude