Skip to content

Commit b2a05a4

Browse files
committed
Merge remote-tracking branch 'origin/master' into dev
2 parents cf71e08 + d5bc7f7 commit b2a05a4

14 files changed

+188
-106
lines changed

README.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ Kotlin serialization consists of a compiler plugin, that generates visitor code
2323
* [Introduction and references](#introduction-and-references)
2424
* [Setup](#setup)
2525
* [Gradle](#gradle)
26-
* [Using the `plugins` block](#using-the-plugins-block)
27-
* [Using `apply plugin` (the old way)](#using-apply-plugin-the-old-way)
28-
* [Dependency on the JSON library](#dependency-on-the-json-library)
26+
* [1) Setting up the serialization plugin](#1-setting-up-the-serialization-plugin)
27+
* [2) Dependency on the JSON library](#2-dependency-on-the-json-library)
2928
* [Android](#android)
3029
* [Multiplatform (Common, JS, Native)](#multiplatform-common-js-native)
3130
* [Maven](#maven)
@@ -83,9 +82,13 @@ Make sure you have the corresponding Kotlin plugin installed in the IDE, no addi
8382

8483
### Gradle
8584

86-
#### Using the `plugins` block
85+
To set up kotlinx.serialization, you have to do two things:
86+
1) Add the **[serialization plugin](#1-setting-up-the-serialization-plugin)**.
87+
2) Add the **[serialization library dependency](#2-dependency-on-the-json-library)**.
8788

88-
You can set up the serialization plugin with the Kotlin plugin using
89+
#### 1) Setting up the serialization plugin
90+
91+
You can set up the serialization plugin with the Kotlin plugin using the
8992
[Gradle plugins DSL](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block):
9093

9194
Kotlin DSL:
@@ -106,9 +109,10 @@ plugins {
106109
}
107110
```
108111

109-
> Kotlin versions before 1.4.0 are not supported by the stable release of Kotlin serialization
112+
> Kotlin versions before 1.4.0 are not supported by the stable release of Kotlin serialization.
110113
111-
#### Using `apply plugin` (the old way)
114+
<details>
115+
<summary>Using <code>apply plugin</code> (the old way)</summary>
112116

113117
First, you have to add the serialization plugin to your classpath as the other [compiler plugins](https://kotlinlang.org/docs/reference/compiler-plugins.html):
114118

@@ -145,10 +149,11 @@ Then you can `apply plugin` (example in Groovy):
145149
apply plugin: 'kotlin' // or 'kotlin-multiplatform' for multiplatform projects
146150
apply plugin: 'kotlinx-serialization'
147151
```
152+
</details>
148153

149-
#### Dependency on the JSON library
154+
#### 2) Dependency on the JSON library
150155

151-
After setting up the plugin one way or another, you have to add a dependency on the serialization library.
156+
After setting up the plugin, you have to add a dependency on the serialization library.
152157
Note that while the plugin has version the same as the compiler one, runtime library has different coordinates, repository and versioning.
153158

154159
Kotlin DSL:

core/commonMain/src/kotlinx/serialization/ContextualSerializer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import kotlin.reflect.*
2929
* @Serializable
3030
* class ClassWithDate(val data: String, @Contextual val timestamp: Date)
3131
*
32-
* val moduleForDate = serializersModule(MyISO8601DateSerializer)
32+
* val moduleForDate = serializersModuleOf(MyISO8601DateSerializer)
3333
* val json = Json { serializersModule = moduleForDate }
3434
* json.encodeToString(ClassWithDate("foo", Date())
3535
* ```

docs/serialization-guide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Once the project is set up, we can start serializing some classes.
7171
* <a name='serializing-3rd-party-classes'></a>[Serializing 3rd party classes](serializers.md#serializing-3rd-party-classes)
7272
* <a name='passing-a-serializer-manually'></a>[Passing a serializer manually](serializers.md#passing-a-serializer-manually)
7373
* <a name='specifying-serializer-on-a-property'></a>[Specifying serializer on a property](serializers.md#specifying-serializer-on-a-property)
74+
* <a name='specifying-serializer-for-a-particular-type'></a>[Specifying serializer for a particular type](serializers.md#specifying-serializer-for-a-particular-type)
7475
* <a name='specifying-serializers-for-a-file'></a>[Specifying serializers for a file](serializers.md#specifying-serializers-for-a-file)
7576
* <a name='specifying-serializer-globally-using-typealias'></a>[Specifying serializer globally using typealias](serializers.md#specifying-serializer-globally-using-typealias)
7677
* <a name='custom-serializers-for-a-generic-type'></a>[Custom serializers for a generic type](serializers.md#custom-serializers-for-a-generic-type)

docs/serializers.md

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ In this chapter we'll take a look at serializers in more detail, and we'll see h
2424
* [Serializing 3rd party classes](#serializing-3rd-party-classes)
2525
* [Passing a serializer manually](#passing-a-serializer-manually)
2626
* [Specifying serializer on a property](#specifying-serializer-on-a-property)
27+
* [Specifying serializer for a particular type](#specifying-serializer-for-a-particular-type)
2728
* [Specifying serializers for a file](#specifying-serializers-for-a-file)
2829
* [Specifying serializer globally using typealias](#specifying-serializer-globally-using-typealias)
2930
* [Custom serializers for a generic type](#custom-serializers-for-a-generic-type)
@@ -713,7 +714,7 @@ because we don't control the `Date` source code. There are several ways to work
713714
### Passing a serializer manually
714715

715716
All `encodeToXxx` and `decodeFromXxx` functions have an overload with the first serializer parameter.
716-
When a non-serializable class, like `Date`, is the top-level class being serialized we can use those.
717+
When a non-serializable class, like `Date`, is the top-level class being serialized, we can use those.
717718

718719
```kotlin
719720
fun main() {
@@ -770,6 +771,45 @@ The `stableReleaseDate` property is serialized with the serialization strategy t
770771

771772
<!--- TEST -->
772773

774+
### Specifying serializer for a particular type
775+
776+
[`@Serializable`][Serializable] annotation can also be applied directly to the types.
777+
This is handy when a class that requires a custom serializer, such as `Date`, happens to be a generic type argument.
778+
The most common use case for that is when you have a list of dates:
779+
780+
<!--- INCLUDE
781+
import java.util.Date
782+
import java.text.SimpleDateFormat
783+
784+
object DateAsLongSerializer : KSerializer<Date> {
785+
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("Date", PrimitiveKind.LONG)
786+
override fun serialize(encoder: Encoder, value: Date) = encoder.encodeLong(value.time)
787+
override fun deserialize(decoder: Decoder): Date = Date(decoder.decodeLong())
788+
}
789+
-->
790+
791+
```kotlin
792+
@Serializable
793+
class ProgrammingLanguage(
794+
val name: String,
795+
val releaseDates: List<@Serializable(DateAsLongSerializer::class) Date>
796+
)
797+
798+
fun main() {
799+
val df = SimpleDateFormat("yyyy-MM-ddX")
800+
val data = ProgrammingLanguage("Kotlin", listOf(df.parse("2023-07-06+00"), df.parse("2023-04-25+00"), df.parse("2022-12-28+00")))
801+
println(Json.encodeToString(data))
802+
}
803+
```
804+
805+
> You can get the full code [here](../guide/example/example-serializer-16.kt).
806+
807+
```text
808+
{"name":"Kotlin","releaseDates":[1688601600000,1682380800000,1672185600000]}
809+
```
810+
811+
<!--- TEST -->
812+
773813
### Specifying serializers for a file
774814

775815
A serializer for a specific type, like `Date`, can be specified for a whole source code file with the file-level
@@ -803,7 +843,7 @@ fun main() {
803843
println(Json.encodeToString(data))
804844
}
805845
```
806-
> You can get the full code [here](../guide/example/example-serializer-16.kt).
846+
> You can get the full code [here](../guide/example/example-serializer-17.kt).
807847

808848
```text
809849
{"name":"Kotlin","stableReleaseDate":1455494400000}
@@ -857,7 +897,7 @@ fun main() {
857897
}
858898
```
859899

860-
> You can get the full code [here](../guide/example/example-serializer-17.kt).
900+
> You can get the full code [here](../guide/example/example-serializer-18.kt).
861901

862902
```text
863903
{"stableReleaseDate":"2016-02-15","lastReleaseTimestamp":1657152000000}
@@ -905,7 +945,7 @@ fun main() {
905945
}
906946
```
907947
908-
> You can get the full code [here](../guide/example/example-serializer-18.kt).
948+
> You can get the full code [here](../guide/example/example-serializer-19.kt).
909949
910950
The resulting JSON looks like the `Project` class was serialized directly.
911951
@@ -969,7 +1009,7 @@ fun main() {
9691009
To actually serialize this class we must provide the corresponding context when calling the `encodeToXxx`/`decodeFromXxx`
9701010
functions. Without it we'll get a "Serializer for class 'Date' is not found" exception.
9711011

972-
> See [here](../guide/example/example-serializer-19.kt) for an example that produces that exception.
1012+
> See [here](../guide/example/example-serializer-20.kt) for an example that produces that exception.
9731013

9741014
<!--- TEST LINES_START
9751015
Exception in thread "main" kotlinx.serialization.SerializationException: Serializer for class 'Date' is not found.
@@ -1028,7 +1068,7 @@ fun main() {
10281068
}
10291069
```
10301070

1031-
> You can get the full code [here](../guide/example/example-serializer-20.kt).
1071+
> You can get the full code [here](../guide/example/example-serializer-21.kt).
10321072
```text
10331073
{"name":"Kotlin","stableReleaseDate":1455494400000}
10341074
```
@@ -1087,7 +1127,7 @@ fun main() {
10871127
}
10881128
```
10891129

1090-
> You can get the full code [here](../guide/example/example-serializer-21.kt).
1130+
> You can get the full code [here](../guide/example/example-serializer-22.kt).
10911131

10921132
This gets all the `Project` properties serialized:
10931133

@@ -1128,7 +1168,7 @@ fun main() {
11281168
}
11291169
```
11301170

1131-
> You can get the full code [here](../guide/example/example-serializer-22.kt).
1171+
> You can get the full code [here](../guide/example/example-serializer-23.kt).
11321172

11331173
The output is shown below.
11341174

formats/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ For convenience, they have same `groupId`, versioning and release cycle as core
2020
|--------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
2121
| Avro | [sksamuel/avro4k](https://github.com/sksamuel/avro4k) <br> `com.sksamuel.avro4k:avro4k` | JVM only | This library allows serialization and deserialization of objects to and from [Avro](https://avro.apache.org). It will read and write from Avro binary or json streams or generate Avro Generic Records directly. It will also generate Avro schemas from data classes. The library allows for easy extension and overrides for custom schema formats, compatiblity with schemas defined outside out of the JVM and for types not supported out of the box. |
2222
| Bson | [jershell/kbson](https://github.com/jershell/kbson) <br> `com.github.jershell:kbson` | JVM only | Allows serialization and deserialization of objects to and from [BSON](https://docs.mongodb.com/manual/reference/bson-types/). |
23+
| TOML | [Peanuuutz/tomlkt](https://github.com/Peanuuutz/tomlkt) <br> `net.peanuuutz.tomlkt:tomlkt` | all supported platforms | Multiplatform encoder and decoder for [TOML](http://toml.io/) 1.0.0 compliant. This library aims to provide similar API to the official JSON format (such as TomlLiteral, TomlTable), while adding TOML specific features (such as @TomlComment, @TomlMultilineString). |
2324
| TOML | [akuleshov7/ktoml](https://github.com/akuleshov7/ktoml) <br> `com.akuleshov7:ktoml-core` | all supported platforms | Fully Native and Multiplatform Kotlin serialization library for serialization/deserialization of TOML format. This library contains no Java code and no Java dependencies and it implements multiplatform parser, decoder and encoder of TOML. |
24-
| Minecraft NBT | [BenWoodworth/knbt](https://github.com/BenWoodworth/knbt) <br> `net.benwoodworth.knbt:knbt` | all supported platforms | Implements the [NBT format](https://minecraft.fandom.com/wiki/NBT_format) for kotlinx.serialization, and provides a type-safe DSL for constructing NBT tags. |
25+
| Minecraft NBT | [BenWoodworth/knbt](https://github.com/BenWoodworth/knbt) <br> `net.benwoodworth.knbt:knbt` | all supported platforms | Implements the [NBT format](https://minecraft.wiki/w/NBT_format) for kotlinx.serialization, and provides a type-safe DSL for constructing NBT tags. |
2526
| MsgPack | [esensar/kotlinx-serialization-msgpack](https://github.com/esensar/kotlinx-serialization-msgpack) <br> `com.ensarsarajcic.kotlinx:serialization-msgpack` | all supported platforms | Allows serialization and deserialization of objects to and from [MsgPack](https://msgpack.org/). |
2627
| SharedPreferences | [EdwarDDay/serialization.kprefs](https://github.com/EdwarDDay/serialization.kprefs) <br> `net.edwardday.serialization:kprefs` | Android only | This library allows serialization and deserialization of objects into and from Android [SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences). |
2728
| XML | [pdvrieze/xmlutil](https://github.com/pdvrieze/xmlutil) <br> `io.github.pdvrieze.xmlutil:serialization` | all supported platforms | This library allows for reading and writing of XML documents with the serialization library. It is multiplatform, providing both a shared parser/writer for xml as well as platform-specific parsers where available. The library is designed to handle existing xml formats that use features that would not be available in other formats such as JSON. |
@@ -32,4 +33,4 @@ For convenience, they have same `groupId`, versioning and release cycle as core
3233
| android.os.Bundle | [AhmedMourad0/bundlizer](https://github.com/AhmedMourad0/bundlizer) <br> `dev.ahmedmourad.bundlizer:bundlizer-core` | Android | Allow serialization and deserialization of objects to and from [android.os.Bundle](https://developer.android.com/reference/android/os/Bundle). |
3334
| CSV | [hfhbd/kotlinx-serialization-csv](https://github.com/hfhbd/kotlinx-serialization-csv) <br> `app.softwork:kotlinx-serialization-csv` | all supported platforms | Allows serialization and deserialization of CSV files. There are still some limitations (ordered properties). |
3435
| Fixed Length Format | [hfhbd/kotlinx-serialization-csv](https://github.com/hfhbd/kotlinx-serialization-csv) <br> `app.softwork:kotlinx-serialization-flf` | all supported platforms | Allows serialization and deserialization of [Fixed Length Format files](https://www.ibm.com/docs/en/psfa/7.2.1?topic=format-fixed-length-files). Each property must be annotated with `@FixedLength` and there are still some limitations due to missing delimiters. |
35-
| JSON5 | [xn32/json5k](https://github.com/xn32/json5k) <br> `io.github.xn32:json5k` | JVM, Native | Library for the serialization to and deserialization from [JSON5](https://json5.org) text. |
36+
| JSON5 | [xn32/json5k](https://github.com/xn32/json5k) <br> `io.github.xn32:json5k` | JVM, Native | Library for the serialization to and deserialization from [JSON5](https://json5.org) text. |

guide/example/example-serializer-16.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
@file:UseSerializers(DateAsLongSerializer::class)
21
// This file was automatically generated from serializers.md by Knit tool. Do not edit.
32
package example.exampleSerializer16
43

@@ -17,9 +16,13 @@ object DateAsLongSerializer : KSerializer<Date> {
1716
}
1817

1918
@Serializable
20-
class ProgrammingLanguage(val name: String, val stableReleaseDate: Date)
19+
class ProgrammingLanguage(
20+
val name: String,
21+
val releaseDates: List<@Serializable(DateAsLongSerializer::class) Date>
22+
)
2123

2224
fun main() {
23-
val data = ProgrammingLanguage("Kotlin", SimpleDateFormat("yyyy-MM-ddX").parse("2016-02-15+00"))
25+
val df = SimpleDateFormat("yyyy-MM-ddX")
26+
val data = ProgrammingLanguage("Kotlin", listOf(df.parse("2023-07-06+00"), df.parse("2023-04-25+00"), df.parse("2022-12-28+00")))
2427
println(Json.encodeToString(data))
2528
}

0 commit comments

Comments
 (0)