AutoValue: Jackson JSON Parser Extension
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.