Coder Social home page Coder Social logo

jiangshikun / jackson-utils Goto Github PK

View Code? Open in Web Editor NEW

This project forked from arakelian/jackson-utils

0.0 0.0 0.0 414 KB

Useful utilities when using Jackson for JSON serialization and deserialization

License: Apache License 2.0

Java 100.00%

jackson-utils's Introduction

jackson-utils

version CI

Utilities for reading and writing JSON and XML using Jackson.

Requirements

  • Compatible with Java 8+

Serialization and Deserialization

Enumerated Types

When deserializating strings into Java Enum types, it's useful to be able to accept lowercase, uppercase and mixed-cased string input.

This deserializer attempts to coerce the string value using the provided case first, but then falls back to UPPERCASE or lowercase if needed.

objectMapper.registerModule(new SimpleModule()
        .setDeserializerModifier(new EnumUppercaseDeserializerModifier()));

Trimming Whitespace

When deserializing string values, it's useful to automatically trim leading and trailing whitespace including newlines, tabs, etc.

This deserializer uses the Character.isWhitespace(char) function to determine what is whitespace.

objectMapper.registerModule(new SimpleModule()
        .addDeserializer(String.class, TrimWhitespaceDeserializer.SINGLETON));

ZonedDateTime

When serializing and deserializing ZonedDateTime values, it's useful to accept a wide variety of string formats.

In addition to the widely-supported ISO date format, you also get variety of other common formats:

  • yyyy-MM-dd
  • yyyyMMdd
  • yyyyMMMddd
  • MM/dd/yyyy
  • MM-dd-yyyy
  • MM.dd.yyyy
  • dd-MMM-yyyy
  • MMM dd, YYYY
  • MMMM dd, yyyy
objectMapper.registerModule(new SimpleModule()
        .addSerializer(new ZonedDateTimeSerializer()) //
        .addDeserializer(ZonedDateTime.class, new ZonedDateTimeDeserializer());

GeoPoint

This library exposes an immutable GeoPoint class that represents a lat/lon coordinate. GeoPoint supports a variety of serialization and deserialization options, which happen to be compatible with those used by Elasticsearch.

Latitude and longitude fields

"location": {
  "lat": 41.12,
  "lon": -71.34
}

Comma separated latitude and longitude

"location": "41.12,-71.34"

Geohash

"location": "drm3btev3e86"

MapPath

When deserializing JSON that does not conform to a fixed schema, it's sometimes useful to deserialize the JSON to native Java objects like Map and List instead using Jackson's tree structure (e.g. JsonNode).

When we do this however we lose the ability to traverse the data in an easy way and null-safe manner. Enter MapPath, which is loosely inspired by JsonPath.

Let's assume the following JSON input:

{
  "store": {
    "book": [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  },
  "expensive": 10
}

Let's use Jackson to parse this JSON into a Map.

String json = "...see above...";
Map map = objectMapper.readValue(json, Map.class);

Let's wrap the Map using MapPath

MapPath mapPath = MapPath.of(map);

Now we can traverse the properties of the map very easily:

   mapPath.getString("store.bicycle.color")    ' red
   mapPath.getDouble("store.bicycle.price")    ' 19.95
   mapPath.getInt("expensive")                 ' 10

Installation

The library is available on Maven Central.

Maven

Add the following to your pom.xml:

<repositories>
    <repository>
        <id>central</id>
        <name>Central Repository</name>
        <url>http://repo.maven.apache.org/maven2</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>

...

<dependency>
    <groupId>com.arakelian</groupId>
    <artifactId>jackson-utils</artifactId>
    <version>3.8.0</version>
    <scope>compile</scope>
</dependency>

Gradle

Add the following to your build.gradle:

repositories {
  mavenCentral()
}

dependencies {
  compile 'com.arakelian:jackson-utils:3.8.0'
}

Licence

Apache Version 2.0

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.