I am using Google's GSON package http://code.google.com/p/google-gson/
I am converting JSON to Java.
I have this fragment of code where I do the conversion.
Gson gson = new Gson();
Type collectionType = new TypeToken<Collection<QueryProperty>>() {}.getType();
Collection<QueryProperty> queryProperties = gson.fromJson(query, collectionType);
My QueryProperty class has these fields (with getters/setters):
int id;
String uri;
String name;
Set<QueryValue> values;
String selected;
The QueryValue class had these fields (with getters/setters) perviously:
int id;
String uri;
String name;
I now want to be able to have different types
of QueryValue.
I want to add a NumericQueryValue class (subclass of QueryValue), so I can pass in a set of bounds with which to query the db.
double lower;
double upper;
And want to make a new ResourceQueryValue (subclass of QueryValue) which looks the same as QueryValue used to:
int id;
String uri;
String name;
Is there anyway I can make this work. That is have a generic QueryValue class, but have the correct subclass returned depending on the parameters that the JSON supplies.
If I have not been clear please do let me know.