AutoValue: Parcel Extension

General

Category
Free
Tag
APT
License
Apache License, Version 2.0
Registered
Aug 15, 2015
Favorites
0
Link
https://github.com/rharter/auto-value-parcel
See also
AutoJackson
Auto
piri
Freighter
JsonAnnotation

Additional

Language
Java
Version
0.2.5 (Oct 18, 2016)
Created
May 13, 2015
Updated
Sep 4, 2019
Owner
Ryan Harter (rharter)
Contributors
luciofm
Felipe Lima (felipecsl)
Gabriel Ittner (gabrielittner)
Jake Wharton (JakeWharton)
Niklas Baudy (vanniktech)
cushon
Ryan Harter (rharter)
Menny Even Danan (menny)
Eric Cochran (NightlyNexus)
guavabot
PaulWoitaschek
Bradley Campbell (grandstaish)
ndurell
SUPERCILEX
maxchursin
realdm
mik9
Zac Sweers (ZacSweers)
Show all (19)19
Activity
Badge
Generate
Download
Source code

Advertising

AutoValue: Parcel Extension

An extension for Google's AutoValue that supports Android's Parcelable interface.

Usage

Simply include the AutoValue: Parcel Extension in your project and make any of your @AutoValue annotated classed implement Parcelable.

@AutoValue public abstract class Foo implements Parcelable {

  public abstract String bar();

}

Now build your project and enjoy your Parcelable Foo.

TypeAdapters

Out of the box AutoValue: Parcel Extension support all of the types supported by the Parcel class, but sometimes you may need to parcel other types, like SparseArray or ArrayMap. You can do this using a custom TypeAdapter.

TypeAdapter allows you to define custom de/serialization logic for properties by allowing you to parcel and unparcel those properties manually.

public class DateTypeAdapter implements TypeAdapter<Date> {
  public Date fromParcel(Parcel in) {
    return new Date(in.readLong());
  }
  
  public void toParcel(Date value, Parcel dest) {
    dest.writeLong(value.getTime());
  }
}

Once you've defined your custom TypeAdapter, using it on an AutoValue class is as simple as adding the ParcelAdapter annotation to any property you'd like to be serialized with your TypeAdapter.

@AutoValue public abstract class Foo implements Parcelable {
  @ParcelAdapter(DateTypeAdapter.class) public abstract Date date();
}

Since TypeAdapters require a small runtime component, they are optional. To use TypeAdapters in your project you'll have to add a compile dependency on the auto-value-parcel-adapter artifact.

compile 'com.ryanharter.auto.value:auto-value-parcel-adapter:0.2.7'

Download

Add a Gradle dependency:

annotationProcessor 'com.ryanharter.auto.value:auto-value-parcel:0.2.7'

// Optionally for TypeAdapter support
compile 'com.ryanharter.auto.value:auto-value-parcel-adapter:0.2.7'

(Migrating from apt to annotationProcessor)

License

Copyright 2015 Ryan Harter.

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.