- Getting Started
- Features
- Glossary
- Requirements
- Usage
- Json - utility class with set of methods to use json transformation;
- Read json from
String
- read json fromString
; - Read json from
ByteBuffer
- read json fromByteBuffer
;- Read json from
ByteBuffer
lazy - read json fromByteBuffer
lazy;
- Read json from
- Read json from
InputStream
- read json fromInputStream
;- Read json from
InputStream
lazy - read json fromInputStream
lazy;
- Read json from
- Read json from
Reader
- read json fromReader
;- Read json from
Reader
lazy - read json fromReader
lazy;
- Read json from
- Write json - write json to
String
,OutputStream
orWriter
; - Get default decorators - get current instances of
JsonReader
andJsonWriter
; - Create copy of default decorators - create a new
instance of
JsonReader
andJsonWriter
with default setting; - Create custom decorators - create an instance of
JsonReader
andJsonWriter
with custom setting. - Convert object - convert given
Object
to another object.
- Read json from
- JsonHelper - utility class with set of methods to update actual settings;
- EnumId - advanced enum serialization support.
- Json - utility class with set of methods to use json transformation;
- Custom
json-api
implementation - Links
E.g. you would like to use jackson 2.16.1 as a json framework in your application. In this case, you have several options:
- Add jackson dependencies and use it directly;
- Along with jackson dependencies add jackson-json-api and use Jackson via json-api.
If you choose 2nd option, you should add json-api implementation for jackson (which is json-jackson-impl) along with existed jackson dependencies, because json-api implementation does not contain concrete version of the json framework. The version should be additionally specified. I.e. version of json-api implementation does not depend on the version of the json framework.
implementation 'ru.oleg-cherednik.json:json-jackson-impl:3.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.16.0'
<dependencies>
<dependency>
<groupId>ru.oleg-cherednik.json</groupId>
<artifactId>json-jackson-impl</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>ru.oleg-cherednik.json</groupId>
<artifactId>json-jackson-impl</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
- Single file API for all json actions;
- Give an easy way to provide custom engine implementation;
- It's free of any engine's specific code;
- It's fully open-source and does not depend on any limited licenses.
json framework
is a framework for working with json files like jackson, gson, json-simple, etc. Usually we use json framework in the application directly by adding required dependencies. In general all these json frameworks have its own API and style of coding.json engine
is an abstraction above all json framework. The main idea is to provide a unified API over all json frameworks. I.e. using this unified API (i.e. json engine), the client is not able to use some specific logic of concrete json framework, but the most common use-cases are available.json decorator
is a decorator over json engine. There are read and write decorators that contain the complete set of not static methods for json manipulation. You can use default decorators or create custom once with required settings. The decorator is used to work with json engine with given settings.JSON-API
is an abstraction over various json decorators. It provides a simple way to do the most common use-cases of json manipulations. Moreover, it provides the way of single point configuration and exception handling. Using this json-api you are able to not depend on the specific json framework directly and use any of them via given json engine.json-api-impl
is a concreted implementation of json-api for given json framework. E.g. an implementation of json-api for jackson called json-jackson-impl and contains instance of json engine calledJacksonEngine
.
json-api provides set of classes to work with json. You should use only these classes for any json manipulations.
String
to a custom object type (but not a collection)
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
{
"intVal" : 666,
"strVal" : "omen"
}
""";
Data data = Json.readValue(json, Data.class);
}
}
String
to a List
of Object
type
class Data {
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
List<Object> res = Json.readList(json);
}
}
String
to a Set
of Object
type
class Data {
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
Set<Object> res = Json.readSet(json);
}
}
String
to a List
of custom object type
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
List<Data> res = Json.readList(json, Data.class);
}
}
String
to a Set
of custom object type
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
Set<Data> res = Json.readSet(json, Data.class);
}
}
String
to a List
of Map
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
List<Map<String, Object>> res = Json.readListOfMap(json);
}
}
String
to a Map
of String
key and Object
value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"one": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"two": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
Map<String, Object> res = Json.readMap(json);
}
}
String
to a Map
of String
key and custom value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"one": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"two": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
Map<String, Book> res = Json.readMap(json, Book.class);
}
}
String
to a Map
of custom key and value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"1": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"2": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
Map<Integer, Book> res = Json.readMap(json, Integer.class, Book.class);
}
}
ByteBuffer
to a custom object type (but not a collection)
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
{
"intVal" : 666,
"strVal" : "omen"
}
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
Data data = Json.readValue(buf, Data.class);
}
}
ByteBuffer
to a List
of Object
type
class Data {
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
List<Object> res = Json.readList(buf);
}
}
ByteBuffer
to a Set
of Object
type
class Data {
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
Set<Object> res = Json.readSet(buf);
}
}
ByteBuffer
to a List
of custom object type
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
List<Data> res = Json.readList(buf, Data.class);
}
}
ByteBuffer
to a Set
of custom object type
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
Set<Data> res = Json.readSet(buf, Data.class);
}
}
ByteBuffer
to a List
of Map
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
List<Map<String, Object>> res = Json.readListOfMap(buf);
}
}
ByteBuffer
to a Map
of String
key and Object
value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"one": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"two": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
Map<String, Object> res = Json.readMap(buf);
}
}
ByteBuffer
to a Map
of String
key and custom value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"one": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"two": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
Map<String, Book> res = Json.readMap(buf, Book.class);
}
}
ByteBuffer
to a Map
of custom key and value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"1": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"2": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
Map<Integer, Book> res = Json.readMap(buf, Integer.class, Book.class);
}
}
ByteBuffer
to an Iterator
of Object
type with lazy reading
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
Iterator<Object> it = Json.readListLazy(buf);
}
}
ByteBuffer
to an Iterator
of custom object type with lazy reading
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
Iterator<Data> it = Json.readListLazy(buf, Data.class);
}
}
ByteBuffer
to an Iterator
of Map
with lazy reading
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
ByteBuffer buf = ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8));
Iterator<Map<String, Object>> it = Json.readListOfMapLazy(buf);
}
}
InputStream
to a custom object type (but not a collection)
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
{
"intVal" : 666,
"strVal" : "omen"
}
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
Data data = Json.readValue(in, Data.class);
}
}
InputStream
to a List
of Object
type
class Data {
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
List<Object> res = Json.readList(in);
}
}
InputStream
to a Set
of Object
type
class Data {
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
Set<Object> res = Json.readSet(in);
}
}
InputStream
to a List
of custom object type
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
List<Data> res = Json.readList(in, Data.class);
}
}
InputStream
to a Set
of custom object type
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
Set<Data> res = Json.readSet(in, Data.class);
}
}
InputStream
to a List
of Map
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
List<Map<String, Object>> res = Json.readListOfMap(in);
}
}
InputStream
to a Map
of String
key and Object
value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"one": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"two": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
Map<String, Object> res = Json.readMap(in);
}
}
InputStream
to a Map
of String
key and custom value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"one": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"two": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
Map<String, Book> res = Json.readMap(in, Book.class);
}
}
InputStream
to a Map
of custom key and value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"1": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"2": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
Map<Integer, Book> res = Json.readMap(in, Integer.class, Book.class);
}
}
InputStream
to an Iterator
of Object
type with lazy reading
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
AutoCloseableIterator<Object> it = Json.readListLazy(in);
}
}
InputStream
to an Iterator
of custom object type with lazy reading
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
AutoCloseableIterator<Data> it = Json.readListLazy(in, Data.class);
}
}
InputStream
to an Iterator
of Map
with lazy reading
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
InputStream in = new ByteArrayInputStream(json.getBytes());
AutoCloseableIterator<Map<String, Object>> it = Json.readListOfMapLazy(in);
}
}
Reader
to a custom object type (but not a collection)
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
{
"intVal" : 666,
"strVal" : "omen"
}
""";
Reader reader = new ByteArrayInputStream(json.getBytes());
Data data = Json.readValue(reader, Data.class);
}
}
Reader
to a List
of Object
type
class Data {
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
Reader reader = new StringReader(json);
List<Object> res = Json.readList(reader);
}
}
Reader
to a Set
of Object
type
class Data {
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
Reader reader = new StringReader(json);
Set<Object> res = Json.readSet(reader);
}
}
Reader
to a List
of custom object type
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
Reader reader = new StringReader(json);
List<Data> res = Json.readList(reader, Data.class);
}
}
Reader
to a Set
of custom object type
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
Reader reader = new StringReader(json);
Set<Data> res = Json.readSet(reader, Data.class);
}
}
Reader
to a List
of Map
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
Reader reader = new StringReader(json);
List<Map<String, Object>> res = Json.readListOfMap(reader);
}
}
Reader
to a Map
of String
key and Object
value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"one": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"two": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
Reader reader = new StringReader(json);
Map<String, Object> res = Json.readMap(reader);
}
}
Reader
to a Map
of String
key and custom value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"one": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"two": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
Reader reader = new StringReader(json);
Map<String, Book> res = Json.readMap(reader, Book.class);
}
}
Reader
to a Map
of custom key and value type
public class Book {
private String title;
private ZonedDateTime date;
private int year;
private List<String> authors;
public static void demo() {
String json = """
{
"1": {
"title": "Thinking in Java",
"date": "2017-07-23T13:57:14.225Z",
"year": 1998,
"authors": [
"Bruce Eckel"
]
},
"2": {
"title": "Ready for a victory",
"date": "2020-07-23T13:57:14.225Z",
"year": 2020,
"authors": [
"Oleg Cherednik"
]
}
}
""";
Reader reader = new StringReader(json);
Map<Integer, Book> res = Json.readMap(reader, Integer.class, Book.class);
}
}
Reader
to an Iterator
of Object
type with lazy reading
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
Reader reader = new StringReader(json);
AutoCloseableIterator<Object> it = Json.readListLazy(reader);
}
}
Reader
to an Iterator
of custom object type with lazy reading
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
Reader reader = new StringReader(json);
AutoCloseableIterator<Data> it = Json.readListLazy(reader, Data.class);
}
}
Reader
to an Iterator
of Map
with lazy reading
class Data {
int intVal;
String strVal;
public static void demo() {
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
Reader reader = new StringReader(json);
AutoCloseableIterator<Map<String, Object>> it = Json.readListOfMapLazy(reader);
}
}
Write to String
class Data {
int intVal;
String strVal;
public static void demo() {
Data data = new Data(666, "omen");
String json = Json.writeValue(data);
}
}
Write to OutputStream
class Data {
int intVal;
String strVal;
public static void demo() {
Data data = new Data(666, "omen");
try (OutputStream out = new ByteArrayOutputStream()) {
Json.writeValue(data, out);
}
}
}
Write to Writer
class Data {
int intVal;
String strVal;
public static void demo() {
Data data = new Data(666, "omen");
try (Writer out = new StringWriter()) {
Json.writeValue(data, out);
}
}
}
Get current default instance of JsonReader
class Data {
public static void demo() {
String json = """
{
"intVal" : 666,
"strVal" : "omen"
}
""";
// using static method
Data data1 = Json.readValue(json, Data.class);
// alternative: using JsonReader instance
JsonReader reader = Json.reader();
Data data2 = reader.readValue(json, Data.class);
}
}
Get current default instance of JsonWriter
class Data {
public static void demo() {
Data data = new Data(666, "omen");
// using static method
String json1 = Json.writeValue(data);
// alternative: using JsonWriter instance
JsonWriter writer = Json.writer();
String json2 = writer.writeValue(data);
}
}
Get current default instance of JsonWriter
with enabled pretty-print
option
class Data {
public static void demo() {
Data data = new Data(666, "omen");
// there is no way for pretty-print using static method
// using JsonWriter instance with pretty-print option
JsonWriter writer = Json.prettyPrint();
String json = writer.writeValue(data);
}
}
Create a new instance of JsonReader
with default settings
class Data {
public static void demo() {
String json = """
{
"intVal" : 666,
"strVal" : "omen"
}
""";
// using static method
Data data1 = Json.readValue(json, Data.class);
// alternative: using new JsonReader instance
JsonReader reader = Json.createReader();
Data data2 = reader.readValue(json, Data.class);
}
}
Create a new instance of JsonWriter
with default settings
class Data {
public static void demo() {
Data data = new Data(666, "omen");
// using static method
String json1 = Json.writeValue(data);
// alternative: using new JsonWriter instance
JsonWriter writer = Json.createWriter();
String json2 = writer.writeValue(data);
}
}
Create a new instance of JsonWriter
with default settings and enabled
pretty-print
option
class Data {
public static void demo() {
Data data = new Data(666, "omen");
// there is no way for pretty-print using static method
// using new JsonWriter instance with pretty-print option
JsonWriter writer = Json.createPrettyPrint();
String json = writer.writeValue(data);
}
}
Create a new instance of JsonReader
with custom settings
class Data {
public static void demo() {
String json = """
{
"intVal" : 666,
"strVal" : "omen"
}
""";
// there is no way for pretty-print using static method
// using new JsonReader instance with custom settings
JsonSettings settings = new JsonSettings();
JsonReader reader = Json.createReader(settings);
Data data = reader.readValue(json, Data.class);
}
}
Create a new instance of JsonWriter
with custom settings
class Data {
public static void demo() {
Data data = new Data(666, "omen");
// there is no way for pretty-print using static method
// using new JsonWriter instance with custom settings
JsonSettings settings = new JsonSettings();
JsonWriter writer = Json.createWriter(settings);
String json = writer.writeValue(data);
}
}
Create a new instance of JsonWriter
with custom settings and enabled
pretty-print
option
class Data {
public static void demo() {
Data data = new Data(666, "omen");
// there is no way for pretty-print using static method
// using new JsonWriter instance with custom settings and pretty-print option
JsonSettings settings = new JsonSettings();
JsonWriter writer = Json.createPrettyPrint(settings);
String json = writer.writeValue(data);
}
}
Convert any Object
instance to Map
class Data {
public static void demo() {
Data data = new Data(666, "omen");
Map<String, Object> map = Json.convertToMap(data);
}
}