Class DeserializationContext

java.lang.Object
org.loxlylabs.nestedtext.DeserializationContext

public class DeserializationContext extends Object
Provides context for a deserialization operation and serves as the main entry point for data binding.

This is the primary class for deserializing a String, List, or Map object structure parsed from Minimal NestedText into a strongly-typed Java object.

The conversion process follows a set of configurable rules:

  • A Map<String, Object> is converted to a Java Record or POJO.
  • A List<Object> is converted to a Collection (e.g., ArrayList) or an array.
  • A String is parsed into a simple type (e.g., int, boolean, BigDecimal, Enum).
See Also:
  • Constructor Details

    • DeserializationContext

      public DeserializationContext(Map<Class<?>,TypeMappingRules> typeMappingRules)
      Creates a new context with no custom deserializers.
      Parameters:
      typeMappingRules - the type specific rules for deserialization
    • DeserializationContext

      public DeserializationContext(Map<Class<?>,Deserializer<?>> customDeserializers, Map<Class<?>,TypeMappingRules> typeMappingRules)
      Creates a new context with the given custom deserializers.
      Parameters:
      customDeserializers - A map of type-to-deserializer mappings to use for conversion.
      typeMappingRules - the type specific rules for deserialization
  • Method Details

    • convert

      public <T> T convert(Object source, Class<T> targetClass)
      Converts a source object graph into an instance of a specified non-generic target class.

      This method is the primary entry point for deserializing into simple, non-generic types like User.class or String.class. For types with generics (e.g., List<Integer>), use convert(Object, TypeReference) instead.

      Type Parameters:
      T - The generic type of the target class.
      Parameters:
      source - The raw source object (Map, List, or String).
      targetClass - The Class of the object to be created, e.g., User.class.
      Returns:
      A new instance of the targetClass, populated with data from the source.
      Throws:
      DeserializationException - if the conversion fails for any reason.
    • convert

      public <T> T convert(Object source, TypeReference<T> typeRef)
      Converts a source object graph into an instance of a specified generic target class.
      
       // To convert to a List<Integer>:
       TypeReference<List<Integer>> typeRef = new TypeReference<List<Integer>>() {};
       List<Integer> numbers = context.convert(sourceList, typeRef);
       
      Or simply:
      
       List<Integer> numbers = context.convert(sourceList, new TypeReference<>() {});
       
      For simple, non-generic types, the overload convert(Object, Class) can be used for convenience.
      Type Parameters:
      T - The generic type of the target class.
      Parameters:
      source - The raw source object (Map, List, or String). A null source yields a null output.
      typeRef - A TypeReference that captures the complete generic type.
      Returns:
      A new instance of the target type, populated with data from the source.
      Throws:
      DeserializationException - if the conversion fails for any reason.