All Projects → chRyNaN → serialization-parcelable

chRyNaN / serialization-parcelable

Licence: Apache-2.0 license
Android Parcelable support for the Kotlinx Serialization library.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to serialization-parcelable

realm-kotlin-samples
Samples demonstrating the usage of Realm-Kotlin SDK
Stars: ✭ 50 (-5.66%)
Mutual labels:  kotlin-multiplatform, kotlin-multiplatform-library, kotlin-multiplatform-mobile
chords
A Kotlin multi-platform view library for displaying stringed instrument chord diagrams
Stars: ✭ 25 (-52.83%)
Mutual labels:  kotlin-multiplatform, kotlin-multiplatform-library
CompleteKotlin
Gradle Plugin to enable auto-completion and symbol resolution for all Kotlin/Native platforms.
Stars: ✭ 236 (+345.28%)
Mutual labels:  kotlin-multiplatform, kotlin-multiplatform-mobile
Aboutlibraries
AboutLibraries automatically detects all dependencies of a project and collects their information including the license. Optionally visualising it via the provided ui components.
Stars: ✭ 2,777 (+5139.62%)
Mutual labels:  kotlin-multiplatform, kotlin-multiplatform-library
moko-geo
Geolocation access for mobile (android & ios) Kotlin Multiplatform development
Stars: ✭ 41 (-22.64%)
Mutual labels:  kotlin-multiplatform, kotlin-multiplatform-mobile
Scout
Scout is a kotlin multiplatform application that allows users to search and save games to lists to be browsed later.
Stars: ✭ 28 (-47.17%)
Mutual labels:  kotlin-multiplatform, kotlinx-serialization
KMP-NativeCoroutines
Library to use Kotlin Coroutines from Swift code in KMP apps
Stars: ✭ 502 (+847.17%)
Mutual labels:  kotlin-multiplatform, kotlin-multiplatform-mobile
StarWars
Minimal GraphQL based Jetpack Compose, Wear Compose and SwiftUI Kotlin Multiplatform sample (using StarWars endpoint - https://graphql.org/swapi-graphql)
Stars: ✭ 165 (+211.32%)
Mutual labels:  kotlin-multiplatform, kotlin-multiplatform-mobile
tv-maniac
Tv-Maniac is a Multiplatform app (Android & iOS) for viewing TV Shows from TMDB.
Stars: ✭ 55 (+3.77%)
Mutual labels:  kotlin-serialization, kotlin-multiplatform
KotlinMultiplatformAndoridParcelize
Use the Parcelize Annotation of the Kotlin Android Extensions in Kotin Multiplatform projects
Stars: ✭ 16 (-69.81%)
Mutual labels:  kotlin-serialization, kotlin-multiplatform
auto-parcel
A fast annotation processor to make your objects `Parcelable` without writing any of the boilerplate.
Stars: ✭ 80 (+50.94%)
Mutual labels:  parcel, parcelable
json-serialization-benchmarking
Miscellaneous benchmarks for JSON serialization on JVM/Android
Stars: ✭ 48 (-9.43%)
Mutual labels:  serialization, kotlinx-serialization
Koin
Koin - a pragmatic lightweight dependency injection framework for Kotlin
Stars: ✭ 7,142 (+13375.47%)
Mutual labels:  kotlin-multiplatform, kotlin-multiplatform-library
moko-network
Network components with codegeneration of rest api for mobile (android & ios) Kotlin Multiplatform development
Stars: ✭ 107 (+101.89%)
Mutual labels:  serialization, kotlin-multiplatform
D-KMP-sample
D-KMP Architecture official sample: it uses a shared KMP ViewModel and Navigation for Compose and SwiftUI apps.
Stars: ✭ 636 (+1100%)
Mutual labels:  kotlin-multiplatform, kotlin-multiplatform-mobile
SafeParcel
Helper library and format description for SafeParcel, a version-agnostic parcelable serializer
Stars: ✭ 29 (-45.28%)
Mutual labels:  serialization
json to cpp
Generate C++ class from JSON data
Stars: ✭ 42 (-20.75%)
Mutual labels:  serialization
Cereal
A C++11 library for serialization
Stars: ✭ 2,986 (+5533.96%)
Mutual labels:  serialization
pony-capnp
Cap’n Proto plugin for generating serializable Pony classes. 🐴 - 🎩'n 🅿️
Stars: ✭ 19 (-64.15%)
Mutual labels:  serialization
FlexBuffersSwift
Swift implementation of FlexBuffers - a sub project of FlatBuffers
Stars: ✭ 24 (-54.72%)
Mutual labels:  serialization

parcelable

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

// Add to Android Intent
intent.putExtra(name = "com.example.MyModel", value = myModel, serializer = MyModel.serializer())

// Retrieve from Android Intent
val result = intent.getParcelableExtra(name = "com.example.MyModel", deserializer = MyModel.serializer())

check(result == myModel) // True

// Compose
val result = rememberSavable(serializer = MyModel.serializer()) { MyModel(...) }

All serialization for an Android or Multi-platform project can be handled from a single library, no need to create both custom Android Parcelers and Serializers. Use the Kotlinx Serialization library, and it will automatically work with Android's Parcel, Bundle, and Intent.

Building

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 = uri("https://repo.repsy.io/mvn/chrynan/public") }
}

Dependency

Core

implementation("com.chrynan.parcelable:parcelable-core:$VERSION")

Compose

implementation("com.chrynan.parcelable:parcelable-compose:$VERSION")

Usage

Set up the Parcelable object

Similar to the Json object introduced by the Kotlinx Serialization library, this library introduces a Parcelable object. The extension functions default to using the Parcelable.Default value if a Parcelable instance is not provided, but if customization is required, an instance can be created as follows:

val parcelable = Parcelable {
    serializersModule = mySerializersModule
}

Use Kotlinx Serialization

Utilize the Kotlinx Serialization to serialize your model classes. These classes can then be used with Android's Bundle, Intent, and Parcel objects.

@Serializable
data class MyModel(
    val intField: Int,
    val stringField: String,
    val nullableStringField: String? = null
)

Encode and decode models

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)

Parcels and Bundles

// To
parcelable.encodeToParcel(parcel, myModel)
val bundle = parcelable.encodeToBundle(myModel)

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

Saver with Jetpack Compose

val model = rememberSavable(parcelable = parcelable, serializer = MyModel.serializer) { myModel }

Multiplatform

This library is a Kotlin Multi-platform library and supports Android, iOS, JVM, and JS Kotlin targets. The Parcelable and Parcel objects are available in the common source set and can be used to encode and decode models. Then, in platform specific code, the Parcels can be stored and retrieved between different components (ex: Android Intents).

// Common Code
val parcel = parcelable.encodeToParcel(serializer = MyModel.serializer(), value = myModel)

// Android Code
Bundle().readFromParcel(parcel)

Documentation

More detailed documentation is available in the docs folder. The entry point to the documentation can be found here.

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.
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].