Reputation: 951
let's say i have two objects: Person and ReqSingUp, and ReqSingUp contains Person.
Now if i try:
Person p=new Person("dan","lala");
ReqSingUp reqSingUp=new ReqSingUp(p);
String s = gson.toJson(reqSingUp,ReqSingUp.class);
Object o = gson.fromJson(s, Object.class);
if (o instanceof ReqSingUp) {
System.out.println("it's ReqSingUp");
}
if (o instanceof Person ) {
System.out.println("it's person");
}
it does not satisfy any condition (not instanceof ReqSingUp
and not instanceof Person
).
Is there a way to know which type it is?
Thanks in advance.
Upvotes: 0
Views: 59
Reputation: 133609
No, there is no way to directly know it.
This because the information contained in a JSon file doesn't contain directly any type information about the object that is serialized in there. That's why you usually provide the class when reading a JSon file, as in
fromJSon(myObject, MyClass.class)
This indeed creates problem even when reading collection of arbitrary types or genericized objects.
Upvotes: 2