Sometimes you need to share state across custom serializers/deserializers (see this discussion). You can use the following three strategies to accomplish this: 1.Store shared state in static fields 2.Declare the serializer/deserializer as inner classes of a parent type, and use the instance fields of parent type to store shared state 3.Use Java ThreadLocal 1 and 2 are not thread-safe options, but 3 is.
I think it may be an efficiency problem. Once it is written as static, it will be in the memory as soon as it is loaded, which will undoubtedly reduce the running speed!
多线程同时调用会有问题吧
以下摘自Gson User Guide:
Variable sharing conflicts are prone to occur
I would like to ask, is gson written incorrectly as json? Or is there a language called gson?
I think it may be an efficiency problem. Once it is written as static, it will be in the memory as soon as it is loaded, which will undoubtedly reduce the running speed!
A Gson object may have a custom Adapter inside, see GsonBuilder documentation
If you only need the default
new Gson()
, you can just use a singleton without any problem