Giter VIP home page Giter VIP logo

safeparcel's Introduction

SafeParcel

SafeParcel is a mechanism to serialize objects on the Android platform into Parcelables version-agnostic.

This is achieved by prefixing each field with a unique identifying number and its length. When deserializing, unknown fields can be skipped and the field order is not relevant.

The SafeParcel format was originally developed by Google for Play Services. Until now, neither the format description, nor a library to use it had been released by Google.

For this reason, although we spent much effort into it, we can't guarantee that this implementation is 100% compatible with Google's implementation.

Usage

SafeParcel is in the Maven Central Repository. To use it, just add the following dependency statement to your Android app or library:

compile 'org.microg:safe-parcel:[version]'

To find the latest available version, check the central repository

Directly accessing SafeParcel format

The SafeParcelReader and SafeParcelWriter allow to directly read from or write to the SafeParcel format. However, usage of them requires some amount of code to be written, which is painful if you want to store many objects in SafeParcel format. Therefor, there is a more automatic approach based on reflection build into the library

Automatic safe parceling

The SafeParcelUtil provides several easy methods to use the SafeParcel format to serialize and deserialize object. The three methods createObject, readObject and writeObject allow to create an object from parcel, read from parcel into an existing object or write an object into a parcel.

To use these, you first need to annotate the fields in the object you want to parcel and make the object implement the SafeParcelable interface. In most cases you simply add the @SafeParceled annotation to give each field a unique number. When using ArrayLists, it is required to further annotate the field. Check documentation of @SafeParceled for details. Additionally, make sure that the object has a default or no-parameter constructor. If you don't want such a constructor to be part of the API, make the constructor private.

For convenience, you can even skip the work of calling the methods in SafeParcelUtil by extending the abstract AutoSafeParcelable class. After that, the only thing required to unparcel the object is to add a static CREATOR field with an AutoCreator value, check the example below.

Example

import org.microg.safeparcel.*;

public class ExampleObject extends AutoSafeParcelable {
    @SafeParceled(1000)
    private int versionCode = 1;

    @SafeParceled(1)
    public String name;

    @SafeParceled(value=2, subClass=Integer.class)
    public List<Integer> ids = new ArrayList<Integer>();

    public static final Creator<ExampleObject> CREATOR = new AutoCreator<ExampleObject>(ExampleObject.class);
}

Note: When using ProGuard and automatic safe parceling, make sure that all relevant classes and annotations are available at runtime, as SafeParcelUtil will use reflection. See proguard.txt for relevant proguard rules.

SafeParcel design patterns

It is recommended to add a version code to the fields being parceled in a SafeParcel object. Although the SafeParcel format is version-agnostic, your code may be not. With a version code, you can realize that the object was deserialized from an older version and ensure that fields that did not exist previously are populated.

Additionally it turned out to be a good idea to not remove fields that have been used in the previous version of your app or library. This way, code relying on the old version will continue to work. For performance reason or if it is an unreasonable amount of work to manage the old code, you can still remove the field in a future version, after having the field being marked as deprecated for some time.

License

safeparcel's People

Contributors

emlove avatar mar-v-in avatar sww13 avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

safeparcel's Issues

Support for `Map` in AutoSafeParcelable

Hello Marvin,

Our code uses AutoParcelable for a lot of stuff, and we are stuck trying to add a new Map<> field. Our options include creating a ParcelableMap class, storing our data in a list instead, etc, but we feel that supporting Map directly in AutoSafeParcelable would the best option.

The changes necessary seem straightforward and I can write a PR for that, however I understand that this project mirrors the encoding present in Google APIs, and adding new features might break compatibility with the original -- With that in mind, would the change be acceptable for merging?

google drive not working: folder created on other device does not show up

google drive does not work together with microg:

Error reading field: 6 in com.google.android.gms.playlog.internal.PlayLoggerContext, skipping.
org.microg.safeparcel.SafeParcelReader$ReadException: Expected size 4 got 8 (0x8)
	at org.microg.safeparcel.SafeParcelReader.readStart(SafeParcelReader.java:49)
	at org.microg.safeparcel.SafeParcelReader.readInt(SafeParcelReader.java:65)
	at org.microg.safeparcel.SafeParcelUtil.readField(SafeParcelUtil.java:275)
	at org.microg.safeparcel.SafeParcelUtil.readObject(SafeParcelUtil.java:106)
	at org.microg.safeparcel.SafeParcelUtil.createObject(SafeParcelUtil.java:44)
	at org.microg.safeparcel.AutoSafeParcelable$AutoCreator.createFromParcel(AutoSafeParcelable.java:44)
	at org.microg.safeparcel.AutoSafeParcelable$AutoCreator.createFromParcel(AutoSafeParcelable.java:34)
	at org.microg.safeparcel.SafeParcelReader.readParcelable(SafeParcelReader.java:124)
	at org.microg.safeparcel.SafeParcelUtil.readField(SafeParcelUtil.java:228)
	at org.microg.safeparcel.SafeParcelUtil.readObject(SafeParcelUtil.java:106)
	at org.microg.safeparcel.SafeParcelUtil.createObject(SafeParcelUtil.java:44)
	at org.microg.safeparcel.AutoSafeParcelable$AutoCreator.createFromParcel(AutoSafeParcelable.java:44)
	at org.microg.safeparcel.AutoSafeParcelable$AutoCreator.createFromParcel(AutoSafeParcelable.java:34)
	at com.google.android.gms.clearcut.internal.IClearcutLoggerService$Stub.onTransact(IClearcutLoggerService.java:52)
	at android.os.Binder.execTransact(Binder.java:565)

SafeParcelUtil.getListItemClass() returns null in common cases

The issue described in microg/GmsCore#1186 is caused by SafeParcelUtil.getListItemClass() returning null. This makes the StringList detection fails in SafeParcelType.fromField().

From what I understand, SafeParcelUtil.getListItemClass expects the subClass/subType to be defined in the field annotation, and returns null if it's not present in the annotation. And it won't fall back to checking the generic arguments manually, because the SafeParceled annotation is present. This basically makes getListItemClass fail for any list definition in GmsCore, as subClass/subType is not defined in the annotation (AFAICS, most, if not all, annotations in GmsCore only define the field ID). For instance: https://github.com/microg/android_packages_apps_GmsCore/blob/master/play-services-cast-framework-api/src/main/java/com/google/android/gms/cast/framework/media/NotificationOptions.java#L30

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.