强制 GSON 使用特定的构造函数
public class UserAction {
private final UUID uuid;
private String userId;
/* more fields, setters and getters here */
public UserAction(){
this.uuid = UUID.fromString(new com.eaio.uuid.UUID().toString());
}
public UserAction(UUID uuid){
this.uuid = uuid;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UserAction other = (UserAction) obj;
if (this.uuid != other.uuid && (this.uuid == null || !this.uuid.equals(other.uuid))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 53 * hash + (this.uuid != null ? this.uuid.hashCode() : 0);
return hash;
}
}
我正在使用 Gson 来序列化和反序列化这个类。今天我必须在这个对象中添加一个最终的 UUID。我的序列化没有问题。我需要强制 gson 在反序列化时使用 public UserAction(UUID uuid) 构造函数。我怎样才能做到这一点?
public class UserAction {
private final UUID uuid;
private String userId;
/* more fields, setters and getters here */
public UserAction(){
this.uuid = UUID.fromString(new com.eaio.uuid.UUID().toString());
}
public UserAction(UUID uuid){
this.uuid = uuid;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UserAction other = (UserAction) obj;
if (this.uuid != other.uuid && (this.uuid == null || !this.uuid.equals(other.uuid))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 53 * hash + (this.uuid != null ? this.uuid.hashCode() : 0);
return hash;
}
}
I am using Gson to serilize and deserialize this class. As today I had to add a final UUID in this object. I have no problem serializing. I need to force gson to use public UserAction(UUID uuid)
constructor when deserializing. How can I achieve that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以实现自定义 JsonDeserializer 并将其注册到 GSON。
请记住,此代码尚未经过测试。
You could implement a custom JsonDeserializer and register it with GSON.
Bear in mind that this code has not been tested.
解决这个问题的另一种方法是利用这样一个事实:在反序列化过程中,Gson 会用 JSON 中找到的新值破坏构造函数设置的任何值,因此只需使用 InstanceCreator,专门用于“创建未定义无参数的类的实例”构造函数。”当要使用的构造函数仅将参数值分配给字段而不执行任何有效性检查或以其他方式执行任何有意义的基于状态的处理时,此方法尤其有效。
此外,此方法不需要进一步的自定义反序列化 - 不需要
JsonDeserializer
的自定义实现。这对于引入自定义反序列化器来解决一个小问题然后需要“手动”处理附近的其他 JSON 元素的情况可能是有利的,这可能是不平凡的。话虽如此,这里有一个有效的解决方案,它使用首选的 UserAction 构造函数,但仅向其传递空引用。 JSON 中的实际值稍后设置。 (Gson 不关心
uuid
字段应该是最终的。)Another approach to solve this problem would be to take advantage of the fact that during deserialization Gson will clobber any values set by constructors with new values found in the JSON, and so just use an InstanceCreator, which exists specifically "to create instances of a class that does not define a no-args constructor." This approach works especially well when the constructor to be used just assigns parameter values to fields, and doesn't perform any validity checks or otherwise perform any meaningful state-based processing.
Also, this approach does not require further custom deserialization -- no custom implementation of
JsonDeserializer
is necessary. This can be advantageous to situations where introducing a custom deserializer to solve one small issue then necessitates "manual" processing of other JSON elements in close proximity, which could be non-trivial.With that said, here's such a working solution that uses the preferred
UserAction
constructor, but passes it only a null reference. The actual value from the JSON is later set. (Gson doesn't care that theuuid
field is supposed to be final.)