public class ObjectResolver extends Object
The ObjectResolver converts the raw Maps created from the JsonParser to Java objects (a graph of Java instances). The Maps have an optional type entry associated to them to indicate what Java peer instance to create. The reason type is optional is because it can be inferred in a couple instances. A non-primitive field that points to an object that is of the same type of the field, does not require the '@type' because it can be inferred from the field. This is not always the case. For example, if a Person field points to an Employee object (where Employee is a subclass of Person), then the resolver cannot create an instance of the field type (Person) because this is not the proper type. (It had an Employee record with more fields in this example). In this case, the writer recognizes that the instance type and field type are not the same and therefore it writes the @type.
A similar case as above occurs with specific array types. If there is a Person[] containing Person and Employee instances, then the Person instances will not have the '@type' but the employee instances will (because they are more derived than Person).
The resolver 'wires' the original object graph. It does this by replacing '@ref' values in the Maps with pointers (on the field of the associated instance of the Map) to the object that has the same ID. If the object has not yet been read, then an UnresolvedReference is created. These are back-patched at the end of the resolution process. UnresolvedReference keeps track of what field or array element the actual value should be stored within, and then locates the object (by id), and updates the appropriate value.
Modifier and Type | Class and Description |
---|---|
protected static class |
Resolver.Missingfields
stores missing fields information to notify client after the complete deserialization resolution
|
Modifier and Type | Field and Description |
---|---|
protected JsonReader.MissingFieldHandler |
missingFieldHandler |
protected Collection<Resolver.Missingfields> |
missingFields |
protected JsonReader |
reader |
Modifier | Constructor and Description |
---|---|
protected |
ObjectResolver(JsonReader reader,
ClassLoader classLoader)
Constructor
|
Modifier and Type | Method and Description |
---|---|
protected void |
assignField(Deque<JsonObject<String,Object>> stack,
JsonObject jsonObj,
Field field,
Object rhs)
Map Json Map object field to Java object field.
|
protected void |
cleanup() |
protected Object |
coerceCertainTypes(String type) |
protected Object |
convertMapsToObjects(JsonObject<String,Object> root)
Walk a JsonObject (Map of String keys to values) and return the
Java object equivalent filled in as best as possible (everything
except unresolved reference fields or unresolved array/collection elements).
|
protected static void |
convertMapToKeysItems(JsonObject<String,Object> map)
Convert an input JsonObject map (known to represent a Map.class or derivative) that has regular keys and values
to have its keys placed into @keys, and its values placed into @items.
|
protected Object |
createJavaObjectInstance(Class clazz,
JsonObject jsonObj)
This method creates a Java Object instance based on the passed in parameters.
|
protected JsonReader.JsonClassReaderBase |
getCustomReader(Class c) |
static Class |
getRawType(Type t)
Given the passed in Type t, return the raw type of it, if the passed in value is a ParameterizedType.
|
protected JsonReader |
getReader() |
protected Map<Class,JsonReader.JsonClassReaderBase> |
getReaders() |
protected JsonObject |
getReferencedObj(Long ref) |
protected void |
handleMissingField(Deque<JsonObject<String,Object>> stack,
JsonObject jsonObj,
Object rhs,
String missingField)
Try to create an java object from the missing field.
|
static Object |
newInstance(Class c,
JsonObject jsonObject) |
protected boolean |
notCustom(Class cls) |
protected void |
patchUnresolvedReferences()
For all fields where the value was "@ref":"n" where 'n' was the id of an object
that had not yet been encountered in the stream, make the final substitution.
|
protected Object |
readIfMatching(Object o,
Class compType,
Deque<JsonObject<String,Object>> stack)
Convert the passed in object (o) to a proper Java object.
|
protected void |
rehashMaps()
Process Maps/Sets (fix up their internal indexing structure)
This is required because Maps hash items using hashCode(), which will
change between VMs.
|
protected void |
traverseArray(Deque<JsonObject<String,Object>> stack,
JsonObject<String,Object> jsonObj)
Traverse the JsonObject associated to an array (of any type).
|
protected void |
traverseCollection(Deque<JsonObject<String,Object>> stack,
JsonObject<String,Object> jsonObj)
Process java.util.Collection and it's derivatives.
|
void |
traverseFields(Deque<JsonObject<String,Object>> stack,
JsonObject<String,Object> jsonObj)
Walk the Java object fields and copy them from the JSON object to the Java object, performing
any necessary conversions on primitives, or deep traversals for field assignments to other objects,
arrays, Collections, or Maps.
|
protected void |
traverseMap(Deque<JsonObject<String,Object>> stack,
JsonObject<String,Object> jsonObj)
Process java.util.Map and it's derivatives.
|
protected JsonReader.MissingFieldHandler missingFieldHandler
protected final JsonReader reader
protected final Collection<Resolver.Missingfields> missingFields
protected ObjectResolver(JsonReader reader, ClassLoader classLoader)
reader
- JsonReader instance being usedclassLoader
- ClassLoader that was set in the passed in 'options' arguments to JsonReader.public void traverseFields(Deque<JsonObject<String,Object>> stack, JsonObject<String,Object> jsonObj)
stack
- Stack (Deque) used for graph traversal.jsonObj
- a Map-of-Map representation of the current object being examined (containing all fields).protected void assignField(Deque<JsonObject<String,Object>> stack, JsonObject jsonObj, Field field, Object rhs)
stack
- Stack (Deque) used for graph traversal.jsonObj
- a Map-of-Map representation of the current object being examined (containing all fields).field
- a Java Field object representing where the jsonObj should be converted and stored.rhs
- the JSON value that will be converted and stored in the 'field' on the associated
Java target object.protected void handleMissingField(Deque<JsonObject<String,Object>> stack, JsonObject jsonObj, Object rhs, String missingField)
stack
- Stack (Deque) used for graph traversal.jsonObj
- a Map-of-Map representation of the current object being examined (containing all fields).rhs
- the JSON value that will be converted and stored in the 'field' on the associated Java target object.missingField
- name of the missing field in the java object.protected void traverseCollection(Deque<JsonObject<String,Object>> stack, JsonObject<String,Object> jsonObj)
jsonObj
- a Map-of-Map representation of the JSON input stream.protected void traverseArray(Deque<JsonObject<String,Object>> stack, JsonObject<String,Object> jsonObj)
stack
- a Stack (Deque) used to support graph traversal.jsonObj
- a Map-of-Map representation of the JSON input stream.protected Object readIfMatching(Object o, Class compType, Deque<JsonObject<String,Object>> stack)
o
- Object to read (convert). Will be either a JsonObject or a JSON primitive String, long, boolean,
double, or null.compType
- Class destination type to which the passed in object should be converted to.stack
- a Stack (Deque) used to support graph traversal.public static Class getRawType(Type t)
t
- Type to attempt to get raw type from.protected JsonReader getReader()
protected Object convertMapsToObjects(JsonObject<String,Object> root)
root
- JsonObject reference to a Map-of-Maps representation of the JSON
input after it has been completely read.protected void cleanup()
protected void traverseMap(Deque<JsonObject<String,Object>> stack, JsonObject<String,Object> jsonObj)
stack
- a Stack (Deque) used to support graph traversal.jsonObj
- a Map-of-Map representation of the JSON input stream.protected static void convertMapToKeysItems(JsonObject<String,Object> map)
map
- Map to convertprotected Object createJavaObjectInstance(Class clazz, JsonObject jsonObj)
clazz
- Instance will be create of this class.jsonObj
- Map-of-Map representation of object to create.protected JsonObject getReferencedObj(Long ref)
protected JsonReader.JsonClassReaderBase getCustomReader(Class c)
protected void patchUnresolvedReferences()
protected void rehashMaps()
public static Object newInstance(Class c, JsonObject jsonObject)
protected Map<Class,JsonReader.JsonClassReaderBase> getReaders()
protected boolean notCustom(Class cls)
Copyright © 2017. All rights reserved.