Giter VIP home page Giter VIP logo

auto-value-parcel's Introduction

AutoValue: Parcel Extension

Build Status

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.4-rc2'

Download

Add a Gradle dependency:

apt 'com.ryanharter.auto.value:auto-value-parcel:0.2.4-rc2'

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

(Using the android-apt plugin)

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.

auto-value-parcel's People

Contributors

felipecsl avatar grandstaish avatar guavabot avatar jakewharton avatar luciofm avatar menny avatar nightlynexus avatar paulwoitaschek avatar rharter avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

isabella232

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.