T - the type of object to load.@NotThreadSafe @LevelUpApi(contract=INTERNAL) public class GsonModelFactory<T> extends Object
com.scvngr.levelup package. Objects from all other packages will be ignored.| Constructor and Description |
|---|
GsonModelFactory(Class<T> type,
boolean wrapped)
Constructs a new factory.
|
GsonModelFactory(String typeKey,
Class<T> type,
boolean wrapped)
Constructs a new factory.
|
| Modifier and Type | Method and Description |
|---|---|
protected T |
createFrom(com.google.gson.JsonObject json)
Parse an instance of the model from a
JsonObject. |
T |
from(com.google.gson.JsonObject jsonObject)
Parse a model from the JSON object passed, un-nesting from a root element if necessary.
|
T |
from(String json)
Parse a model from the JSON object passed, un-nesting from a root element if necessary.
|
List<T> |
fromList(com.google.gson.JsonArray jsonArray)
Parse a list of models from the JSON array passed.
|
List<T> |
fromList(String json)
Parse a list of models from the JSON array passed.
|
String |
getTypeKey() |
protected void |
onBuildFactory(com.google.gson.GsonBuilder gsonBuilder)
Override this to register type adapters or anything else of that sort.
|
String |
to(T model) |
com.google.gson.JsonElement |
toJsonElement(T model) |
JsonElementRequestBody |
toRequestSerializer(T model)
Serializes a model instance to a JSON string representation in a
RequestBody. |
public GsonModelFactory(Class<T> type, boolean wrapped)
type - the type of object to loadwrapped - if true, the input JSON must be wrapped with a JSON object that has a typeKey,
as derived from type.public GsonModelFactory(String typeKey, Class<T> type, boolean wrapped)
typeKey - the key which the object to parse can be nested under. It will usually be the
name of the object's type:
{ "typeKey": { "field1": "test" } }
When requesting a single object or a list of objects, the object will be nested under
this type key.type - the type of object to loadwrapped - if true, the input JSON must be wrapped with a JSON object that has a typeKey,
as mentioned above.protected void onBuildFactory(com.google.gson.GsonBuilder gsonBuilder)
gsonBuilder - the Gson builder that will be used for this class.public final T from(com.google.gson.JsonObject jsonObject)
jsonObject - the JSON representation of the model to parse.public final T from(String json) throws com.google.gson.JsonParseException
json - the JSON representation of the model to parse.com.google.gson.JsonParseException - if there is a problem decoding the JSON structure.public final List<T> fromList(com.google.gson.JsonArray jsonArray) throws com.google.gson.JsonParseException
jsonArray - the JsonArray containing a list of models.List of model instances.com.google.gson.JsonParseException - if there is a problem decoding the JSON structure.public final List<T> fromList(String json) throws com.google.gson.JsonParseException
json - the JSON representation of the list of models to parse.List of model instances.com.google.gson.JsonParseException - if there is a problem decoding the JSON structure.public final String to(T model)
model - the model to serialize as JSON.model.public final com.google.gson.JsonElement toJsonElement(T model)
model - the model to serialize as JSON.JsonElement representing the given model.public final JsonElementRequestBody toRequestSerializer(T model)
RequestBody.model - the model to serialize as JSON.RequestBody.protected T createFrom(com.google.gson.JsonObject json) throws com.google.gson.JsonParseException
JsonObject.json - the JSON representation of the model to parse.T parsed from json.com.google.gson.JsonParseException - If the model fails to parse.public final String getTypeKey()