jackson-utils

Some utils for Jackson Project

License

License

Categories

Categories

Jackson Data JSON
GroupId

GroupId

ru.oleg-cherednik.utils
ArtifactId

ArtifactId

jackson-utils
Last Version

Last Version

2.12.0.3
Release Date

Release Date

Type

Type

zip
Description

Description

jackson-utils
Some utils for Jackson Project
Project URL

Project URL

https://github.com/oleg-cherednik/jackson-utils
Source Code Management

Source Code Management

https://github.com/oleg-cherednik/jackson-utils

Download jackson-utils

Dependencies

runtime (4)

Group / Artifact Type Version
com.fasterxml.jackson.module : jackson-module-afterburner jar 2.12.0
com.fasterxml.jackson.module : jackson-module-parameter-names jar 2.12.0
com.fasterxml.jackson.datatype : jackson-datatype-jdk8 jar 2.12.0
com.fasterxml.jackson.datatype : jackson-datatype-jsr310 jar 2.12.0

test (3)

Group / Artifact Type Version
org.testng : testng jar 7.3.0
org.assertj : assertj-core jar 3.11.1
commons-io : commons-io jar 2.5

Project Modules

There are no modules declared in this project.

Maven Central javadoc java1.8 travis-ci circle-ci License codecov Known Vulnerabilities Codacy Badge

JacksonUtils

a java tool to make working with Jackson Project more comfortable

Features

  • Encapsulate all checked exceptions from Jackson with custom runtime exception;
  • A centralized configuration of ObjectMapper;
  • A central place of settings and all ObjectMapper instances;
  • Utility class to make most common operations much more comfortable to use;
  • Ability to change Zone to save ZonedDateTime independently of original zone;
  • InputStream support for objects, lists and maps;
  • Lazy read support for list from InputStream.

Gradle

compile 'ru.oleg-cherednik.utils:jackson-utils:2.12.0.4'

Maven

<dependency>
    <groupId>ru.oleg-cherednik.utils</groupId>
    <artifactId>jackson-utils</artifactId>
    <version>2.12.0.4</version>
</dependency>

In the version, first 3 places are the version of Jackson that is used in this utils. The last section is the jackson-utils version. This number is unique.

Usage

To simplify usage of jackson-utils, there're following classes:

  • JacksonUtils - utility class with set of method to use json transformation;

JacksonUtils class

Read json from String

String to a custom object type (but not a collection)
class Data {
    int intVal;
    String strVal;
}
String json = """
              {
                  "intVal" : 666,
                  "strVal" : "omen"
              }
              """;
Data data = JacksonUtils.readValue(json, Data.class);
String to a list of custom object type
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 to a map of custom object type
Map with String keys and Map or primitive types as values
String json = """
              {
                  "victory" : {
                      "intVal" : 555,
                      "strVal" : "victory"
                  },
                  "omen" : {
                      "intVal" : 666,
                      "strVal" : "omen"
                  }
              }
              """;
Map<String, ?> map = JacksonUtils.readMap(json);

Note: map values have either primitive type or Map or List.

String to a map with String keys and given type as value
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);
String to a map with Integer keys and given type as value
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);

Read json from InputStream

InputStream to a custom object type (but not a collection)
class Data {
    int intVal;
    String strVal;
}
{
    "intVal" : 666,
    "strVal" : "omen"
}
try (InputStream in = ...) {
    Data data = JacksonUtils.readValue(in, Data.class);
}
InputStream to a list of custom object type
Read eager
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);
}
Read lazy
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();
    }
}
InputStream to a map of custom object type
InputStream to a map with String keys and Map or primitive types as values
{
    "victory" : {
        "intVal" : 555,
        "strVal" : "victory"
    },
    "omen" : {
        "intVal" : 666,
        "strVal" : "omen"
    }
}
try (InputStream in = ...) {
    Map<String, ?> map = JacksonUtils.readMap(in);
}

Note: map values have either primitive type or Map or List.

InputStream to a map with String keys and given type as value
class Data {
    int intVal;
    String strVal;
}
{
    "victory" : {
        "intVal" : 555,
        "strVal" : "victory"
    },
    "omen" : {
        "intVal" : 666,
        "strVal" : "omen"
    }
}
try (InputStream in = ...) {
    Map<String, ?> map = JacksonUtils.readMap(in, Data.class);
}
Map with Integer keys and given type as value
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);
}
Links

Versions

Version
2.12.0.3
2.12.0.2