本文介绍了优化Gson反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 优化反序列化的最佳方法是什么? 我目前使用标准的Gson.toJson和Gson.fromJson方法对一些复杂对象进行序列化和反序列化,如果可能的话,我期望减少反序列化时间。 我的对象中最复杂的元素包含43个变量(如果有的话)。 解决方案如果您想使用Gson,而不是切换到另一个Java到/来自JSON API,并且如果Gson的automagic数据绑定的性能不够好,那么有可能停留在Gson API上,并挤出一些稍微好一点的性能。 在https://github.com/eishay/jvm-serializers/wiki ,结果表明Gson序列化和反序列化的综合性能可能得到改善约25%,通过使用 Gson的流媒体API ,而不是数据绑定。请注意,这通常会显着复杂在用户代码中,与使用数据绑定API的单行程序比较的解决方案(例如 new Gson()。toJson(something))被替换为(很容易)几十行包括循环和条件。因此,改进性能的代价是更复杂的代码。 有关使用流API与数据绑定API的示例,请查看 和 JsonGsonDatabind 实现,在 jvm-serializers项目中。 JsonGsonTree 。 ) 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. 解决方案 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.) 这篇关于优化Gson反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 10:25