Optimizing Gson deserialization

后端 未结 5 909
野的像风
野的像风 2021-02-04 08:39

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 c

5条回答
  •  情深已故
    2021-02-04 09:02

    If you want to use Gson, and not switch to another Java-to/from-JSON API, and if the performance of Gson's automagic databinding isn't good enough, then it is possible to stay with the Gson API, and squeeze out some moderately better performance.

    In the latest rounds of performance tests posted at https://github.com/eishay/jvm-serializers/wiki, the results suggest that the combined performance of Gson serialization and deserialization could possibly be improved by about 25%, by using the streaming API of Gson instead of databinding.

    Note that this generally significantly complicates the implementation of the user code, where solutions comparable to one-liners using the databinding API, e.g., new Gson().toJson(something), are replaced with (easily) dozens of lines, including loops and conditionals. So, the cost of the improved performance is more complicated code.

    For examples of using the streaming API versus the databinding API, take a look at the JsonGsonManual and JsonGsonDatabind implementations, in the jvm-serializers project.

    (Note: One could also use the tree-model in the Gson API, instead of the streaming or databinding APIs, but it doesn't appear to offer any performance improvements over databinding. For an example, see JsonGsonTree.)

提交回复
热议问题