3

I have the following code snippet:

public class MyClass{

private Object returnValue;

//getters setters

}

Gson gson=...;
MyClass cl=new MyClass();

cl.setResultValue(***new OtherClass()***);

gson.toJson(cl,MyClass.class);

When I try to deserialize, I need to observe OtherClass instance in returnValue field, but I get LinkedHashMap there. I understand this is likely because of Object field type, but how to force it get the actual type of object, not just Object?

weston
  • 54,145
  • 21
  • 145
  • 203
avalon
  • 2,231
  • 3
  • 24
  • 49
  • 1
    What is the otherObject you are passing? can you show small example and what the json result? – Aviad Jan 06 '16 at 08:28
  • I'm removing spring tag, add it back if you really need it, but include some info about how it relates to that tag in the question. – weston Jan 06 '16 at 08:38

3 Answers3

0

You can achive it with Generics.

At first make you class generic like below:

public class MyClass<T>{

    private T returnValue;

    public T getReturnValue(){
        return returnValue;
    }
    public void setReturnValue(T returnValue){
        this.returnValue = returnValue;
    }    
}

After that you can deserialize json with Type:

public static void main(String[] args){
    Gson gson = new Gson();
    MyClass<OtherClass> myClass = new MyClass<>();
    myClass.setReturnValue(new OtherClass());
    //serialization
    String json = gson.toJson(myClass);    
    Type type = new TypeToken<MyClass<OtherClass>>
    MyClass<OtherClass> deserializedClass = gson.fromJson(json, type);
}
-1

Look on the next flow i have done

public class SomeClass {

    private int x;
    private Object object;

    public SomeClass(Object object) {
        this.object = object;
    }

}

public class AnotherClass {

    private String test;
    private int y;

    public AnotherClass() {
        test = "dsadas";
        y = 5;
    }

}

When i am doing the next:

gson.toJson(new SomeClass(new AnotherClass()));

The result is: {"x":0,"object":{"test":"dsadas","y":5}}

As you can see it works fine this way

EDIT: In case of deserialization there is a known issues and handling with this situation. The problem that gson don't know what to convert the json to.. So you have to send in gson the type you want to parse it too and build deserizaler.

What is more common is to create Interface for this other class.. So you can mark it. Let's say IOtherClass

Now one of the fields for IOtherClass will be the TYPE.

You need to build deseriaze for IOtherClass, and there you will make the conversion according to the type. This is the only way to tell gson what to parse to

Look on this thread: How to handle deserializing with polymorphism?

And this one: Gson serialize a list of polymorphic objects

Community
  • 1
  • 1
Aviad
  • 1,539
  • 1
  • 9
  • 24
  • The issue they have is in deserialization. – weston Jan 06 '16 at 08:39
  • Thanks, but the issue is when I deserialize the parent object, the clild object is LinkedHashMap, but not Object with instance of AnotherClass like I need – avalon Jan 06 '16 at 08:41
  • I can't make an interface, in resultValue I may have any object of any type. I thought there is a way to determine the actual type from instance. – avalon Jan 06 '16 at 08:47
  • 1
    So unfortunately this is impossible. What is the purpose? What you are trying to achieve? maybe there is other solutions – Aviad Jan 06 '16 at 08:54
-1

I didn't find any graceful solution, so I just decided to deal with inner LinkedHashMap and process it manually. Thanks

avalon
  • 2,231
  • 3
  • 24
  • 49