All Projects → microg → SafeParcel

microg / SafeParcel

Licence: other
Helper library and format description for SafeParcel, a version-agnostic parcelable serializer

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to SafeParcel

Api struct
API wrapper builder with response serialization
Stars: ✭ 224 (+672.41%)
Mutual labels:  serialization
Jsonapi Rails
Rails gem for fast jsonapi-compliant APIs.
Stars: ✭ 242 (+734.48%)
Mutual labels:  serialization
roswasm suite
Libraries for compiling C++ ROS nodes to Webassembly using Emscripten
Stars: ✭ 62 (+113.79%)
Mutual labels:  serialization
Qs
Quick serialization of R objects
Stars: ✭ 225 (+675.86%)
Mutual labels:  serialization
Parse5
HTML parsing/serialization toolset for Node.js. WHATWG HTML Living Standard (aka HTML5)-compliant.
Stars: ✭ 2,778 (+9479.31%)
Mutual labels:  serialization
Cbor
CBOR RFC 7049 (Go/Golang) - safe & fast with standard API + toarray & keyasint, CBOR tags, float64/32/16, fuzz tested.
Stars: ✭ 250 (+762.07%)
Mutual labels:  serialization
Schematics
Project documentation: https://schematics.readthedocs.io/en/latest/
Stars: ✭ 2,461 (+8386.21%)
Mutual labels:  serialization
dubbo-hessian-lite
Hessian Lite for Apache Dubbo
Stars: ✭ 45 (+55.17%)
Mutual labels:  serialization
Thorsserializer
C++ Serialization library for JSON
Stars: ✭ 241 (+731.03%)
Mutual labels:  serialization
json to cpp
Generate C++ class from JSON data
Stars: ✭ 42 (+44.83%)
Mutual labels:  serialization
Prefser
Wrapper for Android SharedPreferences with object serialization and RxJava Observables
Stars: ✭ 228 (+686.21%)
Mutual labels:  serialization
Hprose Html5
Hprose is a cross-language RPC. This project is Hprose 2.0 Client for HTML5
Stars: ✭ 237 (+717.24%)
Mutual labels:  serialization
Unicorn
A Sitecore utility designed to simplify deployment of Sitecore items across environments automatically
Stars: ✭ 252 (+768.97%)
Mutual labels:  serialization
Hyperion
Polymorphic serialization for .NET
Stars: ✭ 225 (+675.86%)
Mutual labels:  serialization
Fossapps
Fossapps has been abandoned and replaced by Fossapps Creator.
Stars: ✭ 13 (-55.17%)
Mutual labels:  microg
Jsonapi Rb
Efficiently produce and consume JSON API documents.
Stars: ✭ 219 (+655.17%)
Mutual labels:  serialization
Depot.js
📦 depot.js is a storage library with a simple API
Stars: ✭ 247 (+751.72%)
Mutual labels:  serialization
FlexBuffersSwift
Swift implementation of FlexBuffers - a sub project of FlatBuffers
Stars: ✭ 24 (-17.24%)
Mutual labels:  serialization
iris
Lightweight Component Model and Messaging Framework based on ØMQ
Stars: ✭ 50 (+72.41%)
Mutual labels:  serialization
Cereal
A C++11 library for serialization
Stars: ✭ 2,986 (+10196.55%)
Mutual labels:  serialization

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].