Class JsonIo.JavaObjectBuilder
- Enclosing class:
JsonIo
This builder completes the conversion process started by JsonIo.toJava(JsonObject, ReadOptions)
.
It provides methods to specify the target type for the conversion from the intermediate
JsonObject representation to fully resolved Java objects.
-
Method Summary
Modifier and TypeMethodDescription<T> T
Completes the conversion by specifying a target class.<T> T
asType
(TypeHolder<T> typeHolder) Completes the conversion by specifying a generic type.
-
Method Details
-
asClass
Completes the conversion by specifying a target class.This method converts the JsonObject (Map representation) into an instance of the specified class. This is useful when you have a JsonObject obtained from a previous parsing step (using
ReadOptions.returnAsJsonObjects()
) and want to convert it to a concrete Java object.This method is suitable for converting to:
- Concrete Java classes (e.g.,
Person.class
) - Primitive wrapper classes (e.g.,
Integer.class
) - Interface types like
Map.class
orList.class
(which will use appropriate implementations)
For generic types like
List<Person>
, use theasType(TypeHolder)
method instead.Example:
// First parse JSON to a Map representation ReadOptions mapOptions = new ReadOptionsBuilder() .returnAsJsonObjects() .build(); Map<String, Object> jsonMap = JsonIo.toJava(jsonString, mapOptions).asClass(Map.class); // Modify the map jsonMap.put("age", 31); // Then convert to a Java object Person person = JsonIo.toJava(jsonMap, readOptions).asClass(Person.class);
- Type Parameters:
T
- the type to convert the JsonObject to- Parameters:
clazz
- the target class; if null, the type will be inferred from the JsonObject- Returns:
- an instance of the specified class populated from the JsonObject
- Concrete Java classes (e.g.,
-
asType
Completes the conversion 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 conversion.
Example:
// First parse JSON to a Map representation ReadOptions mapOptions = new ReadOptionsBuilder() .returnAsJsonObjects() .build(); Map<String, Object> jsonMap = JsonIo.toJava(jsonString, mapOptions).asClass(Map.class); // Then convert to a generic collection List<Person> people = JsonIo.toJava(jsonMap, readOptions) .asType(new TypeHolder<List<Person>>(){});
- Type Parameters:
T
- the type to convert the JsonObject to- Parameters:
typeHolder
- a TypeHolder instance capturing the full generic type. It can be null, and JsonIo will do it's best to infer type's/classes, though we recommend passing a Type.- Returns:
- an object of the specified type populated from the JsonObject
-