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()
    • 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.toObjects(String) or JsonIo.toObjects(InputStream) when using the ReadOptions.returnAsJsonObjects() setting. 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)
    • 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)