Package com.cedarsoftware.util.io
Class Resolver
java.lang.Object
com.cedarsoftware.util.io.Resolver
- All Implemented Interfaces:
ReaderContext
- Direct Known Subclasses:
MapResolver
,ObjectResolver
This class is used to convert a source of Java Maps that were created from
the JsonParser. These are in 'raw' form with no 'pointers'. This code will
reconstruct the 'shape' of the graph by connecting @ref's to @ids.
The subclasses that override this class can build an object graph using Java
classes or a Map-of-Map representation. In both cases, the @ref value will
be replaced with the Object (or Map) that had the corresponding @id.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
stores missing fields information to notify client after the complete deserialization resolution -
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
Resolver
(ReadOptions readOptions, ReferenceTracker references, com.cedarsoftware.util.convert.Converter converter) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
cleanup()
protected Class<?>
coerceClassIfNeeded
(Class<?> type) protected <T> T
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
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
createInstance
(JsonObject jsonObj) This method creates a Java Object instance based on the passed in parameters.protected Object
createInstanceUsingType
(JsonObject jsonObj) Create an instance of a Java class using the ".type" field on the jsonObj.protected EnumSet<?>
extractEnumSet
(Class c, JsonObject jsonObj) protected void
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 abstract Object
readWithFactoryIfExists
(Object o, Class compType, Deque<JsonObject> stack) <T> T
reentrantConvertJsonValueToJava
(JsonObject rootObj, Class<T> root) This method converts a rootObj Map, (which contains nested Maps and so forth representing a Java Object graph), to a Java object instance.protected void
Process Maps/Sets (fix up their internal indexing structure) This is required because Maps hash items using hashCode(), which will change between VMs.protected JsonObject
resolveRefs
(JsonObject jsonObject) protected abstract void
traverseArray
(Deque<JsonObject> stack, JsonObject jsonObj) protected abstract void
traverseCollection
(Deque<JsonObject> stack, JsonObject jsonObj) protected void
traverseMap
(Deque<JsonObject> stack, JsonObject jsonObj) Process java.util.Map and it's derivatives.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.cedarsoftware.util.io.ReaderContext
getConverter, getReadOptions, getReferences, traverseFields
-
Field Details
-
missingFields
-
-
Constructor Details
-
Resolver
protected Resolver(ReadOptions readOptions, ReferenceTracker references, com.cedarsoftware.util.convert.Converter converter)
-
-
Method Details
-
reentrantConvertJsonValueToJava
This method converts a rootObj Map, (which contains nested Maps and so forth representing a Java Object graph), to a Java object instance. The rootObj map came from using the JsonReader to parse a JSON graph (using the API that puts the graph into Maps, not the typed representation).- Specified by:
reentrantConvertJsonValueToJava
in interfaceReaderContext
- Parameters:
rootObj
- JsonObject instance that was the rootObj object from theroot
- When you know the type you will be returning. Can be null (effectively Map.class) JSON input that was parsed in an earlier call to JsonReader.- Returns:
- a typed Java instance that was serialized into JSON.
-
convertJsonValuesToJava
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).- Parameters:
root
- JsonObject reference to a Map-of-Maps representation of the JSON input after it has been completely read.- Returns:
- Properly constructed, typed, Java object graph built from a Map of Maps representation (JsonObject root).
-
readWithFactoryIfExists
protected abstract Object readWithFactoryIfExists(Object o, Class compType, Deque<JsonObject> stack) -
traverseCollection
-
traverseArray
-
cleanup
protected void cleanup() -
traverseMap
Process java.util.Map and it's derivatives. These are written specially so that the serialization does not expose the class internals (internal fields of TreeMap for example).- Parameters:
stack
- a Stack (Deque) used to support graph traversal.jsonObj
- a Map-of-Map representation of the JSON input stream.
-
convertMapToKeysItems
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.- Parameters:
jObj
- Map to convert
-
createInstance
This method creates a Java Object instance based on the passed in parameters. If the JsonObject contains a key '@type' then that is used, as the type was explicitly set in the JSON stream. If the key '@type' does not exist, then the passed in Class is used to create the instance, handling creating an Array or regular Object instance.
The '@type' is not often specified in the JSON input stream, as in many cases it can be inferred from a field reference or array component type.- Parameters:
jsonObj
- Map-of-Map representation of object to create.- Returns:
- a new Java object of the appropriate type (clazz) using the jsonObj to provide enough hints to get the right class instantiated. It is not populated when returned.
-
createInstanceUsingType
Create an instance of a Java class using the ".type" field on the jsonObj. The clazz argument is not used for determining type, just for clarity in an exception message. TODO: These instances are not all LOADED yet, so that is why they are not in the main createInstance() TODO: method. As they are loaded, they will move up. Also, pulling primitives, class, and others into TODO: factories will shrink this to just unknown generic classes, Object[]'s, and Collections of such. -
coerceClassIfNeeded
-
extractEnumSet
-
patchUnresolvedReferences
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. -
rehashMaps
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. Rehashing the map fixes this.
If useMaps==true, then move @keys to keys and @items to values and then drop these two entries from the map.
This hashes both Sets and Maps because the JDK sets are implemented as Maps. If you have a custom built Set, this would not 'treat' it and you would need to provide a custom reader for that set. -
resolveRefs
-