1

I have problem when I design a Class to serialize and deserialize.

Here is the problem, I have a Class

class Thing {
  int a;
  String b;
  String type;
  Type typeinfo;
}

Basically the Type is the an abstract class like

abstract class Type {
 //nothing here
}

and I have a few implementation

class CarType extends Type {
  int x;
  int y;
}

and

class TruckType extends Type {
  list<Integer> listOfInt;
}

The String type is set to "CarType" or "TruckType" even others and I could know what's actual type of typeinfo

It's good to use Gson to serialize it but how to deserialize the json string?

EDIT

Here is an example of Json String

case1:
{
 "a" : 1,
 "b" : "aaa",
 "type": "car",
 "typeinfo": {
    "x" : 10,
    "y" : 100
  }
}

case2: 
{ 
  "a" : 1,
  "b" : "aaa",
 "type": "truck",
 "typeinfo": {
   "listOfInt" : [1, 2, 3]
 }
}

Just a quick mock up but I guess the idea should be clear, and the problem is I don't want to put the "type" into the typeinfo which supposed only the information.

mark
  • 292
  • 1
  • 6
  • 18

0 Answers0