Package org.loxlylabs.nestedtext
Class DeserializationContext
java.lang.Object
org.loxlylabs.nestedtext.DeserializationContext
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 aCollection(e.g., ArrayList) or an array. - A
Stringis parsed into a simple type (e.g., int, boolean, BigDecimal, Enum).
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionDeserializationContext(Map<Class<?>, Deserializer<?>> customDeserializers, Map<Class<?>, TypeMappingRules> typeMappingRules) Creates a new context with the given custom deserializers.DeserializationContext(Map<Class<?>, TypeMappingRules> typeMappingRules) Creates a new context with no custom deserializers. -
Method Summary
Modifier and TypeMethodDescription<T> TConverts a source object graph into an instance of a specified non-generic target class.<T> Tconvert(Object source, TypeReference<T> typeRef) Converts a source object graph into an instance of a specified generic target class.
-
Constructor Details
-
DeserializationContext
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
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.classorString.class. For types with generics (e.g.,List<Integer>), useconvert(Object, TypeReference)instead.- Type Parameters:
T- The generic type of the target class.- Parameters:
source- The raw source object (Map, List, or String).targetClass- TheClassof 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
Converts a source object graph into an instance of a specified generic target class.
Or simply:// To convert to a List<Integer>: TypeReference<List<Integer>> typeRef = new TypeReference<List<Integer>>() {}; List<Integer> numbers = context.convert(sourceList, typeRef);
For simple, non-generic types, the overloadList<Integer> numbers = context.convert(sourceList, new TypeReference<>() {});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). Anullsource yields anulloutput.typeRef- ATypeReferencethat 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.
-