使用Gson和抽象类

我正在尝试使用GSON在客户端和服务器之间交换消息。

问题如下:

我有这个结构:

public class Message { private TypeOfContent type; // It's a enum private Content content; .... } 

然后,对象内容可以是各种类的类。

我在这里和这里找到了2个教程,但没有一个能解决问题。

EDIT1:

消息类是这样的:


 public class Mensagem { private TipoMensagem type; private Conteudo conteudo; private Cliente autor; private Cliente destino; // null -> to all(broadcast) } 

内容就是这个:

 public class Conteudo { protected TipoConteudo typeConteudo; protected String texto; protected Posicao posicao; public Conteudo(TipoConteudo typeConteudo, String texto, Posicao posicao) { this.texto = texto; this.posicao = posicao; this.typeConteudo = typeConteudo; } } 

来自contedo的扩展类的一个例子是这样的:

 public class ConteudoTweet extends Conteudo { protected String pathImagem; public ConteudoTweet(TipoConteudo typeConteudo, String tweet, Posicao location, String picturePath) { super(typeConteudo,tweet, location); this.pathImagem = picturePath; } } 

最后我做的是:“String strObject = new Gson()。toJson(mensage);” 它的工作原理,但反序列化它不会,因为它始终假定它来自Content类

我终于解决了!

  // GSON GsonBuilder gsonBilder = new GsonBuilder(); gsonBilder.registerTypeAdapter(Conteudo.class, new InterfaceAdapter()); gsonBilder.setPrettyPrinting(); Gson gson =gsonBilder.create(); String str2send = gson.toJson(message); Mensagem msg_recv = gson.fromJson(str2send,Mensagem.class); 

注意:“registerTypeAdapter( AbstractClass.class ,new InterfaceAdapter());”

通过AbstractClass.class我的意思是你在我的案例中实现的类是Conteúdo,可以是ConteudoTweet或ConteudoUserSystem等等……

InterfaceAdapter的实现是:

 import java.lang.reflect.Type; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; public class InterfaceAdapter implements JsonSerializer, JsonDeserializer { @Override public final JsonElement serialize(final T object, final Type interfaceType, final JsonSerializationContext context) { final JsonObject member = new JsonObject(); member.addProperty("type", object.getClass().getName()); member.add("data", context.serialize(object)); return member; } @Override public final T deserialize(final JsonElement elem, final Type interfaceType, final JsonDeserializationContext context) throws JsonParseException { final JsonObject member = (JsonObject) elem; final JsonElement typeString = get(member, "type"); final JsonElement data = get(member, "data"); final Type actualType = typeForName(typeString); return context.deserialize(data, actualType); } private Type typeForName(final JsonElement typeElem) { try { return Class.forName(typeElem.getAsString()); } catch (ClassNotFoundException e) { throw new JsonParseException(e); } } private JsonElement get(final JsonObject wrapper, final String memberName) { final JsonElement elem = wrapper.get(memberName); if (elem == null) { throw new JsonParseException( "no '" + memberName + "' member found in json file."); } return elem; } } 

这个InterfaceAdapter是通用的,因此它应该可以正常工作……

而已!

你应该看看我在这里回答的类似问题: https : //stackoverflow.com/a/22081826/3315914

你需要使用Gson的RuntimeTypeAdapterFactory

并注册基类和所有子类以使其工作。