Jackson Interceptor Module

Jackson 2.x module for intercepting and manipulating raw JSON before deserialization and after serialization.

License

License

Categories

Categories

JSON Data Jackson
GroupId

GroupId

com.github.jonpeterson
ArtifactId

ArtifactId

jackson-module-json-interceptor
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Jackson Interceptor Module
Jackson 2.x module for intercepting and manipulating raw JSON before deserialization and after serialization.
Project URL

Project URL

https://github.com/jonpeterson/jackson-module-json-interceptor
Source Code Management

Source Code Management

https://github.com/jonpeterson/jackson-module-json-interceptor

Download jackson-module-json-interceptor

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.jonpeterson/jackson-module-json-interceptor/ -->
<dependency>
    <groupId>com.github.jonpeterson</groupId>
    <artifactId>jackson-module-json-interceptor</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.jonpeterson/jackson-module-json-interceptor/
implementation 'com.github.jonpeterson:jackson-module-json-interceptor:1.0.0'
// https://jarcasting.com/artifacts/com.github.jonpeterson/jackson-module-json-interceptor/
implementation ("com.github.jonpeterson:jackson-module-json-interceptor:1.0.0")
'com.github.jonpeterson:jackson-module-json-interceptor:jar:1.0.0'
<dependency org="com.github.jonpeterson" name="jackson-module-json-interceptor" rev="1.0.0">
  <artifact name="jackson-module-json-interceptor" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.jonpeterson', module='jackson-module-json-interceptor', version='1.0.0')
)
libraryDependencies += "com.github.jonpeterson" % "jackson-module-json-interceptor" % "1.0.0"
[com.github.jonpeterson/jackson-module-json-interceptor "1.0.0"]

Dependencies

compile (2)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar [2.2.0,)
org.slf4j : slf4j-api jar 1.7.12

test (3)

Group / Artifact Type Version
org.codehaus.groovy : groovy-all jar 2.4.5
org.slf4j : slf4j-simple jar 1.7.12
org.spockframework : spock-core jar 1.0-groovy-2.4

Project Modules

There are no modules declared in this project.

Jackson JSON Interceptor Module

Jackson 2.x module for intercepting and manipulating raw JSON before deserialization and after serialization.

Compatibility

Compiled for Java 6 and tested with Jackson 2.2 - 2.8.

Getting Started with Gradle

dependencies {
    compile 'com.github.jonpeterson:jackson-module-json-interceptor:1.0.0'
}

Getting Started with Maven

<dependency>
    <groupId>com.github.jonpeterson</groupId>
    <artifactId>jackson-module-json-interceptor</artifactId>
    <version>1.0.0</version>
</dependency>

Example

Define the POJO models

class CarsByType {
    String type
    List<Car> data
}
@JsonInterceptors(beforeDeserialization = CarDeserializationInterceptor)
class Car {
    String make
    String model
    int year
}

Define the JSON interceptor

class CarDeserializationInterceptor implements JsonInterceptor {

    @Override
    JsonNode intercept(JsonNode node, JsonNodeFactory nodeFactory) {
        if(node.isTextual()) {
            def parts = node.asText().split(':')

            def objectNode = nodeFactory.objectNode()
            objectNode.put('make', parts[0])
            objectNode.put('model', parts[1])
            objectNode.put('year', parts[2] as int)
            node = objectNode
        }

        return node
    }
}

Test the interceptor

// create the ObjectMapper and load the JSON Interceptor Module
def mapper = new ObjectMapper().registerModule(new JsonInterceptorModule())

// deserialize the data
def cars = mapper.readValue(

    // note that Honda Civic is expressed in a packed string and Toyota Camry is expressed in an exploded object
    '''{
      |  "type": "sedans",
      |  "data": [
      |    "Honda:Civic:2016",
      |    {
      |      "make":"Toyota",
      |      "model":"Camry",
      |      "year":2017
      |    }
      |  ]
      |}'''.stripMargin(),

    // class to deserialize as
    CarsByType
)

// serialize and compare
assert mapper.writeValueAsString(cars) == '{"type":"sedans","data":[{"make":"Honda","model":"Civic","year":2016},{"make":"Toyota","model":"Camry","year":2017}]}'

See other test cases under src/test/groovy.

JavaDoc

Versions

Version
1.0.0