Interface Jsonb


public interface Jsonb
Provides access to json adapters by type.

Initialise with defaults


   Jsonb jsonb = Jsonb.newBuilder().build();
 

Initialise with some configuration


   Jsonb jsonb = Jsonb.newBuilder()
     .serializeNulls(true)
     .serializeEmpty(true)
     .failOnUnknown(true)
     .build();
 

fromJson

Read json content from: String, byte[], Reader, InputStream, JsonReader



  JsonType<Customer> customerType = jsonb.type(Customer.class);

  Customer customer = customerType.fromJson(content);

 

toJson

Write json content to: String, byte[], Writer, OutputStream, JsonWriter



  JsonType<Customer> customerType = jsonb.type(Customer.class);

  String asJson = customerType.toJson(customer);

 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Function to build a JsonAdapter that needs Jsonb.
    static interface 
    Build the Jsonb instance adding JsonAdapter, Factory or AdapterBuilder.
    static interface 
    Components register JsonAdapters Jsonb.Builder
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> JsonAdapter<T>
    adapter(Class<T> cls)
    Return the JsonAdapter used to read and write json for the given class.
    <T> JsonAdapter<T>
    adapter(Type type)
    Return the JsonAdapter used to read and write json for the given type.
    Create and return a new Jsonb.Builder to configure before building the Jsonb instance.
    properties(String... names)
    Return the property names as PropertyNames.
    reader(byte[] jsonBytes)
    Return the JsonReader used to read the given json content in bytes.
    reader(InputStream inputStream)
    Return the JsonReader used to read the json content from the given inputStream.
    reader(Reader reader)
    Return the JsonReader used to read the json content from the given reader.
    reader(String json)
    Return the JsonReader used to read the given json content.
    <T> JsonType<T>
    type(Class<T> cls)
    Return the JsonType used to read and write json for the given class.
    <T> JsonType<T>
    type(Type type)
    Return the JsonType used to read and write json for the given type.
    <T> JsonType<T>
    typeOf(Object value)
    Return the JsonType for the given value using the values class.
    writer(OutputStream outputStream)
    Return the JsonWriter used to write json to the given outputStream.
    writer(Writer writer)
    Return the JsonWriter used to write json to the given writer.
  • Method Details

    • newBuilder

      Create and return a new Jsonb.Builder to configure before building the Jsonb instance.

      We can register JsonAdapter's to use for specific types before building and returning the Jsonb instance to use.

      Note that JsonAdapter's that are generated are automatically registered via service loading so there is no need to explicitly register those generated JsonAdapters.

      
      
         Jsonb jsonb = Jsonb.newBuilder()
           .serializeNulls(true)
           .serializeEmpty(true)
           .failOnUnknown(true)
           .build();
      
       
    • type

      <T> JsonType<T> type(Class<T> cls)
      Return the JsonType used to read and write json for the given class.

      Using Object.class

      We can use type(Object.class) when we don't know the specific type that is being written toJson or read fromJson.

      When using Object.class and writing toJson() then the underlying JsonAdapter is determined dynamically based on the type of the object value passed in.

      When using Object.class and reading fromJson() then the java types used in the result are determined dynamically based on the JSON types being read and the resulting java types are ArrayList, LinkedHashMap, String, boolean, and double.

    • type

      <T> JsonType<T> type(Type type)
      Return the JsonType used to read and write json for the given type.

      Using Object.class

      We can use type(Object.class) when we don't know the specific type that is being written toJson or read fromJson.

      When using Object.class and writing toJson() then the underlying JsonAdapter is determined dynamically based on the type of the object value passed in.

      When using Object.class and reading fromJson() then the java types used in the result are determined dynamically based on the JSON types being read and the resulting java types are ArrayList, LinkedHashMap, String, boolean, and double.

    • typeOf

      <T> JsonType<T> typeOf(Object value)
      Return the JsonType for the given value using the values class.

      This is a helper method that supports returning an inferred generic type.

      Type Parameters:
      T - The inferred generic parameter type
      Parameters:
      value - The value of the given type
      Returns:
      JsonType for the given value
    • adapter

      <T> JsonAdapter<T> adapter(Class<T> cls)
      Return the JsonAdapter used to read and write json for the given class.
    • adapter

      <T> JsonAdapter<T> adapter(Type type)
      Return the JsonAdapter used to read and write json for the given type.
    • reader

      Return the JsonReader used to read the given json content.
    • reader

      JsonReader reader(byte[] jsonBytes)
      Return the JsonReader used to read the given json content in bytes.
    • reader

      Return the JsonReader used to read the json content from the given reader.
    • reader

      Return the JsonReader used to read the json content from the given inputStream.
    • writer

      Return the JsonWriter used to write json to the given writer.
    • writer

      Return the JsonWriter used to write json to the given outputStream.
    • properties

      Return the property names as PropertyNames.

      Provides the option of optimising the writing of json for property names by having them already escaped and encoded rather than as plain strings.