AutoMoshi annotations

An AutoValue extension that supports JSON serialization and deserialization through Moshi.

License

License

Categories

Categories

Auto Application Layer Libs Code Generators Moshi Data JSON
GroupId

GroupId

glass.phil.auto.moshi
ArtifactId

ArtifactId

auto-moshi-annotations
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

AutoMoshi annotations
An AutoValue extension that supports JSON serialization and deserialization through Moshi.
Project URL

Project URL

https://github.com/PhilGlass/auto-moshi/
Source Code Management

Source Code Management

https://github.com/PhilGlass/auto-moshi/

Download auto-moshi-annotations

How to add to project

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

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

AutoMoshi

An AutoValue extension that supports JSON serialization and deserialization through Moshi.

Usage

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 JsonAdapters 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();
    }
  }
}

Download

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.

Acknowledgements

This library is heavily influenced by auto-value-moshi.

Versions

Version
0.2.0
0.1.0