Android AutoParcel Processor

Port of a port of Google AutoValue for the Android platform to work seamlessly with gson

License

License

Categories

Categories

Auto Application Layer Libs Code Generators Gson Data JSON
GroupId

GroupId

me.tatarka
ArtifactId

ArtifactId

auto-parcel-gson-processor
Last Version

Last Version

0.2
Release Date

Release Date

Type

Type

jar
Description

Description

Android AutoParcel Processor
Port of a port of Google AutoValue for the Android platform to work seamlessly with gson
Project URL

Project URL

https://github.com/evant/auto-parcel-gson
Source Code Management

Source Code Management

https://github.com/evant/auto-parcel-gson

Download auto-parcel-gson-processor

How to add to project

<!-- https://jarcasting.com/artifacts/me.tatarka/auto-parcel-gson-processor/ -->
<dependency>
    <groupId>me.tatarka</groupId>
    <artifactId>auto-parcel-gson-processor</artifactId>
    <version>0.2</version>
</dependency>
// https://jarcasting.com/artifacts/me.tatarka/auto-parcel-gson-processor/
implementation 'me.tatarka:auto-parcel-gson-processor:0.2'
// https://jarcasting.com/artifacts/me.tatarka/auto-parcel-gson-processor/
implementation ("me.tatarka:auto-parcel-gson-processor:0.2")
'me.tatarka:auto-parcel-gson-processor:jar:0.2'
<dependency org="me.tatarka" name="auto-parcel-gson-processor" rev="0.2">
  <artifact name="auto-parcel-gson-processor" type="jar" />
</dependency>
@Grapes(
@Grab(group='me.tatarka', module='auto-parcel-gson-processor', version='0.2')
)
libraryDependencies += "me.tatarka" % "auto-parcel-gson-processor" % "0.2"
[me.tatarka/auto-parcel-gson-processor "0.2"]

Dependencies

compile (5)

Group / Artifact Type Version
org.ow2.asm : asm jar 4.1
com.google.code.findbugs : jsr305 jar 2.0.3
org.apache.velocity : velocity jar 1.7
me.tatarka : auto-parcel-gson jar 0.2
com.google.auto.service : auto-service jar 1.0-rc2

test (4)

Group / Artifact Type Version
com.google.testing.compile : compile-testing jar 0.6
com.google.android : android jar 4.1.1.4
com.google.truth : truth jar 0.25
com.google.guava : guava-testlib jar 17.0

Project Modules

There are no modules declared in this project.

Android AutoParcelGson

Port of Google AutoValue for Android with Parcelable generation goodies and gson.

Note: Depending on your needs may want to use gsonvalue instead. While it doesn't support all of gson's features, it solved the problem of it bypassing autovalue's null checks as well as any other invariants you might want to add.

Why AutoValue?

Because it's awesome. I can't explain it better than that.

Ok then why a fork for Android?

Because AutoValue is not extensible yet. This fork adds automatic Parcelable implementation for your POJOS. It's easy as just adding implements Parcelable.

Ok then why fork the fork?

Same reason as the first fork, AutoValue is not extensible. This fork adds support for gson as well.

Fine, how do I use it?

import auto.parcelgson.gson.AutoParcelGsonTypeAdapterFactory;
import auto.parcelgson.AutoParcelGson;
import auto.parcelgson.gson.annotations.SerializedName;

@AutoParcelGson
abstract class SomeModel implements Parcelable {
  abstract String name();
  @SerializedName("sub_models");
  abstract List<SomeSubModel> subModels();
  @SerializedName("models_map");
  abstract Map<String, OtherSubModel> modelsMap();

  static SomeModel create(String name, List<SomeSubModel> subModels, Map<String, OtherSubModel> modelsMap) {
    return new AutoParcelGson_SomeModel(name, subModels, modelsMap);
  }
}

...

Gson gson = new GsonBuilder().registerTypeAdapterFactory(new AutoParcelGsonTypeAdapterFactory()).create();
SomeModel = gson.fromJson(jsonModel, SomeModel.class);

That's that simple. And you get Parcelable, hashCode, equals and toString implementations for free. As your models evolve you don't need to worry about keeping all the boilerplate in sync with the new implementation, it's already taken care of.

All gson annotations are mirrored in auto.parcelgson.gson.annotations which work on methods instead of fields. They will be copied to the fields of the generated class. The provided AutoParcelGsonTypeAdapterFactory will take care of finding the right generated class to construct from the given @AutoParcelGson abstract class.

Can you make the work with jackson/my random json parser?

Sorry nope, this is already a pretty brittle solution due to the lack of extensibility in autovalue. Feel free to make your own fork!

Sounds great, where can I download it?

The easy way is to use Gradle.

buildscript {
  repositories {
    mavenCentral()
    jcenter()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:+'
  }
}

apply plugin: 'com.android.application'

dependencies {
  compile 'me.tatarka:auto-parcel-gson:0.1'
  annotationProcessor 'me.tatarka:auto-parcel-gson-processor:0.1'
}

repositories {
  mavenCentral()
  jcenter()
}

Proguard

The Gson TypeAdapter uses reflection to map your abstract classes to the AutoParcelGson implementations. If you are using proguard you need to keep the classes.

 -keep class **.AutoParcelGson_*
 -keepnames @auto.parcelgson.AutoParcelGson class *

License

Copyright 2015 Evan Tatarka
Copyright 2014 Frankie Sardo
Copyright 2013 Google, 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.2
0.1