I have a class hierarchy that I want to convert to and from GSON. I'm not sure how to approach this with GSON (I currently have a class Factorythat looks at JSONObject and is based on the presence or absence of keys, which it calls the correct constructor, which in turn delegates part of its work to a super class) . When I store these objects in a local SQLite DB, I use an integer to indicate my type, and the factory class uses this type to call the correct constructor. I don't have that type in JSON (which is not mine).
How do I tell GSON based on the contents of a JSON object for which an instance of the object is being created for me?
In the examples below, process the ...JSON inside the brackets, as there may or may not be more elements
Here's a breakdown of the class hierarchy:
There is an abstract abstract type: SuperTypewith a JSON representation{"ct":12345,"id":"abc123 ...}
There are two main abstract subtypes: TypeA(has json key "a") and TypeB(has json key "b")
Typea
Example: {"ct":12345,"id":"abc123, "a":{...}}
TypeAhas 15 children (call these TypeA_Aon TypeA_P). The JSON representation of these objects will be something like {"ct":12345,"id":"abc123, "a":{"aa":1 ...} ...}or{"ct":12345,"id":"abc123, "a":{"ag":"Yo dawg I head you like JSON" ...} ...}
Typeb
Example: {"ct":12345,"id":"abc123, "b":{...} ...}
TypeBhas another abstract subtype ( TypeB_A) and few children (let them name these TypeB_Bon TypeB_I). The JSON representation of these objects will be {"ct":12345,"id":"abc123, "b":{"ba":{...} ...} ...}either{"ct":12345,"id":"abc123, "b":{"bg":"Stayin alive" ...} ...}
, , ( ). if (something==null), , .
TypeAdapter TypeAdapterFactory, , , JSON.
GSON JSON, ?
.