为什么GSON使用字段而不是getter / setter?

时间:2022-09-24 21:37:37

Why does GSON use ONLY fields(private,public,protected)? Is there a way to tell GSON to use only getters and setters?

为什么GSON只使用字段(私有,公共,受保护)?有没有办法告诉GSON只使用getter和setter?

4 个解决方案

#1


85  

Generally speaking when you serialize/deserialize an object, you are doing so to end up with an exact copy of the state of the object; As such, you generally want to circumvent the encapsulation normally desired in an OO design. If you do not circumvent the encapsulation, it may not be possible to end up with an object that has the exact same state after deserialization as it had prior to serialization. Additionally, consider the case where you do not want to provide a setter for a particular property. How should serialization/deserialization act if you are working through the getters and setters?

一般来说,当你序列化/反序列化一个对象时,你这样做是为了得到一个对象状态的精确副本;因此,您通常希望避免在OO设计中通常所需的封装。如果不绕过封装,则可能无法在反序列化之后使用与序列化之前具有完全相同状态的对象。此外,请考虑您不希望为特定属性提供setter的情况。如果您正在使用getter和setter,序列化/反序列化应如何起作用?

#2


23  

Is there a way to tell GSON to use only getters and setters?

有没有办法告诉GSON只使用getter和setter?

Not yet.

还没。

From the design doc:

从设计文档:

[T]here are good arguments to support properties as well. We intend to enhance Gson in a latter version to support properties as an alternate mapping for indicating Json fields. For now, Gson is fields-based.

[T]这里也是支持属性的好参数。我们打算在后一版本中增强Gson以支持属性作为指示Json字段的备用映射。目前,Gson是以田地为基础的。

#3


4  

It is possible to to patch Gson to use getters.

可以修补Gson使用getter。

#4


0  

The vague outline of how this works in our app is that we have a lot of TypeAdapter implementations - some for specific value-like objects and some for bean-style objects where we know that JavaBeans logic will work. We then jam all of these onto a GsonBuilder before creating the Gson object.

在我们的应用程序中如何工作的模糊概述是我们有很多TypeAdapter实现 - 一些用于特定的类似对象的对象,一些用于bean风格的对象,我们知道JavaBeans逻辑将起作用。然后,在创建Gson对象之前,我们将所有这些都粘贴到GsonBuilder上。

Unfortunately, GSON is really crap at handling types like Object[]. We mostly saw this when we were trying to make a JSON object to represent method parameters. The workaround for that was to make custom TypeAdapter instances which reflect the methods. (This does mean that you end up using one Gson instance per method you intend to call...)

不幸的是,GSON在处理像Object []这样的类型时确实很糟糕。当我们尝试创建一个JSON对象来表示方法参数时,我们主要看到了这一点。解决方法是制作反映方法的自定义TypeAdapter实例。 (这意味着你最终会为每个打算调用的方法使用一个Gson实例...)

#1


85  

Generally speaking when you serialize/deserialize an object, you are doing so to end up with an exact copy of the state of the object; As such, you generally want to circumvent the encapsulation normally desired in an OO design. If you do not circumvent the encapsulation, it may not be possible to end up with an object that has the exact same state after deserialization as it had prior to serialization. Additionally, consider the case where you do not want to provide a setter for a particular property. How should serialization/deserialization act if you are working through the getters and setters?

一般来说,当你序列化/反序列化一个对象时,你这样做是为了得到一个对象状态的精确副本;因此,您通常希望避免在OO设计中通常所需的封装。如果不绕过封装,则可能无法在反序列化之后使用与序列化之前具有完全相同状态的对象。此外,请考虑您不希望为特定属性提供setter的情况。如果您正在使用getter和setter,序列化/反序列化应如何起作用?

#2


23  

Is there a way to tell GSON to use only getters and setters?

有没有办法告诉GSON只使用getter和setter?

Not yet.

还没。

From the design doc:

从设计文档:

[T]here are good arguments to support properties as well. We intend to enhance Gson in a latter version to support properties as an alternate mapping for indicating Json fields. For now, Gson is fields-based.

[T]这里也是支持属性的好参数。我们打算在后一版本中增强Gson以支持属性作为指示Json字段的备用映射。目前,Gson是以田地为基础的。

#3


4  

It is possible to to patch Gson to use getters.

可以修补Gson使用getter。

#4


0  

The vague outline of how this works in our app is that we have a lot of TypeAdapter implementations - some for specific value-like objects and some for bean-style objects where we know that JavaBeans logic will work. We then jam all of these onto a GsonBuilder before creating the Gson object.

在我们的应用程序中如何工作的模糊概述是我们有很多TypeAdapter实现 - 一些用于特定的类似对象的对象,一些用于bean风格的对象,我们知道JavaBeans逻辑将起作用。然后,在创建Gson对象之前,我们将所有这些都粘贴到GsonBuilder上。

Unfortunately, GSON is really crap at handling types like Object[]. We mostly saw this when we were trying to make a JSON object to represent method parameters. The workaround for that was to make custom TypeAdapter instances which reflect the methods. (This does mean that you end up using one Gson instance per method you intend to call...)

不幸的是,GSON在处理像Object []这样的类型时确实很糟糕。当我们尝试创建一个JSON对象来表示方法参数时,我们主要看到了这一点。解决方法是制作反映方法的自定义TypeAdapter实例。 (这意味着你最终会为每个打算调用的方法使用一个Gson实例...)