Giter Club home page Giter Club logo

json-jackson-impl's Introduction

Maven Central javadoc java8 License

github-ci vulnerabilities license-scan quality coverage

develop

github-ci quality coverage

jackson-utils

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.

Features

  • 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 save ZonedDateTime 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 or Double (but not only as Double);
  • Advanced Reader/Writer support for enum.

Gradle

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'

Maven

<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

Usage

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.

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, Object> 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, Object> 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, Object> 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

json-jackson-impl's People

Contributors

oleg-cherednik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

json-jackson-impl's Issues

Add PrettyPrint option

This time json write use only single string format. We have to implement option to use pretty print format.

How to use ObjectMapper to read a JSON file having the content map of maps?

I have a JSON file that has the following structure:

Map<Enum1, Map<Enum2, Map<Enum3, Map<Enum4, Map<Enum5, Map<String, String>>>>>>

How do I read the content of the file into the above map structure?

I have tried the following, but Eclipse gave an error The type Map is not generic; it cannot be parameterized with arguments <String, String>:

ObjectMapper mapper = new ObjectMapper();

TypeReference<Map<Enum1, Map<Enum2, Map<Enum3, Map<Enum4, Map<Enum5, Map<String, String>>>>>>>
        typeRef =
        new TypeReference<Map<Enum1, Map<Enum2, Map<Enum3, Map<Enum4, Map<Enum5, Map<String, String>>>>>>>() {
        };

mapper.readValue(file, typeRef);

The content of the file looks like this:

{
  "enum1": {
    "enum2": {
      "enum3": {
        "enum4-1": {
          "enum5-1": {
            "example1": "example1 string",
            "example2": "example2 string"
          },
          "enum5-2": {
            "example1": "example1 string",
            "example2": "example2 string"
          }
        },
        "enum4-2": {
          "enum5-1": {
            "example1": "example1 string",
            "example2": "example2 string"
          },
          "enum5-2": {
            "example1": "example1 string",
            "example2": "example2 string"
          }
        }
        ....
      }
      .....
    }
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.