AutoValue: Jackson Extension

AutoValue extension for serializing with Jasckon

License

License

Categories

Categories

Auto Application Layer Libs Code Generators Jackson Data JSON
GroupId

GroupId

com.ppiech.auto.value
ArtifactId

ArtifactId

auto-value-jackson
Last Version

Last Version

0.1.2
Release Date

Release Date

Type

Type

jar
Description

Description

AutoValue: Jackson Extension
AutoValue extension for serializing with Jasckon
Project URL

Project URL

https://github.com/ppiech/auto-value-jackson/
Source Code Management

Source Code Management

https://github.com/ppiech/auto-value-jackson/

Download auto-value-jackson

How to add to project

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

Dependencies

compile (5)

Group / Artifact Type Version
com.ppiech.auto.value : jackson-auto jar 0.1.2
com.squareup : javapoet jar 1.8.0
com.google.auto.value : auto-value jar 1.3
com.google.auto.service : auto-service jar 1.0-rc2
com.google.auto : auto-common jar 0.6

test (3)

Group / Artifact Type Version
junit : junit jar 4.11
com.google.truth : truth jar 0.27
com.google.testing.compile : compile-testing jar 0.9

Project Modules

There are no modules declared in this project.

AutoValue: Jackson JSON Parser Extension

Build Status

An extension for Google's AutoValue that creates a simple JsonMapper for each AutoValue annotated object. Based on LoganSquare and auto-value-gson projects.

Usage

Simply include auto-value-jackson in your project and add a public static method to your @AutoValue annotated class returning a JsonMapper. You can also annotate your properties using @JsonProperty to define an alternate name for de/serialization.

@AutoValue public abstract class Foo {
  abstract String bar();
  @JsonProperty(name="Baz") abstract String baz();
  abstract int quux();
  abstract String with_underscores();

  // The public static method returning a <Foo> is what
  // tells auto-value-jackson to create a JsonMapper for Foo.
  public static JsonMapper<Foo> jsonMapper(JacksonAuto jacksonAuto) {
    return new AutoValue_Foo.JsonObjectMapper(jacksonAuto)
      // You can set custom default values
      .setDefaultQuux(4711)
      .setDefaultWith_underscores("");
  }
}

Now build your project and de/serialize your Foo.

The JsonMapper

To trigger JsonMapper generation, you need include a non-private static factory method that accepts a JacksonAuto parameter and returns a JsonMapper for your AutoValue type. From within this method you can instantiate a new JsonObjectMapper which will have been generated as an inner class of your AutoValue generated implementation.

@AutoValue public abstract class Foo {
  // properties...

  public static JsonMapper<Foo> typeAdapter(JacksonAuto jacksonAuto) {
    return new AutoValue_Foo.JsonObjectMapper(jacksonAuto);
  }
}

Generics support

TODO

Factory

Optionally, auto-value-gson can create a single JsonMapperFactory so that you don't have to add each generated JsonMapper to your JacksonAuto instance manually.

To generate a JsonMapperFactory for all of your auto-value-jackson classes, simply create an abstract class that implements JsonMapperFactory and annotate it with @AutoValueJsonMapperFactory, and auto-value-jackson will create an implementation for you. You simply need to provide a static factory method, just like your AutoValue classes, and you can use the generated JsonMapperFactory to help JacksonAuto de/serialize your types.

@AutoValueJsonMapperFactory
public abstract class MyAdapterFactory implements JsonMapperFactory {

  // Static factory method to access the package
  // private generated implementation
  public static JsonMapperFactory create() {
    return new AutoValueJackson_MyAdapterFactory();
  }

}

Then you simply need to register the Factory with Gson.

JacksonAuto gson = new JacksonAuto.Builder()
    .registerJsonMapperFactory(MyAdapterFactory.create())
    .build();

Download

Add a Gradle dependency to the apt and provided configuration.

apt 'com.ppiech.auto.value:auto-value-jackson:0.1.0'
provided 'com.ppiech.auto.value:auto-value-jackson:0.1.0'

(Using the android-apt plugin)

Snapshots of the latest development version are available in Sonatype's snapshots repository.

You will also need a normal runtime dependency for jackson-auto runtime component, which pulls in jackson-core as well.

compile 'com.ppiech.auto.value:jackson-auto:0.1.0'

License

Copyright 2015 Ryan Harter
Copyright 2015 BlueLine Labs, Inc.
Copyright 2017 Fitbit, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Versions

Version
0.1.2
0.1.1
0.1.0