|
| 1 | +# kotlinx.serialization additions |
| 2 | + |
| 3 | +## GenerateEnumSerializer |
| 4 | + |
| 5 | +This annotation and [KSP](https://github.com/google/ksp) processor will generate enum serializers which allow for pre-specified mappings on the enum name. For example, |
| 6 | + |
| 7 | +```kotlin |
| 8 | +@GenerateEnumSerializer(mode = Mode.SnakeCase, name = "MyEnumSerializer") |
| 9 | +@Serializable(with = MyEnumSerializer::class) |
| 10 | +enum class MyEnum { |
| 11 | + HelloWorld, |
| 12 | +} |
| 13 | +``` |
| 14 | + |
| 15 | +will result in an enum that will naturally serialize to/from `"hello_world"`. |
| 16 | + |
| 17 | +To use, add the annotation and processor to `build.gradle`. |
| 18 | + |
| 19 | +```groovy |
| 20 | +plugins { |
| 21 | + id 'org.jetbrains.kotlin.jvm' version '1.6.10' |
| 22 | + id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.10' |
| 23 | + id 'com.google.devtools.ksp' version '1.6.10' |
| 24 | +} |
| 25 | +
|
| 26 | +repositories { |
| 27 | + mavenCentral() |
| 28 | + maven { |
| 29 | + url = uri('https://maven.pkg.github.com/ephemient/kotlinx-serialization-contrib') |
| 30 | + credentials { |
| 31 | + // GitHub username and access token here |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | +
|
| 36 | +dependencies { |
| 37 | + implementation 'com.github.ephemient.kotlinx-serialization-contrib:annotations:0.0.1' |
| 38 | + implementation 'org.jetbrains.kotlinx:kotlinx-serialization-core:1.3.2' |
| 39 | + ksp 'com.github.ephemient.kotlinx-serialization-contrib:processor:0.0.1' |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## JSON-java |
| 44 | + |
| 45 | +This is a [kotlinx.serialization](https://github.com/Kotlin/kotlinx.serialization) format which uses [JSON-java](https://github.com/stleary/JSON-java) instead of `kotlinx.serialization.json`. Usage is similar to other formats. |
| 46 | + |
| 47 | +```kotlin |
| 48 | +val json = JSON.encodeToJSON(serializer, value) |
| 49 | +val string = JSON.encodeToString(serializer, value) |
| 50 | +val value = JSON.decodeFromJSON(serializer, json) |
| 51 | +val value = JSON.decodeFromString(serializer, string) |
| 52 | +``` |
| 53 | + |
| 54 | +The format may be customized using the builder interface. |
| 55 | + |
| 56 | +```kotlin |
| 57 | +JSON { |
| 58 | + serializersModule = SerializersModule { |
| 59 | + // ... |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +This can be found in the `json-java` artifact. |
| 65 | + |
| 66 | +```groovy |
| 67 | +repositories { |
| 68 | + mavenCentral() |
| 69 | + maven { |
| 70 | + url = uri('https://maven.pkg.github.com/ephemient/kotlinx-serialization-contrib') |
| 71 | + credentials { |
| 72 | + // GitHub username and access token here |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +
|
| 77 | +dependencies { |
| 78 | + implementation 'com.github.ephemient.kotlinx-serialization-contrib:json-java:0.0.1' |
| 79 | +} |
| 80 | +``` |
0 commit comments