Jackson Project usability utilities. It's designed to add additional features like easy and centralized configuration, builder or static method set. Artifact does not include direct
Jackson Project
. It is up to you to add them into your project.
- Encapsulate all checked exceptions from Jackson with custom runtime exception;
- A central place for configuration;
- A central place for holding
ObjectMapper
instances; - Utility class to make most common operations much more comfortable to use;
- Ability to change
Zone
to saveZonedDateTime
independently of original zone; ByteBuffer
/InputStream
support for objects, lists and maps;- Lazy read support for list from
Writer
; - Read numeric as
Integer
,Long
,BigInteger
orDouble
(but not only asDouble
); - Advanced
Reader
/Writer
support forenum
.
implementation 'ru.oleg-cherednik.jackson:jackson-utils:2.7'
Optional dependencies (e.g. Jackson of version 2.15.3):
implementation 'com.fasterxml.jackson.module:jackson-module-afterburner:2.15.3'
implementation 'com.fasterxml.jackson.module:jackson-module-parameter-names:2.15.3'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.15.3'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.3'
<dependency>
<groupId>ru.oleg-cherednik.jackson</groupId>
<artifactId>jackson-utils</artifactId>
<version>2.7</version>
</dependency>
Optional dependencies (e.g. Jackson of version 2.15.3):
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-afterburner</artifactId>
<version>2.15.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
<version>2.15.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.15.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.15.3</version>
</dependency>
</dependencies>
Note: jackson-utils
does not contain dependency to the specific Jackson Project
version, so you have to add any version additionally
To simplify usage of jackson-utils, there're following classes:
- JacksonUtils - utility class with set of methods to use json transformation;
- EnumId - advanced enum serialization support.
class Data {
int intVal;
String strVal;
}
String json = """
{
"intVal" : 666,
"strVal" : "omen"
}
""";
Data data = JacksonUtils.readValue(json, Data.class);
class Data {
int intVal;
String strVal;
}
String json = """
[
{
"intVal" : 555,
"strVal" : "victory"
},
{
"intVal" : 666,
"strVal" : "omen"
}
]
""";
List<Data> res = JacksonUtils.readList(json, Data.class);
String json = """
{
"victory" : {
"intVal" : 555,
"strVal" : "victory"
},
"omen" : {
"intVal" : 666,
"strVal" : "omen"
}
}
""";
Map<String, Object> map = JacksonUtils.readMap(json);
Note: map
values have either primitive type or Map
or List
.
class Data {
int intVal;
String strVal;
}
String json = """
{
"victory" : {
"intVal" : 555,
"strVal" : "victory"
},
"omen" : {
"intVal" : 666,
"strVal" : "omen"
}
}
""";
Map<String, Data> map = JacksonUtils.readMap(json, Data.class);
class Data {
int intVal;
String strVal;
}
String json = """
{
"1" : {
"intVal" : 555,
"strVal" : "victory"
},
"2" : {
"intVal" : 666,
"strVal" : "omen"
}
}
""";
Map<Integer, Data> map = JacksonUtils.readMap(json, Integer.class, Data.class);
class Data {
int intVal;
String strVal;
}
{
"intVal": 666,
"strVal": "omen"
}
try(InputStream in = ...) {
Data data = JacksonUtils.readValue(in, Data.class);
}
class Data {
int intVal;
String strVal;
}
[
{
"intVal": 555,
"strVal": "victory"
},
{
"intVal": 666,
"strVal": "omen"
}
]
try (InputStream in = ...) {
List<Data> res = JacksonUtils.readList(in, Data.class);
}
class Data {
int intVal;
String strVal;
}
[
{
"intVal": 555,
"strVal": "victory"
},
{
"intVal": 666,
"strVal": "omen"
}
]
try(InputStream in = ...) {
Iterator<Data> it = JacksonUtils.readListLazy(in, Data.class);
while (it.hasNext()) {
Data data = it.next();
}
}
{
"victory": {
"intVal": 555,
"strVal": "victory"
},
"omen": {
"intVal": 666,
"strVal": "omen"
}
}
try (InputStream in = ...) {
Map<String, Object> map = JacksonUtils.readMap(in);
}
Note: map
values have either primitive type or Map
or List
.
class Data {
int intVal;
String strVal;
}
{
"victory": {
"intVal": 555,
"strVal": "victory"
},
"omen": {
"intVal": 666,
"strVal": "omen"
}
}
try (InputStream in = ...) {
Map<String, Object> map = JacksonUtils.readMap(in, Data.class);
}
class Data {
int intVal;
String strVal;
}
{
"1": {
"intVal": 555,
"strVal": "victory"
},
"2": {
"intVal": 666,
"strVal": "omen"
}
}
try (InputStream in = ...) {
Map<Integer, Data> map = JacksonUtils.readMap(in, Integer.class, Data.class);
}