Interface Jsonb
Initialise with defaults
Jsonb jsonb = Jsonb.newBuilder().build();
Initialise with some configuration
Jsonb jsonb = Jsonb.newBuilder()
.serializeNulls(true)
.serializeEmpty(true)
.failOnUnknown(true)
.build();
fromJson
Jsonb jsonb = Jsonb.newBuilder().build();
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 ClassesModifier and TypeInterfaceDescriptionstatic interfaceFunction to build a JsonAdapter that needs Jsonb.static interfaceBuild the Jsonb instance adding JsonAdapter, Factory or AdapterBuilder.static interfaceComponents register JsonAdapters Jsonb.Builder -
Method Summary
Modifier and TypeMethodDescription<T> JsonAdapter<T>Return the JsonAdapter used to read and write json for the given class.<T> JsonAdapter<T>Return the JsonAdapter used to read and write json for the given type.static Jsonb.BuilderCreate 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.Return the JsonReader used to read the json content from the given reader.Return the JsonReader used to read the given json content.<T> JsonType<T>Return the JsonType used to read and write json for the given class.<T> JsonType<T>Return the JsonType used to read and write json for the given type.<T> JsonType<T>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.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
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.classand writingtoJson()then the underlying JsonAdapter is determined dynamically based on the type of the object value passed in.When using
Object.classand readingfromJson()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
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.classand writingtoJson()then the underlying JsonAdapter is determined dynamically based on the type of the object value passed in.When using
Object.classand readingfromJson()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
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
Return the JsonAdapter used to read and write json for the given class. -
adapter
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
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.
-