为每个序列实例化GSON的新实例? [英] Instantiate a new instance of GSON for every serialization?

查看:187
本文介绍了为每个序列实例化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.

推荐答案

根据

在调用Json操作时,Gson实例不保持任何状态.因此,您可以自由地将同一对象重用于多个Json序列化和反序列化操作.

The Gson instance does not maintain any state while invoking Json operations. So, you are free to reuse the same object for multiple Json serialization and deserialization operations.

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆