Android Parcelable support for the Kotlinx Serialization library.

Overview

parcelable

Android Parceling support for the Kotlinx Serialization library.
GitHub tag (latest by date)

Badge

val parcelable = Parcelable.Default
parcelable.encodeToBundle(myModel)
val myModel = parcelable.decodeFromBundle(bundle)

Now all the serialization can be handled from a single library, no need to create both custom Parcelers and Serializers. Have a custom class that requires specific serialization? Use the Kotlinx Serialization library and it will automatically work with Android's Parcel, Bundle, and Intent.

Building the library

The library is provided through Repsy.io. Checkout the releases page to get the latest version.
GitHub tag (latest by date)

Repository

repositories {
    maven { url = "https://repo.repsy.io/mvn/chrynan/public" }
}

Dependency

implementation "com.chrynan.parcelable:parcelable-android:$VERSION"

Using the library

  • Setup the Parcelable object:
val parcelable = Parcelable {
    serializersModule = mySerializersModule
}
@Serializable
data class MyModel(
    val intField: Int,
    val stringField: String,
    val nullableStringField: String? = null
)
  • Pass your model through Intents and Bundles:
// Put
intent.putExtra(key, myModel, parcelable)
bundle.putParcelable(key, myModel, parcelable)

// Get
val myModel = intent.getParcelableExtra(key, parcelable)
val myModel = bundle.getParcelable(key, parcelable)
  • Or serialize to and from Parcels and Bundles:
// To
parcelable.encodeToParcel(parcel, myModel)
val bundle = parcelable.encodeToBundle(myModel)

// From
val myModel = parcelable.decodeFromParcel(parcel)
val myModel = parcelable.decodeFromBundle(bundle)

Sample

The sample-android module contains an Android App showcasing the use of this library and providing a means to test parceling different models.

Simply update the ExpectedModel.model value to be any Serializable class and run the application to test if the parceling worked.

Sample Screenshots

Main Screen Results Screen

License

Copyright 2021 chRyNaN

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.
You might also like...
Automatic CoroutineDispatcher injection and extensions for kotlinx.coroutines

Dispatch Utilities for kotlinx.coroutines which make them type-safe, easier to test, and more expressive. Use the predefined types and factories or de

Small Kafka Playground to play around with Test Containers, and KotlinX Coroutines bindings while reading Kafka Definite Guide V2

KafkaPlayground Small playground where I'm playing around with Kafka in Kotlin and the Kafka SDK whilst reading the Kafka book Definite Guide from Con

An tool to help developer to use Retrofit elegantly while using kotlinx.coroutines.

one An tool to help developer to use Retrofit elegantly while using kotlinx.coroutines. Feature Transform different data structs to one. {errorCode, d

A Kotlin work manager library for Android with progress notifications and Hilt support.

Boot Laces A kotlin work manager library for Android that includes notifications and Hilt support. User Instructions Add the JitPack repository to you

Dependency Injection library for Kotlin Multiplatform, support iOS and Android

Multiplatform-DI library for Kotlin Multiplatform Lightweight dependency injection framework for Kotlin Multiplatform application Dependency injection

Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.
Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.

Codename One - Cross Platform Native Apps with Java or Kotlin Codename One is a mobile first cross platform environment for Java and Kotlin developers

Wrapper of FusedLocationProviderClient for Android to support modern usage like LiveData and Flow

FancyLocationProvider Wrapper of FusedLocationProviderClient for Android to support modern usage like LiveData or Flow. Install Add Jitpack repository

A simple, classic Kotlin MVI implementation based on coroutines with Android support, clean DSL and easy to understand logic

A simple, classic Kotlin MVI implementation based on coroutines with Android support, clean DSL and easy to understand logic

BlurHash support for iOS, Android and JVM via Kotlin Multiplatform
BlurHash support for iOS, Android and JVM via Kotlin Multiplatform

blurhash A Kotlin Multiplatform library to use blurhash in your Android App, iOS / Mac App & JVM Backend. Android iOS JVM Why? If you've tried using b

Comments
  • NPE in ByteArrayParcel init

    NPE in ByteArrayParcel init

    The init block in ByteArrayParcel throws an NPE because data is not set when setDataFromByteArray runs.

    I'm not sure if this is caused by a compiler change in Kotlin 1.7, because ordinarily you'd get a compiler diagnostic like "data is not initialized".

    The easiest fix would be to move the init { } block below where the data property is initialized.

    Stacktrace:

    Cannot invoke "java.util.List.clear()" because "this.data" is null
    java.lang.NullPointerException: Cannot invoke "java.util.List.clear()" because "this.data" is null
    	at com.chrynan.parcelable.core.ByteArrayParcel.setDataFromByteArray(ByteArrayParcel.kt:188)
    	at com.chrynan.parcelable.core.ByteArrayParcel.<init>(ByteArrayParcel.kt:17)
    	at com.chrynan.parcelable.core.ByteArrayParcel.<init>(ByteArrayParcel.kt:14)
    	at com.chrynan.parcelable.core.JvmParcelKt.Parcel(JvmParcel.kt:3)
    
    opened by tadfisher 2
  • Support nullable types

    Support nullable types

    Fixes #3

    Relax the type constraint on encodeToBundle and decodeFromBundle functions that use KSerializer<T> or look up serializers via serializersModule.

    Align the type constraint on rememberSaveable and Saver to match upstream compose; there is still an : Any constraint for rememberSaveable<T : Any> : T but there is none on rememberSaveable<T> : MutableState<T>.

    Also, quickly configure the desktop version of sample-compose for easy testing.

    opened by tadfisher 1
  • Decoding nullable types from Bundle

    Decoding nullable types from Bundle

    Describe the bug

    The Parcelable.decodeFromBundle() functions impose an upper bound of <T : Any> on the value type, which doesn't mirror the decodeFromByteArray and decodeFromParcel functions.

    I'm optimistic that the : Any bound could simply be removed, as it's the serializer's job to encode/decode null and not-null marks.

    opened by tadfisher 1
  • Unmarshalling unknown type code 7340148 at offset 8

    Unmarshalling unknown type code 7340148 at offset 8

    Describe the bug There is a bug in library that happens only on Pixel devices.

    Fatal Exception: java.lang.RuntimeException: Parcel android.os.Parcel@52c801c: Unmarshalling unknown type code 6357038 at offset 8 at android.os.Parcel.readValue(Parcel.java:3305) at android.os.Parcel.readArrayMapInternal(Parcel.java:3623) at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:292) at android.os.BaseBundle.unparcel(BaseBundle.java:236) at android.os.Bundle.filterValues(Bundle.java:395) at android.os.Bundle.filterValues(Bundle.java:405) at android.content.Intent.removeUnsafeExtras(Intent.java:8859) at android.app.ActivityThread.handleRequestAssistContextExtras(ActivityThread.java:3890) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2171) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7839) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

    I can't reproduce it, but we have a lot of bugs in firebase crashlytics. With no specific case, any screen with your parcelable between intents/fragments inside intent is potential threat.

    Smartphone (please complete the following information):

    • Device: any Pixel
    • OS: android 12

    Additional context It is happening with version 0.3.1, I have simultaneously upgraded to 0.4.1 and will give feedback whether it still happens.

    opened by lepicekmichal 7
Releases(0.5.1)
  • 0.5.1(Sep 22, 2022)

    • Addressed Issue https://github.com/chRyNaN/serialization-parcelable/issues/3
    • Updated compose-jb plugin to version 1.2.0-beta01
    • Implemented equals, hashCode, and toString functions for most components

    Full Changelog: https://github.com/chRyNaN/serialization-parcelable/compare/0.5.0...0.5.1

    Source code(tar.gz)
    Source code(zip)
  • 0.5.0(Sep 12, 2022)

    • Fixed NullPointerException in ByteArrayParcel https://github.com/chRyNaN/serialization-parcelable/issues/2
    • Fixed encoding and decoding in ByteArrayParcel
    • Updated Kotlin to version 1.7.10
    • Added desktop UI to sample-compose module

    Full Changelog: https://github.com/chRyNaN/serialization-parcelable/compare/0.4.2...0.5.0

    Source code(tar.gz)
    Source code(zip)
  • 0.4.2(Jul 22, 2022)

    • Added Javascript support for parcelable-compose module

    Full Changelog: https://github.com/chRyNaN/serialization-parcelable/compare/0.4.1...0.4.2

    Source code(tar.gz)
    Source code(zip)
  • 0.4.1(Feb 14, 2022)

    • Added support for Jetpack Compose for both JVM and Android targets
      • New parcelable-compose module
      • Custom Saver class: ParcelableSaver
      • Custom overloaded rememberSavable function

    Full Changelog: https://github.com/chRyNaN/serialization-parcelable/compare/0.4.0...0.4.1

    Source code(tar.gz)
    Source code(zip)
  • 0.4.0(Feb 13, 2022)

    • Created multi-platform Parcel creation functions
    • Added overloaded functions that take SerializationStrategy and DeserializationStrategy
    • Added nullable return type functions

    Full Changelog: https://github.com/chRyNaN/serialization-parcelable/compare/0.3.1...0.4.0

    Source code(tar.gz)
    Source code(zip)
  • 0.3.1(May 15, 2021)

  • 0.2.0(Jan 17, 2021)

    This is the first working version of the library.

    Note: This is still a pre-release version, which means use at your own risk.

    The API may change slightly in upcoming releases but the core concept will remain the same. Check the README file to see how to use the library.

    Source code(tar.gz)
    Source code(zip)
Owner
Christopher
Christopher
Minecraft NBT support for kotlinx.serialization

knbt An implementation of Minecraft's NBT format for kotlinx.serialization. Technical information about NBT can be found here. Using the same version

Ben Woodworth 41 Dec 21, 2022
A tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serialisation and okio

Store A tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serialisatio

Isuru Rajapakse 98 Jan 3, 2023
CSV and FixedLength Formats for kotlinx-serialization

Module kotlinx-serialization-csv Serialize and deserialize ordered CSV and Fixed Length Format Files with kotlinx-serialization. Source code Docs Inst

Philip Wedemann 12 Dec 16, 2022
Kotlin tooling for generating kotlinx.serialization serializers for serializing a class as a bitmask

kotlinx-serialization-bitmask Kotlin tooling for generating kotlinx.serialization serializers for serializing a class as a bitmask. Example @Serializa

marie 2 May 29, 2022
Type-safe arguments for JetPack Navigation Compose using Kotlinx.Serialization

Navigation Compose Typed Compile-time type-safe arguments for JetPack Navigation Compose library. Based on KotlinX.Serialization. Major features: Comp

Kiwi.com 32 Jan 4, 2023
KotlinX Serialization Standard Serializers (KS3)

KotlinX Serialization Standard Serializers (KS3) This project aims to provide a set of serializers for common types. ⚠️ Consider this project to be Al

Emil Kantis 3 Nov 5, 2022
Auto-generate the fastest possible Parcelable implementations for Java and Kotlin

This project is deprecated It will still be maintained, but no new features will be added. Please use Parcelize, as it is the official way of generati

Bradley Campbell 492 Nov 17, 2022
Kotlinx-murmurhash - Kotlin Multiplatform (KMP) library for hashing using MurmurHash

kotlinx-murmurhash Kotlin Multiplatform (KMP) library for MurmurHash, a non-cryp

Gonçalo Silva 23 Dec 27, 2022
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Practical Kotlin Multiplatform on the Web 본 저장소는 코틀린 멀티플랫폼 기반 웹 프로그래밍 워크숍(강좌)을 위해 작성된 템플릿 프로젝트가 있는 곳입니다. 워크숍 과정에서 코틀린 멀티플랫폼을 기반으로 프론트엔드(front-end)는 Ko

SpringRunner 14 Nov 5, 2022
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Building a Full Stack Web App with Kotlin Multiplatform 본 저장소는 INFCON 2022에서 코틀린 멀티플랫폼 기반 웹 프로그래밍 핸즈온랩을 위해 작성된 템플릿 프로젝트가 있는 곳입니다. 핸즈온 과정에서 코틀린 멀티플랫폼을

Arawn Park 19 Sep 8, 2022