Class JsonIo.JavaStringBuilder
- Enclosing class:
- JsonIo
This builder completes the JSON parsing process started by JsonIo.toJava(String, ReadOptions).
It provides methods to specify the target type for the deserialization.
-
Method Summary
Modifier and TypeMethodDescription<T> TCompletes the JSON parsing by specifying a target class.<T> TasType(TypeHolder<T> typeHolder) Completes the JSON parsing by specifying a generic type.
-
Method Details
-
asClass
Completes the JSON parsing by specifying a target class.This method finishes the conversion process by parsing the JSON string into an instance of the specified class. The method is suitable for converting to:
- Concrete Java classes (e.g.,
Person.class) - Primitive wrapper classes (e.g.,
Integer.class) - Interface types like
Map.classorList.class(which will use appropriate implementations)
For generic types like
List<Person>, use theasType(TypeHolder)method instead.Examples:
// Convert to a specific class Person person = JsonIo.toJava(jsonString, readOptions).asClass(Person.class); // Convert to a Map Map<String, Object> map = JsonIo.toJava(jsonString, readOptions).asClass(Map.class); // Convert to a List (note: without generic type information) List list = JsonIo.toJava(jsonString, readOptions).asClass(List.class);
The behavior is affected by the ReadOptions:
- With
returnAsJavaObjects()(default), this method attempts to create fully instantiated objects of the specified class. - With
returnAsJsonObjects(), this method returns an intermediate representation, typically Map objects, that can be manipulated before further processing.
- Type Parameters:
T- the type to convert the JSON to- Parameters:
clazz- the target class; if null, the type will be inferred from the JSON- Returns:
- an instance of the specified class populated from the JSON
- Throws:
JsonIoException- if an error occurs during parsing or conversion
- Concrete Java classes (e.g.,
-
asType
Completes the JSON parsing by specifying a generic type.This method is particularly useful for handling generic types like
List<Person>orMap<String, List<Integer>>, where the full type information cannot be expressed with a simple Class object.The TypeHolder captures the complete generic type information at compile time, allowing JsonIo to properly handle the generics during deserialization.
Examples:
// Convert to a List of Person objects List<Person> people = JsonIo.toJava(jsonString, readOptions) .asType(new TypeHolder<List<Person>>(){}); // Convert to a Map with generic parameters Map<String, List<Integer>> map = JsonIo.toJava(jsonString, readOptions) .asType(new TypeHolder<Map<String, List<Integer>>>(){});- Type Parameters:
T- the type to convert the JSON to- Parameters:
typeHolder- a TypeHolder instance capturing the full generic type- Returns:
- an object of the specified type populated from the JSON
- Throws:
JsonIoException- if an error occurs during parsing or conversion
-