10

I'm trying to exchange messages between a client and a server using GSON.

The problem is the following:

I have this structure:

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

Then the object content can be a various set of Classes.

I found 2 tutorials here and here, but none of them solves the problem.

Edit1:

The class Message is this one:


public class Mensagem
{
    private TipoMensagem    type;

    private Conteudo        conteudo;

    private Cliente         autor;
    private Cliente         destino;    // null -> to all(broadcast)
}

And Content is this one:

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;
    }    
} 

And an example of a extend class from conteudo is this one:

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;
    }
}

Finally what I do is like : "String strObject = new Gson().toJson(mensage);" which works but on deserialization it doesn't because it assumes always that it is from Content class

Pi Da
  • 359
  • 2
  • 8
André Carvalho
  • 253
  • 1
  • 2
  • 7
  • Are you having a problem with serialization, deserialization, both? – Programmer Bruce Apr 14 '13 at 18:09
  • both. When i try to serialize it get's in a loop and i get a "stack over flow" error. The thing is the abstract class is Content and not Message – André Carvalho Apr 14 '13 at 19:13
  • Gson is not going to be able to magically figure out an unknown child class type based on a JSON string. You're going to have to write a custom serializer that looks at your `TypeOfContent` enum and acts appropriately. – Brian Roach Apr 14 '13 at 23:32

3 Answers3

12

I finally solved it!

    // GSON

    GsonBuilder gsonBilder = new GsonBuilder();
    gsonBilder.registerTypeAdapter(Conteudo.class, new InterfaceAdapter<Conteudo>());
    gsonBilder.setPrettyPrinting();

    Gson gson =gsonBilder.create();

    String str2send = gson.toJson(message);

    Mensagem msg_recv = gson.fromJson(str2send,Mensagem.class);

Note that: "registerTypeAdapter(AbstractClass.class, new InterfaceAdapter());"

by AbstractClass.class i mean the class that you are implementing in my case it was Conteúdo that could be ConteudoTweet or ConteudoUserSystem and so on...

The implementation of InterfaceAdapter is :

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<T>
    implements JsonSerializer<T>, JsonDeserializer<T> {

    @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;
    }

}

And this InterfaceAdapter is generic so it should work in general...

That's it!

André Carvalho
  • 253
  • 1
  • 2
  • 7
  • This worked for me - you should accept your own answer. My problem was in my JsonDeserializer, I was creating a new GSON instance and using gson.fromJson() rather than context.deserialize(). – Jacob Tabak Apr 17 '14 at 21:06
  • 9
    This is just a copy from another answer here : http://stackoverflow.com/a/9550086/795673 – Victor Wong Feb 06 '15 at 06:28
1

You should take a look on a similar question I've answered here : https://stackoverflow.com/a/22081826/3315914

You need to use Gson's RuntimeTypeAdapterFactory

And register the base class and all child classes to make it work.

Community
  • 1
  • 1
rpax
  • 4,468
  • 7
  • 33
  • 57
0

Here is my take on Sub type serialization. (github repo here)

// GsonSerializer.java
package com.rathnas.main;

import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
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;
import com.rathnas.vo.Thing;
import com.rathnas.vo.sub.Animal;
import com.rathnas.vo.sub.Bird;
import com.rathnas.vo.sub.nested.Barking;
import com.rathnas.vo.sub.nested.Chirping;
import com.rathnas.vo.sub.nested.NoiseType;

public class GsonSerializer {
    public static void main(String[] args) {
        GsonBuilder builder = new GsonBuilder().registerTypeAdapter(Thing.class, new ThingSerializer<Thing>()).registerTypeAdapter(NoiseType.class, new ThingSerializer<NoiseType>());
        builder.setPrettyPrinting();
        Gson gson = builder.create();
        Animal thing = God.createDog();
        String tmp = gson.toJson(thing, Thing.class); // Note: StackoverflowError, if you do gson.toJson(thing)
        System.out.println("Ser Dog: " + tmp);
        System.out.println("Des Dog: " + gson.fromJson(tmp, Thing.class));
        Bird thing2 = God.createBird();
        tmp = gson.toJson(thing2, Thing.class);
        System.out.println("\n\n\nSer Bird: " + tmp);
        System.out.println("Des Bird: " + gson.fromJson(tmp, Thing.class));
    }
}

class ThingSerializer<T> implements JsonSerializer<T>, JsonDeserializer<T> {
    private static final String TYPE = "type";

    public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        JsonObject jsonObj = json.getAsJsonObject();
        String className = jsonObj.get(TYPE).getAsString();
        try {
            return context.deserialize(json, Class.forName(className));
        } catch (ClassNotFoundException e) {
            throw new JsonParseException(e);
        }
    }
    public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
        JsonElement jsonEle = context.serialize(src, src.getClass());
        jsonEle.getAsJsonObject().addProperty(TYPE, src.getClass().getCanonicalName());
        return jsonEle;
    }
}

class God {
    public static Animal createDog() {
        Animal thing = new Animal();
        thing.setName("Dog");
        thing.setLegs(4);
        thing.setWings(0);
        thing.setNoise(new Barking());
        return thing;
    }
    public static Bird createBird() {
        Bird thing = new Bird();
        thing.setName("Bird");
        thing.setLegs(1);
        thing.setWings(2);
        thing.setNoise(new Chirping());
        return thing;
    }
}
// Thing.java
public abstract class Thing {
    private String name;
    private NoiseType noise;
    ..
}

// Animal.java
public class Animal extends Thing implements iThing {
    private Integer legs;
    private Integer wings;
    ..
}
// Bird.java
public class Bird extends Thing implements iThing {
    private Integer legs;
    private Integer wings;
    ..
}
// NoiseType.java
public abstract class NoiseType {..}
// Chirping.java
public class Chirping extends NoiseType {..}
// Barking.java
public class Barking extends NoiseType {..}

Output

Ser Dog: {
  "legs": 4,
  "wings": 0,
  "name": "Dog",
  "noise": {
    "noise": "barking",
    "type": "com.rathnas.vo.sub.nested.Barking"
  },
  "type": "com.rathnas.vo.sub.Animal"
}
Des Dog: Animal [legs=4, wings=0, noise=NestedAbstractClass [noise=barking]]

Ser Bird: {
  "legs": 1,
  "wings": 2,
  "name": "Bird",
  "noise": {
    "noise": "chirping",
    "type": "com.rathnas.vo.sub.nested.Chirping"
  },
  "type": "com.rathnas.vo.sub.Bird"
}
Des Bird: Bird [legs=1, wings=2, noise=NestedAbstractClass [noise=chirping]]
Anand Rockzz
  • 6,072
  • 5
  • 64
  • 71