本文介绍了为每个序列实例化GSON的新实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我的代码中发生了很多事情,内容如下:result = new Gson().fromJson(someString, ResultContainer.class);我是否应该在所有这些地方共享GSON对象?如果是这样,则每个对象或每个类都是静态的(可能甚至是超类?)

Currently, I have a lot of occurences in my code that read as follows: result = new Gson().fromJson(someString, ResultContainer.class); Should I share the GSON object over all those places? If so, per object or static per class (potentially even superclass?)

我主要是因为静态共享引用很好,那为什么Gson对象首先不是静态的呢?除非有人使用奇特的自定义序列化规则,否则上面的方法几乎涵盖了人们希望Gson进行的工作.

I am asking mainly because if sharing the reference statically is fine, then why isn't the Gson object static in the first place? Unless one uses fancy custom serialization rules, the method above pretty much covers what one would want Gson to do.

推荐答案

根据 GSON用户指南:

这不是单例,因为您可以通过 GsonBuilder .

It's not singleton because you can configure it differently via GsonBuilder.

这篇关于为每个序列实例化GSON的新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 16:42
查看更多