Kotlin tooling for generating kotlinx.serialization serializers for serializing a class as a bitmask

Overview

kotlinx-serialization-bitmask

Kotlin tooling for generating kotlinx.serialization serializers for serializing a class as a bitmask.

Example

@SerializableBitmask
@Serializable(with = UnixPermissionSerializer::class)
data class UnixPermission(
    val read: Boolean,
    val write: Boolean,
    val execute: Boolean
)
val permission = UnixPermission(read = true, write = true, execute = true)
println(Json.encodeToString(permission)) // 7

Setup

kotlinx-serialization-bitmask uses ksp for annotation processing.

JVM Only

plugins {
    kotlin("jvm") version "1.6.20"
    kotlin("plugin.serialization") version "1.6.20"
    id("com.google.devtools.ksp") version "1.6.20-1.0.5"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(platform("dev.nycode.kotlinx-serialization-bitmask:bom:1.0.0"))
    compileOnly("dev.nycode.kotlinx-serialization-bitmask", "annotations")
    implementation("dev.nycode.kotlinx-serialization-bitmask", "runtime")
    ksp("dev.nycode.kotlinx-serialization-bitmask", "annotation-processor")
    implementation("org.jetbrains.kotlinx", "kotlinx-serialization-core", "1.3.2")
}

kotlin {
    sourceSets {
        all {
            languageSettings {
                optIn("kotlin.RequiresOptIn")
            }
        }
        main {
            kotlin.srcDir("build/generated/ksp/main/kotlin")
        }
        test {
            kotlin.srcDir("build/generated/ksp/test/kotlin")
        }
    }
}

Multiplatform

KSP needs some more configuration to work correctly on multiplatform projects. You need to use the correct configuration to apply the processor to the correct source set.

Name Source Set Configuration
Common1 commonMain kspCommonMainMetadata
JVM jvmMain kspJvm
JS jsMain kspJs
Other other kspOther
plugins {
    kotlin("multiplatform") version "1.6.20"
    kotlin("plugin.serialization") version "1.6.20"
    id("com.google.devtools.ksp") version "1.6.20-1.0.5"
}

repositories {
    mavenCentral()
}

kotlin {
    jvm()
    js(BOTH) {
        browser()
        nodejs()
    }
    sourceSets {
        all {
            languageSettings {
                optIn("kotlin.RequiresOptIn")
            }
        }
        commonMain {
            dependencies {
                compileOnly("dev.nycode.kotlinx-serialization-bitmask:annotations:1.0.0")
                implementation("dev.nycode.kotlinx-serialization-bitmask:runtime:1.0.0")
                ksp("dev.nycode.kotlinx-serialization-bitmask:annotation-processor:1.0.0")
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.3.2")
            }
            kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
        }
    }
}

dependencies {
    kspCommonMainMetadata("dev.nycode.kotlinx-serialization-bitmask",
        "annotation-processor",
        "1.0.0")
}

Footnotes

  1. (Only works if multiple source sets are defined):

You might also like...
Kotlin and Java API for generating .swift source files.

SwiftPoet SwiftPoet is a Kotlin and Java API for generating .swift source files. Source file generation can be useful when doing things such as annota

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

An android application for generating random quotes for learning Room, Jetpack Navigation and Lifecycles, Retrofit
An android application for generating random quotes for learning Room, Jetpack Navigation and Lifecycles, Retrofit

Random-Quote-Generator An android application for generating random quotes for learning Room, Jetpack Navigation and Lifecycles, Retrofit MAD Score Te

An experimental UI toolkit for generating PowerPoint presentation files using Compose
An experimental UI toolkit for generating PowerPoint presentation files using Compose

ComposePPT An experimental UI toolkit for generating PowerPoint presentation files(.pptx) using Compose. Inspired by Glance and Mosaic. Why? This proj

A React Native library making file access easier for developers as first class citizens, without the tears

React Native File Gateway A React Native library making file access easier for developers as first class citizens, without the tears. ⚠️ NOTE: This li

It is far easier to design a class to be thread-safe than to retrofit it for thread safety later
It is far easier to design a class to be thread-safe than to retrofit it for thread safety later

"It is far easier to design a class to be thread-safe than to retrofit it for thread safety later." (Brian Goetz - Java concurrency: Publisher: Addiso

Run Kotlin/JS libraries in Kotlin/JVM and Kotlin/Native programs

Zipline This library streamlines using Kotlin/JS libraries from Kotlin/JVM and Kotlin/Native programs. It makes it possible to do continuous deploymen

Releases(1.0.0)
  • 1.0.0(Apr 9, 2022)

    Initial Release

    What's Changed

    • Add ci by @NyCodeGHG in https://github.com/NyCodeGHG/kotlinx-serialization-bitmask/pull/1
    • Fix MPP example by @DRSchlaubi in https://github.com/NyCodeGHG/kotlinx-serialization-bitmask/pull/2

    New Contributors

    • @NyCodeGHG made their first contribution in https://github.com/NyCodeGHG/kotlinx-serialization-bitmask/pull/1
    • @DRSchlaubi made their first contribution in https://github.com/NyCodeGHG/kotlinx-serialization-bitmask/pull/2

    Full Changelog: https://github.com/NyCodeGHG/kotlinx-serialization-bitmask/commits/1.0.0

    Source code(tar.gz)
    Source code(zip)
Owner
marie
High school student from Germany. she/her
marie
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
Android Bundle format support for Kotlinx Serialization.

Bundlizer Android Bundle format support for Kotlinx Serialization. Usage Annotate your data models with @Serializable: import kotlinx.serialization.Se

Ahmed Mourad 69 Nov 9, 2022
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
Android Parcelable support for the Kotlinx Serialization library.

Android Parcelable support for the Kotlinx Serialization library.

Christopher 50 Nov 20, 2022
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
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
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
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
Functional Kotlin & Arrow based library for generating and verifying JWTs and JWSs

kJWT Functional Kotlin & Arrow based library for generating and verifying JWTs and JWSs. JWS JWT The following Algorithms are supported: HS256 HS384 H

Peter vR 31 Dec 25, 2022