groovy-beans-with-jackson

Implementations of the Jackson API to better support Groovy Beans without annotations, including groovy.transform.Immutable beans.

License

License

Categories

Categories

Groovy Languages Immutables Application Layer Libs Code Generators Jackson Data JSON
GroupId

GroupId

com.phasebash.jackson
ArtifactId

ArtifactId

groovy-beans-with-jackson-immutables-module
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

groovy-beans-with-jackson
Implementations of the Jackson API to better support Groovy Beans without annotations, including groovy.transform.Immutable beans.
Project URL

Project URL

https://github.com/phasebash/groovy-beans-with-jackson
Project Organization

Project Organization

Brandon Zeeb
Source Code Management

Source Code Management

https://github.com/phasebash/groovy-beans-with-jackson

Download groovy-beans-with-jackson-immutables-module

How to add to project

<!-- https://jarcasting.com/artifacts/com.phasebash.jackson/groovy-beans-with-jackson-immutables-module/ -->
<dependency>
    <groupId>com.phasebash.jackson</groupId>
    <artifactId>groovy-beans-with-jackson-immutables-module</artifactId>
    <version>1.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.phasebash.jackson/groovy-beans-with-jackson-immutables-module/
implementation 'com.phasebash.jackson:groovy-beans-with-jackson-immutables-module:1.0'
// https://jarcasting.com/artifacts/com.phasebash.jackson/groovy-beans-with-jackson-immutables-module/
implementation ("com.phasebash.jackson:groovy-beans-with-jackson-immutables-module:1.0")
'com.phasebash.jackson:groovy-beans-with-jackson-immutables-module:jar:1.0'
<dependency org="com.phasebash.jackson" name="groovy-beans-with-jackson-immutables-module" rev="1.0">
  <artifact name="groovy-beans-with-jackson-immutables-module" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.phasebash.jackson', module='groovy-beans-with-jackson-immutables-module', version='1.0')
)
libraryDependencies += "com.phasebash.jackson" % "groovy-beans-with-jackson-immutables-module" % "1.0"
[com.phasebash.jackson/groovy-beans-with-jackson-immutables-module "1.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.codehaus.groovy : groovy-all jar 2.2.1
com.fasterxml.jackson.core : jackson-core jar 2.3.1
org.slf4j : slf4j-api jar 1.7.5
com.fasterxml.jackson.core : jackson-databind jar 2.3.1

test (7)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.0.13
org.slf4j : jcl-over-slf4j jar 1.7.5
org.hamcrest : hamcrest-library jar 1.3
org.slf4j : log4j-over-slf4j jar 1.7.5
junit : junit jar 4.11
org.hamcrest : hamcrest-core jar 1.3
org.gmock : gmock jar 0.8.3

Project Modules

There are no modules declared in this project.

groovy-beans-with-jackson

Implementations of the Jackson API to support Groovy Beans, particularly @Immutables

Current Status

[Build Status] (https://travis-ci.org/phasebash/groovy-beans-with-jackson)

Use Cases

Groovy Beans are a particularly attractive way of developing Java Beans. Yet when making the most of what Groovy provides, particularly the @Immutable transform, this presents challenges during deserialization as Jackson will not be able to find a default constructor (or any annotations if your classes are fully Groovy).

Consider the following Groovy Bean.

@Immutable
class BeanBag {
    Integer count
    String texture
    Zipper zipper
}

Without modification, this stacktrace might look familiar:

com.fasterxml.jackson.databind.JsonMappingException:
No suitable constructor found for type
[simple type, class BeanBag]:
can not instantiate from JSON object (need to add/enable type information?)

As a first naive step, one could provide a private constructor:

@Immutable
class BeanBag {
    private BeanBag() {}
    Integer count
    String texture
    Zipper zipper
}

And would could expect to receive this message:

Groovyc: Explicit constructors not allowed for @Immutable class: BeanBag

Yet, the @Immutable transform prohibits this, any other constructors are also forbidden.

In Use

In order to deserialize @Immutable GroovyBeans without fuss, simply provide the ObjectMapper with a Jackson Module which provides the necessary ValueInstantiator to allow Jackson to instantiate GroovyBeans by their generated HashMap constructors.

ObjectMapper objectMapper = new ObjectMapper()
objectMapper.registerModule(new GroovyImmutableModule())

// this is now possible.
BeanBag beanBag = objectMapper.readValue('{"count": 100, "texture": "smooth"}', BeanBag)

Coordinates

You may make use of this module with the following coordinates:

Maven

<dependency>
    <groupId>com.phasebash.jackson</groupId>
    <artifactId>groovy-beans-with-jackson-immutables-module</artifactId>
    <version>1.0</version>
</dependency>

Gradle

compile(
    [groupId: 'com.phasebash.jackson', name: 'groovy-beans-with-jackson-immutables-module', version: '1.0']
)

Questions, Feedback?

Feel free to submit an issue ticket through github or contact me directly. I will help you.

License

The groovy-beans-with-jackson has been released under Apache 2.0 as per all it's dependencies.

Versions

Version
1.0