Class Resolver

java.lang.Object
com.cedarsoftware.io.Resolver
Direct Known Subclasses:
MapResolver, ObjectResolver

public abstract class Resolver extends Object
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.
  • Field Details

  • Constructor Details

    • Resolver

      protected Resolver(ReadOptions readOptions, ReferenceTracker references, com.cedarsoftware.util.convert.Converter converter)
  • Method Details

    • getReadOptions

      public ReadOptions getReadOptions()
    • getReferences

      public ReferenceTracker getReferences()
    • getConverter

      public com.cedarsoftware.util.convert.Converter getConverter()
    • readString

      public String readString(JsonObject jsonObj, String fieldName)
      Convenience method for reading a String field from a JsonObject in ClassFactory implementations. Handles type conversion automatically.
      Parameters:
      jsonObj - the JsonObject (typically passed to ClassFactory.newInstance)
      fieldName - the field name to read
      Returns:
      the String value, or null if not present or null
    • readInt

      public int readInt(JsonObject jsonObj, String fieldName)
      Convenience method for reading an int field from a JsonObject in ClassFactory implementations. Handles type conversion automatically.
      Parameters:
      jsonObj - the JsonObject (typically passed to ClassFactory.newInstance)
      fieldName - the field name to read
      Returns:
      the int value, or 0 if not present or null
    • readLong

      public long readLong(JsonObject jsonObj, String fieldName)
      Convenience method for reading a long field from a JsonObject in ClassFactory implementations. Handles type conversion automatically.
      Parameters:
      jsonObj - the JsonObject (typically passed to ClassFactory.newInstance)
      fieldName - the field name to read
      Returns:
      the long value, or 0L if not present or null
    • readFloat

      public float readFloat(JsonObject jsonObj, String fieldName)
      Convenience method for reading a float field from a JsonObject in ClassFactory implementations. Handles type conversion automatically.
      Parameters:
      jsonObj - the JsonObject (typically passed to ClassFactory.newInstance)
      fieldName - the field name to read
      Returns:
      the float value, or 0.0f if not present or null
    • readDouble

      public double readDouble(JsonObject jsonObj, String fieldName)
      Convenience method for reading a double field from a JsonObject in ClassFactory implementations. Handles type conversion automatically.
      Parameters:
      jsonObj - the JsonObject (typically passed to ClassFactory.newInstance)
      fieldName - the field name to read
      Returns:
      the double value, or 0.0 if not present or null
    • readBoolean

      public boolean readBoolean(JsonObject jsonObj, String fieldName)
      Convenience method for reading a boolean field from a JsonObject in ClassFactory implementations. Handles type conversion automatically.
      Parameters:
      jsonObj - the JsonObject (typically passed to ClassFactory.newInstance)
      fieldName - the field name to read
      Returns:
      the boolean value, or false if not present or null
    • readObject

      public <T> T readObject(JsonObject jsonObj, String fieldName, Class<T> type)
      Convenience method for reading a typed object field from a JsonObject in ClassFactory implementations. Handles full deserialization including complex types, cycles, and references.
      Parameters:
      jsonObj - the JsonObject (typically passed to ClassFactory.newInstance)
      fieldName - the field name to read
      type - the target type to convert to
      Returns:
      the fully deserialized object, or null if not present
    • readArray

      public <T> T[] readArray(JsonObject jsonObj, String fieldName, Class<T[]> arrayType)
      Convenience method for reading a typed array field from a JsonObject in ClassFactory implementations. Handles full deserialization including complex types, cycles, and references.
      Parameters:
      jsonObj - the JsonObject (typically passed to ClassFactory.newInstance)
      fieldName - the field name to read
      arrayType - the array type (e.g., String[].class, MyObject[].class)
      Returns:
      the fully deserialized array, or null if not present
    • readList

      public <T> List<T> readList(JsonObject jsonObj, String fieldName, Class<T> elementType)
      Convenience method for reading a List field from a JsonObject in ClassFactory implementations. Handles full deserialization including complex types, cycles, and references.
      Parameters:
      jsonObj - the JsonObject (typically passed to ClassFactory.newInstance)
      fieldName - the field name to read
      elementType - the element type (e.g., String.class, MyObject.class)
      Returns:
      the fully deserialized List, or null if not present
    • readMap

      public <K, V> Map<K,V> readMap(JsonObject jsonObj, String fieldName, Class<K> keyType, Class<V> valueType)
      Convenience method for reading a Map field from a JsonObject in ClassFactory implementations. Handles full deserialization including complex types, cycles, and references.
      Parameters:
      jsonObj - the JsonObject (typically passed to ClassFactory.newInstance)
      fieldName - the field name to read
      keyType - the key type (e.g., String.class)
      valueType - the value type (e.g., Object.class, MyObject.class)
      Returns:
      the fully deserialized Map, or null if not present
    • toJavaObjects

      public <T> T toJavaObjects(JsonObject rootObj, Type rootType)

      Convert a Parsed JsonObject to a Fully Resolved Java Object

      This method converts a root-level JsonObject—a Map-of-Maps representation of parsed JSON—into an actual Java object instance. The JsonObject is typically produced by a prior call to JsonIo.toMaps(String) or JsonIo.toMaps(InputStream). The conversion process uses the provided root parameter, a Type that represents the expected root type (including any generic type parameters). Although the full type information is preserved for resolution, the JsonObject's legacy hintType field (which remains a Class<?>) is set using the raw class extracted from the provided type.

      The resolution process works as follows:

      1. Reference Resolution: If the JsonObject is a reference, it is resolved using the internal reference map. If a referenced object is found, it is returned immediately.
      2. Already Converted Check: If the JsonObject has already been fully converted (i.e. its isFinished flag is set), then its target (the converted Java object) is returned.
      3. Instance Creation and Traversal: Otherwise, the method sets the hint type on the JsonObject (using the raw class extracted from the provided full Type), creates a new instance (if necessary), and then traverses the object graph to resolve nested references and perform conversions.

      Parameters

      • rootObj - The root JsonObject (a Map-of-Maps) representing the parsed JSON data.
      • root - A Type representing the expected Java type (including full generic details) for the resulting object. If null, type inference defaults to a generic Map representation.

      Return Value

      Returns a Java object that represents the fully resolved version of the JSON data. Depending on the JSON structure and the provided type hint, the result may be a user-defined DTO, a collection, an array, or a primitive value.

      Type Parameters:
      T - the type of the resulting Java object.
      Parameters:
      rootObj - the root JsonObject representing parsed JSON data.
      rootType - a Type representing the expected Java type (with full generic details) for the root object; may be null.
      Returns:
      a fully resolved Java object representing the JSON data.
    • traverseJsonObject

      public <T> T traverseJsonObject(JsonObject root)
      Walk a JsonObject (Map of String keys to values) and return the Java object equivalent filled in as good 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).
    • traverseSpecificType

      protected void traverseSpecificType(JsonObject jsonObj)
    • traverseObject

      protected void traverseObject(JsonObject jsonObj)
    • getSealedSupplier

      public SealedSupplier getSealedSupplier()
    • push

      public void push(JsonObject jsonObject)
      Push a JsonObject on the work stack that has not yet had it's fields move over to it's Java peer (.target)
      Parameters:
      jsonObject - JsonObject that supplies the source values for the Java peer (target)
    • traverseFields

      public abstract void traverseFields(JsonObject jsonObj)
    • readWithFactoryIfExists

      protected abstract Object readWithFactoryIfExists(Object o, Type compType)
    • traverseCollection

      protected abstract void traverseCollection(JsonObject jsonObj)
    • traverseArray

      protected abstract void traverseArray(JsonObject jsonObj)
    • addUnresolvedReference

      protected void addUnresolvedReference(com.cedarsoftware.io.Resolver.UnresolvedReference ref)
      Security-aware method to add unresolved references with size limits
    • addMissingField

      protected void addMissingField(Resolver.Missingfields field)
      Security-aware method to add missing fields with size limits
    • cleanup

      protected void cleanup()
    • traverseMap

      protected void traverseMap(JsonObject jsonObj)
      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:
      jsonObj - a Map-of-Map representation of the JSON input stream.
    • valueToTarget

      public boolean valueToTarget(JsonObject jsonObject)
    • setArrayElement

      protected void setArrayElement(Object array, int index, Object element)
    • resolveArray

      protected abstract Object resolveArray(Type suggestedType, List<Object> list)