- All Superinterfaces:
JsonView<T>
JsonType provides the main API used to read and write json.
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);
Moshi note: JsonType does not exist in Moshi and has been added to provide a slightly nicer API to use than JsonAdapter.
-
Method Summary
Modifier and TypeMethodDescriptionfromJson
(byte[] content) Read the return the value from the json content.fromJson
(JsonReader reader) Read the return the value from the reader.fromJson
(InputStream inputStream) Read the return the value from the inputStream.Read the return the value from the reader.Read the return the value from the json content.fromObject
(Object value) Convert from 'object form' expectingMap<String,Object>
for 'json object' and expectingCollection<?>
for 'json array'.list()
Return the list type for this JsonType.map()
Return the map with this type as the value type and string keys.optional()
Return the Optional type for this JsonType.set()
Return the set type for this JsonType.stream()
Prefer use ofstream(JsonReader)
rather than using this stream type directly.stream
(JsonReader reader) Return as a Stream that will read the content as the stream is processed.Build and return the view given the DSL that specifies the properties to include.Methods inherited from interface io.avaje.jsonb.JsonView
toJson, toJson, toJson, toJson, toJson, toJsonBytes, toJsonPretty
-
Method Details
-
view
Build and return the view given the DSL that specifies the properties to include.Examples of json view DSL:
// only include the id and name properties (id, name) // include billAddress which is a nested type (id, name, billingAddress(street, suburb)) // include billAddress with all it's properties (id, name, billingAddress(*)) (id, name, billingAddress(street, suburb), shippingAddress(*), contacts(email,lastName, firstName))
-
list
Return the list type for this JsonType. -
stream
Prefer use ofstream(JsonReader)
rather than using this stream type directly.Generally, we should not use this type directly but instead create a
JsonReader
and usestream(JsonReader)
instead. Then we just use a try-with-resources on the JsonReader and can additionally specifyJsonReader.streamArray(boolean)
option.When using this Stream type directly, use a try-with-resources block with the Stream to ensure that any underlying resources are closed.
JsonType<Stream<MyBean>> type = jsonb.type(MyBean.class).stream(); try (Stream<MyBean> asStream = type.fromJson(content)) { // use the stream ... }
- Returns:
- The stream type for this base JsonType.
- See Also:
-
set
Return the set type for this JsonType. -
map
Return the map with this type as the value type and string keys. -
optional
Return the Optional type for this JsonType. -
fromJson
Read the return the value from the reader. -
fromJson
Read the return the value from the json content. -
fromJson
Read the return the value from the json content. -
fromJson
Read the return the value from the reader. -
fromJson
Read the return the value from the inputStream. -
fromObject
Convert from 'object form' expectingMap<String,Object>
for 'json object' and expectingCollection<?>
for 'json array'.- Parameters:
value
- The json value that will be converted into T.- Returns:
- The value converted from 'object form'.
-
stream
Return as a Stream that will read the content as the stream is processed.Use a try-with-resources block with the JsonReader to ensure the underlying resources are closed.
JsonType<MyBean> type = jsonb.type(MyBean.class); try (JsonReader reader = jsonb.reader(content)) { Stream<MyBean> asStream = type.stream(reader); ... }
When using Jackson
When using Jackson-core as the underlying parser we should explicitly state that the content is either an ARRAY (with '[' and ']' tokens or not (x-json-stream new line delimited, there are no '[' and ']' tokens).
When using the builtin avaje-jsonb parser, it automatically detects and handles both cases (with or without the '[' and ']' tokens). With the Jackson-core parser we need to explicitly state if we are processing
JsonType<MyBean> type = jsonb.type(MyBean.class); try (JsonReader reader = jsonb.reader(content)) { // when the content contains the ARRAY '[', ']' tokens set streamArray(true) Stream<MyBean> asStream = type.stream(reader.streamArray(true)); ... }
-