仅在字段上使用自定义的反序列化器? [英] Use a custom deserializer only on fields?

查看:99
本文介绍了仅在字段上使用自定义的反序列化器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个复制机制中同步两个数据库。为了在数据库之间进行通信,我使用Gson将对象序列化为JSON。每个对象都有一个UUID来标识它。为避免发送最新的项目,当对象包含在要复制的对象的字段中时,我使用对象UUID。



我们获得了以下类:

 公共类实体{
字符串uuid;

// Getters和setters ..
}

public class Article extends实体{
String name;
品牌品牌;

// Getters和setters ..
}

public class品牌扩展实体{
字符串名称;
生产者生产者

// Getters和setters ..
}

public class Producer extends Entity {
String name;

// Getters and setters ..
}

如果我序列化文章其JSON表示将如下所示:

  {brand:BrandÖ179d7798-aa63-4dd2-8ff6-885534f99e77 ,uuid:5dbce9aa-4129-41b6-a8e5-d3c030279e82,name:Sun-Maid} 

其中BrandÖ179d7798-aa63-4dd2-8ff6-885534f99e77是类别(品牌)和UUID。



如果我序列化品牌,我期望:

  {producer :ProducerÖ173d7798-aa63-4dd2-8ff6-885534f84732,uuid:5dbce9aa-4129-41b6-a8e5-d3c0302w34w2,name:Carro} 
pre>

在Jackson中,我会将Article类更改为:

  public class Article {
String uuid;
字符串名称;
@JsonDeserialize(使用= EntityUUIDDeserializer.class)@ JsonSerialize(使用= EntityUUIDSerializer.class)
品牌品牌;

// Getters and setters ..
}

和实现自定义序列化器和解串器来返回UUID而不是对象。

Gson没有@JsonDeserialize注释。



如果我们安装序列化器和反序列化器,可以这样做:

  Gson gson = new GsonBuilder()
.registerTypeAdapter(Producer.class,new EntityUUIDDeserializer())
.registerTypeAdapter(Brand.class,new EntityUUIDDeserializer())
.registerTypeAdapter / Producer.class,新的EntityUUIDSerializer())
.registerTypeAdapter(Brand.class,new EntityUUIDSerializer())
.create();

我们可以序列化商品和品牌。

反序列化文章

  Article article = gson.fromJson(inputJSONString,Article.class); 

可以正常工作,但

 品牌品牌= gson.fromJson(inputJSONString,Brand.class); 

不起作用。我猜测是当我们反序列化一个品牌时,我们得到了Brand的反序列化器,试图返回一个UUID字符串,但我们希望反序列化器返回一个Brand对象。

有没有办法避免创建两个不同的Gson对象?两个不同的Gson对象的问题是,当你想要反序列化包含Article和Brand的对象时。

解决方案

您编写序列化程序/反序列化程序并使用Gson注册它(使用 GsonBuilder )。



https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization



$ g $ g $ new gsonBuilder()
.registerTypeAdapter(Producer.class,new MyProducerDeserializer())
.registerTypeAdapter(Producer.class,new MyProducerSerializer() ))
.create();

当您序列化/反序列化您的品牌类,它将把它们用于其中包含的 Producer 字段。

I do a replication mechanism where I synchronize two databases. For communicating between databases I serialize the objects into JSON using Gson. Each object has a UUID to identify it. To avoid having to send the items that are up to date I use the objects UUID when an object is included in a field in an object to be replicated.

We got the following classes:

public class Entity {
    String uuid;

// Getters and setters..
}

public class Article extends Entity {
    String name;
    Brand brand;

// Getters and setters..
}

public class Brand extens Entity {
    String name;
    Producer producer 

// Getters and setters..
}

public class Producer extends Entity {
    String name;

// Getters and setters..
}

If I serialize Article its JSON representation will look like this:

{"brand":"BrandÖ179d7798-aa63-4dd2-8ff6-885534f99e77","uuid":"5dbce9aa-4129-41b6-a8e5-d3c030279e82","name":"Sun-Maid"}

where "BrandÖ179d7798-aa63-4dd2-8ff6-885534f99e77" is the class ("Brand") and the UUID.

If I serialize Brand I expect:

{"producer":"ProducerÖ173d7798-aa63-4dd2-8ff6-885534f84732","uuid":"5dbce9aa-4129-41b6-a8e5-d3c0302w34w2","name":"Carro"}

In Jackson I would change Article class to:

public class Article {
    String uuid;
String name;
    @JsonDeserialize (using = EntityUUIDDeserializer.class) @ JsonSerialize (using = EntityUUIDSerializer.class)        
    Brand brand;

// Getters and setters..
}

and implement custom serializer and deserializer to return the UUID instead of the object.

Gson do not have a @JsonDeserialize annotation.

If we install the serializer and deserializer doing like this:

Gson gson = new GsonBuilder()
          .registerTypeAdapter(Producer.class, new EntityUUIDDeserializer())
          .registerTypeAdapter(Brand.class, new EntityUUIDDeserializer())  
          .registerTypeAdapter/Producer.class, new EntityUUIDSerializer())
          .registerTypeAdapter(Brand.class, new EntityUUIDSerializer())                 
          .create();

We can serialize Article and Brand ok.

Deserialize Article by

Article article= gson.fromJson(inputJSONString, Article.class);

works fine but

Brand brand= gson.fromJson(inputJSONString, Brand.class);

do not work. I guess the probem is that when we deserialize a Brand we get the deserializer for Brand to kick in trying to return an UUID string, but we want the deserializer to return a Brand-object instead.

Is there a way to avoid creating two different Gson objects? The problem with two diffrent Gson objects is when you want to deserialize an object that contains both Article and Brand.

解决方案

You write the serializer/deserializer and register it with Gson (using the GsonBuilder).

https://sites.google.com/site/gson/gson-user-guide#TOC-Custom-Serialization-and-Deserialization

Gson g = new GsonBuilder()
              .registerTypeAdapter(Producer.class, new MyProducerDeserializer())
              .registerTypeAdapter(Producer.class, new MyProducerSerializer())                  
              .create();

When you serialize/deserialize your Brand class, it will use them for the Producer field contained therein.

这篇关于仅在字段上使用自定义的反序列化器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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