T
- The type of model this factory produces.@Immutable @LevelUpApi(contract=INTERNAL) public abstract class AbstractJsonModelFactory<T> extends Object
Constructor and Description |
---|
AbstractJsonModelFactory(String typeKey)
Constructs a new factory.
|
Modifier and Type | Method and Description |
---|---|
protected abstract T |
createFrom(JSONObject json)
Parse an instance of the model from a
JSONObject . |
T |
from(JSONObject jsonObject)
Parse a model from the JSON object passed, un-nesting from a root element if necessary.
|
List<T> |
fromList(JSONArray jsonArray)
Parse a list of models from the JSON array passed.
|
protected String |
getTypeKey() |
public AbstractJsonModelFactory(String typeKey)
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. When you are parsing an object as a field of another object, it will not be nested under a type key.
protected final String getTypeKey()
protected abstract T createFrom(JSONObject json) throws JSONException
JSONObject
.json
- the JSONObject
to parse from.T
parsed from json
.JSONException
- If the model fails to parse.public final List<T> fromList(JSONArray jsonArray) throws JSONException
jsonArray
- the jsonArray to parse from.List
of model instances.JSONException
- if any of the models fail to parse.public final T from(JSONObject jsonObject) throws JSONException
jsonObject
- the JSON representation of the model to parse.JSONException
- if the model fails to parse.