Interface JsonMapper
This supports the basic Java types of String, Boolean, Integer, Long, Double and Maps and List of these.
If avaje-jsonb is available then you will use that and NOT use this JsonMapper at all. This JsonMapper is intended to be used by code that ONLY wants to depend on avaje-json-core and process the basic Java types or provide its own JsonAdapters.
For full support with more types and binding to custom types use avaje-jsonb instead.
Example
static final JsonMapper jsonMapper = JsonMapper.builder().build();
Map<String, Long> map = new LinkedHashMap<>();
map.put("one", 45L);
map.put("two", 93L);
String asJson = jsonMapper.toJson(map);
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
Build the JsonNodeMapper.static interface
Reading and writing with all options such and InputStream, Reader etc. -
Method Summary
Modifier and TypeMethodDescriptionstatic JsonMapper.Builder
builder()
Create a new builder for JsonMapper.default JsonExtract
Return the map wrapped via JsonExtract to make extracting values easier.fromJson
(JsonReader jsonReader) Read the object from JSON.Read the object from JSON string.fromJsonArray
(JsonReader jsonReader) Read a List from JSON ARRAY.fromJsonArray
(String json) Read a List from JSON ARRAY string.fromJsonObject
(JsonReader jsonReader) Read a Map from JSON OBJECT.fromJsonObject
(String json) Read a Map from JSON OBJECT string.list()
Return a mapper for json ARRAY content with more reading/writing options.map()
Return a mapper for json OBJECT content with more reading/writing options.object()
Return a mapper for any json content.properties
(String... names) Return the property names as PropertyNames.Write the object to JSON string.void
toJson
(Object object, JsonWriter jsonWriter) Write the object to JsonWriter.<T> JsonMapper.Type
<T> type
(JsonAdapter<T> customAdapter) Return a Type specific mapper for the given JsonAdapter.<T> JsonMapper.Type
<T> type
(Function<JsonMapper, JsonAdapter<T>> adapterFunction) Return a Type specific mapper using a function that creates a JsonAdapter.
-
Method Details
-
builder
Create a new builder for JsonMapper. -
object
Return a mapper for any json content. -
map
Return a mapper for json OBJECT content with more reading/writing options. -
list
Return a mapper for json ARRAY content with more reading/writing options. -
toJson
Write the object to JSON string.For options to write json content to OutputStream, Writer etc use
JsonMapper.Type
.var list = List.of(42, "hello"); var asJson = mapper.toJson(list);
-
toJson
Write the object to JsonWriter.For options to write json content to OutputStream, Writer etc use
JsonMapper.Type
. -
fromJson
Read the object from JSON string. -
fromJson
Read the object from JSON. -
fromJsonObject
Read a Map from JSON OBJECT string.Use
map()
for more reading options. -
fromJsonObject
Read a Map from JSON OBJECT.Use
map()
for more reading options. -
fromJsonArray
Read a List from JSON ARRAY string.Use
list()
for more reading options. -
fromJsonArray
Read a List from JSON ARRAY.Use
list()
for more reading options. -
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.
-
type
Return a Type specific mapper for the given JsonAdapter.- Type Parameters:
T
- The type of the class to map to/from json.- Parameters:
customAdapter
- The custom adapter to use.- Returns:
- The Type specific mapper.
-
type
Return a Type specific mapper using a function that creates a JsonAdapter.Often the adapterFunction is the constructor of the custom JsonAdapter where the constructor takes JsonMapper as the only argument.
- Type Parameters:
T
- The type of the class to map to/from json.- Parameters:
adapterFunction
- The function that creates a JsonAdapter.- Returns:
- The Type specific mapper.
-
extract
Return the map wrapped via JsonExtract to make extracting values easier.
-