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

Last update: May 29, 2022

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):

GitHub

https://github.com/NyCodeGHG/kotlinx-serialization-bitmask
You might also like...

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

Jun 21, 2022

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

Sep 11, 2021

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

Jun 3, 2022

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

Jun 17, 2022

A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

Jan 20, 2022

Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in Kotlin with Jetpack Compose and a backed in Kotlin hosted on AppEngine.

Conferences4Hall Real life Kotlin Multiplatform project with an iOS application developed in Swift with SwiftUI, an Android application developed in K

Jun 15, 2022

Android + Kotlin + Github Actions + ktlint + Detekt + Gradle Kotlin DSL + buildSrc = ❤️

kotlin-android-template 🤖 A simple Github template that lets you create an Android/Kotlin project and be up and running in a few seconds. This templa

Jun 17, 2022

LifecycleMvp 1.2 0.0 Kotlin is MVP architecture implementation with Android Architecture Components and Kotlin language features

LifecycleMvp 1.2 0.0 Kotlin  is MVP architecture implementation with Android Architecture Components and Kotlin language features

MinSDK 14+ Download Gradle Add to project level build.gradle allprojects { repositories { ... maven { url 'https://jitpack.io' }

Nov 9, 2021

Opinionated Redux-like implementation backed by Kotlin Coroutines and Kotlin Multiplatform Mobile

CoRed CoRed is Redux-like implementation that maintains the benefits of Redux's core idea without the boilerplate. No more action types, action creato

Feb 27, 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

Jun 3, 2022
Android Parcelable support for the Kotlinx Serialization library.
Android Parcelable support for the Kotlinx Serialization library.

Android Parcelable support for the Kotlinx Serialization library.

May 11, 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

May 18, 2022
Kotlinx-murmurhash - Kotlin Multiplatform (KMP) library for hashing using MurmurHash

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

May 24, 2022
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

Jun 17, 2022
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

Apr 23, 2022
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

Jun 8, 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

Jun 21, 2022
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

May 27, 2022
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

Jan 8, 2022