Class JsonIo.JavaStreamBuilder
- Enclosing class:
JsonIo
This builder completes the JSON parsing process started by JsonIo.toJava(InputStream, ReadOptions)
.
It provides methods to specify the target type for the deserialization.
-
Method Summary
Modifier and TypeMethodDescription<T> T
Completes the JSON parsing by specifying a target class.<T> T
asType
(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 from the input stream 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.class
orList.class
(which will use appropriate implementations)
For generic types like
List<Person>
, use theasType(TypeHolder)
method instead.Examples:
// Read from a file try (FileInputStream fis = new FileInputStream("data.json")) { Person person = JsonIo.toJava(fis, readOptions).asClass(Person.class); }
- 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:
// Read from a network stream List<Person> people = JsonIo.toJava(inputStream, readOptions) .asType(new TypeHolder<List<Person>>(){});
- 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
-