Class JsonIo.JavaStreamBuilder

java.lang.Object
com.cedarsoftware.io.JsonIo.JavaStreamBuilder
Enclosing class:
JsonIo

public static final class JsonIo.JavaStreamBuilder extends Object
Builder for converting JSON from an InputStream to Java objects.

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 Details

    • asClass

      public <T> T asClass(Class<T> clazz)
      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 or List.class (which will use appropriate implementations)

      For generic types like List<Person>, use the asType(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
    • asType

      public <T> T asType(TypeHolder<T> typeHolder)
      Completes the JSON parsing by specifying a generic type.

      This method is particularly useful for handling generic types like List<Person> or Map<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