Skip to content

Java Properties format for KotlinX serialization

License

Notifications You must be signed in to change notification settings

bishiboosh/properties-serializer

Java Properties format for KotlinX Serialization

Maven Central Version

This library provides a way to serialize and deserialize Java Properties files using KotlinX Serialization.

Installation

To use this library, add the following dependency to your build.gradle.kts file:

implementation("io.github.bishiboosh:properties-serializer:1.0.0")

Usage

You can use the PropertiesSerializer to serialize and deserialize Java Properties files using the Properties class.

Base functionality uses kotlinx-io to interface with files, but you can also serialize and deserialize to and from strings or byte arrays.

The properties are stored in a Map with string keys and values, serializaed with the format used in Java Properties.

If the given class has non-primitive property d of arbitrary type D, D values are inserted into the same map; keys for such values are prefixed with string d.:

@Serializable
class Data(val property1: String)

@Serializable
class DataHolder(val data: Data, val property2: String)

val result = Properties.encodeToString(DataHolder(Data("value1"), "value2"))
// result will contain the following:
// property2=value2
// data.property1=value1

If the given class has a List property l, each value from the list would be prefixed with l.N., where N is an index for a particular value. Map is treated as a [key,value,...] list.

Difference with the Properties format distributed by KotlinX Serialization

As you may have noticed, the implementation and API surface of this library is very similar to the one provided by KotlinX Serialization in the kotlinx.serialization.properties package.

However, where the KotlinX Serialization implementation just serializes and deserializes from and to a Map, this library actually takes the step to write to or read from an actual data source, which is way more useful for you if you're dealing with configuration files using the Java Properties format.

This library is primarily intended for use in Kotlin Multiplatform projects, where the Properties class from Java cannot be used direcly. If your project is JVM-only, using the KotlinX Serialization library and converting the Map to a Properties object is probably the better option.

Snapshots

Maven metadata URL

You can use the latest snapshot version by using the version in the badge above and adding the following repository to your settings.gradle.kts file:

dependencyResolutionManagement {
    repositories {
        maven("https://central.sonatype.com/repository/maven-snapshots/") {
            mavenContent {
                snapshotsOnly()
            }
        }
        // ... your other repositories
    }
}

About

Java Properties format for KotlinX serialization

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Languages