An AutoValue extension that supports JSON serialization and deserialization through Moshi.
To opt in to Moshi support, annotate your AutoValue class with @AutoMoshi
:
@AutoValue @AutoMoshi abstract class Foo {
abstract String a();
}
AutoMoshi will generate a JsonAdapter
for each @AutoMoshi
annotated class.
There is generally no need to reference these generated JsonAdapter
s directly - instead, create an abstract class that implements JsonAdapter.Factory
and annotate it with @AutoMoshi.Factory
.
AutoMoshi will generate a package-private implementation of this factory class, which should be passed to the builder used to create your Moshi
instance:
@AutoMoshi.Factory abstract class AdapterFactory implements JsonAdapter.Factory {
static JsonAdapter.Factory create() {
return new AutoMoshi_AdapterFactory();
}
}
Moshi moshi = new Moshi.Builder().add(AdapterFactory.create()).build();
If your factory class is nested, it must be static and must have at least package visibility.
The generated implementation will have a name of the form AutoMoshi_Outer_Middle_Inner
, where Inner
is the name of your factory class:
class Outer {
@AutoMoshi.Factory static abstract class InnerAdapterFactory implements JsonAdapter.Factory {
static JsonAdapter.Factory create() {
return new AutoMoshi_Outer_InnerAdapterFactory();
}
}
}
To use AutoMoshi in an Android project, add the following lines to your build.gradle
:
dependencies {
provided 'glass.phil.auto.moshi:auto-moshi-annotations:0.2.0'
annotationProcessor 'glass.phil.auto.moshi:auto-moshi-processor:0.2.0'
}
As these artifacts are available only at compile time, an explicit dependency on Moshi is also required:
dependencies {
compile 'com.squareup.moshi:moshi:1.5.0'
}
Releases are published to Maven Central. Snapshots are published to Sonatype's snapshots repository.
This library is heavily influenced by auto-value-moshi.