Class MapResolver
The MapResolver converts the raw Maps created from the JsonParser to higher
quality Maps representing the implied object graph. It does this by replacing
@ref values with the Map indicated by the @id key with the same value.
This approach 'wires' the original object graph. During the resolution process, if 'peer' classes can be found for given Maps (for example, an @type entry is available which indicates the class that would have been associated to the Map, then the associated class is consulted to help 'improve' the quality of the primitive values within the map fields. For example, if the peer class indicated that a field was of type 'short', and the Map had a long value (JSON only returns long's for integer types), then the long would be converted to a short.
The final Map representation is a very high-quality graph that represents the original JSON graph. It can be passed as input to JsonWriter, and the JsonWriter will write out the equivalent JSON to what was originally read. This technique allows json-io to be used on a machine that does not have any of the Java classes from the original graph, read it in a JSON graph (any JSON graph), return the equivalent maps, allow mutations of those maps, and finally this graph can be written out.
- Author:
- John DeRegnaucourt (jdereg@gmail.com)
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
Nested classes/interfaces inherited from class com.cedarsoftware.io.Resolver
Resolver.DefaultReferenceTracker, Resolver.MissingField -
Field Summary
Fields inherited from class com.cedarsoftware.io.Resolver
converter, maxObjectReferences, maxReferenceChainDepth, missingFieldHandler, readOptions, references, returningJavaObjects, stack, unknownTypeClass -
Constructor Summary
ConstructorsConstructorDescriptionMapResolver(ReadOptions readOptions, ReferenceTracker references, com.cedarsoftware.util.convert.Converter converter) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidadjustTypeBeforeResolve(JsonObject rootObj, Type rootType) In Maps mode, substitute sorted collections from JSON's @type with LinkedHashSet/LinkedHashMap.protected ObjectreadWithFactoryIfExists(Object o, Type compType) protected ObjectreconcileResult(Object result, JsonObject rootObj, Type rootType) In Maps mode, handle type reconciliation for the root result when no explicit rootType was specified by the user.protected ObjectresolveArray(Type suggestedType, List<Object> list) protected voidtraverseArray(JsonObject jsonObj) protected voidtraverseCollection(JsonObject jsonObj) Traverse a JsonObject representing a collection (array) and deserialize its elements.voidtraverseFields(JsonObject jsonObj) Walk the JsonObject fields and perform necessary substitutions so that all references matched up.protected voidtraverseMap(JsonObject jsonObj) In Maps mode, Maps (like HashMap) need to have their entries traversed to patch @ref references.Methods inherited from class com.cedarsoftware.io.Resolver
addMapToRehash, addMissingField, addResolvedObjectToCollection, addUnresolvedReference, cleanup, convertIfNonRefType, ensureCollectionCapacity, getConverter, getReadOptions, getReferences, getSealedSupplier, isDirectlyAddableJsonValue, markFinishedIfNot, push, readArray, readBoolean, readDouble, readFloat, readInt, readList, readLong, readMap, readObject, readString, resolveReference, resolveReferenceInCollection, setArrayElement, setArrayElement, toJava, traverseJsonObject, traverseObject, valueToTarget, wrapArrayAndAddToCollection
-
Constructor Details
-
MapResolver
public MapResolver(ReadOptions readOptions, ReferenceTracker references, com.cedarsoftware.util.convert.Converter converter)
-
-
Method Details
-
adjustTypeBeforeResolve
In Maps mode, substitute sorted collections from JSON's @type with LinkedHashSet/LinkedHashMap. This is necessary because TreeSet/TreeMap require Comparators that aren't serialized. Only substitute if the type came from JSON's @type (indicated by typeString being set). If the type was set via asClass(TreeMap.class), the user's request should be honored.- Overrides:
adjustTypeBeforeResolvein classResolver- Parameters:
rootObj- the JsonObject about to be resolvedrootType- the expected root type (may be null)
-
traverseMap
In Maps mode, Maps (like HashMap) need to have their entries traversed to patch @ref references. The parent class's traverseMap() doesn't do this - it just processes keys/values arrays. We override to handle both @keys/@items format and regular String-key Maps.- Overrides:
traverseMapin classResolver- Parameters:
jsonObj- a Map-of-Map representation of the JSON input stream.
-
reconcileResult
In Maps mode, handle type reconciliation for the root result when no explicit rootType was specified by the user.This logic determines the appropriate return type: - Arrays are always returned as actual arrays (not JsonObject) - If @type is a simple type (String, Number, etc.), convert to that type - If the result is a complex type, return the raw JsonObject - If the result is already a simple type, return it as-is
- Overrides:
reconcileResultin classResolver- Parameters:
result- the resolved Java objectrootObj- the original JsonObjectrootType- the expected root type (may be null)- Returns:
- the reconciled result (may be converted or wrapped)
-
readWithFactoryIfExists
- Specified by:
readWithFactoryIfExistsin classResolver
-
traverseFields
Walk the JsonObject fields and perform necessary substitutions so that all references matched up. This code patches @ref and @id pairings up, in the 'Map of Map' mode. Where the JSON may contain an '@id' of an object which can have more than one @ref to it, this code will make sure that each '@ref' (value side of the Map associated to a given field name) will be pointer to the appropriate Map instance.Note: We intentionally do NOT call setFinished() here because in Maps mode, the JsonObject itself is the final result, and the same JsonObject may later be converted to Java objects via toJava(). Marking it finished here would cause the ObjectResolver to skip it.
- Specified by:
traverseFieldsin classResolver- Parameters:
jsonObj- a Map-of-Map representation of the current object being examined (containing all fields).
-
traverseArray
- Specified by:
traverseArrayin classResolver
-
traverseCollection
Traverse a JsonObject representing a collection (array) and deserialize its elements.- Specified by:
traverseCollectionin classResolver- Parameters:
jsonObj- The JsonObject representing the collection.
-
resolveArray
- Specified by:
resolveArrayin classResolver
-