Skip to content

Commit b3cfe56

Browse files
committed
Merge remote-tracking branch 'origin/master' into dev
2 parents 550e1a8 + e4fa8a3 commit b3cfe56

File tree

8 files changed

+55
-49
lines changed

8 files changed

+55
-49
lines changed

buildSrc/src/main/kotlin/publishing-conventions.gradle.kts

-7
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,6 @@ tasks.withType<KotlinNativeLink>() {
145145
mustRunAfter(tasks.withType<Sign>().named { it == "sign${targetName}Publication" })
146146
}
147147

148-
// Compatibility with old TeamCity configurations that perform :kotlinx-coroutines-core:bintrayUpload
149-
tasks.register("bintrayUpload") {
150-
dependsOn(tasks.publish)
151-
// This is required for K/N publishing
152-
dependsOn(tasks.publishToMavenLocal)
153-
}
154-
155148
fun MavenPom.configureMavenCentralMetadata() {
156149
name = project.name
157150
description = "Kotlin multiplatform serialization runtime library"

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ import kotlin.reflect.*
4242
*
4343
* @Serializable
4444
* class RgbExample(
45-
* @Serializable(with = RgbAsHexString::class) p1: RgpPixel, // Serialize as HEX string, e.g. #FFFF00
46-
* @Serializable(with = RgbAsSingleInt::class) p2: RgpPixel, // Serialize as single integer, e.g. 16711680
47-
* p3: RgpPixel // Serialize as 3 short components, e.g. { "red": 255, "green": 255, "blue": 0 }
45+
* @Serializable(with = RgbAsHexString::class) p1: RgbPixel, // Serialize as HEX string, e.g. #FFFF00
46+
* @Serializable(with = RgbAsSingleInt::class) p2: RgbPixel, // Serialize as single integer, e.g. 16711680
47+
* p3: RgbPixel // Serialize as 3 short components, e.g. { "red": 255, "green": 255, "blue": 0 }
4848
* )
4949
* ```
5050
* In this example, each pixel will be serialized using different data representation.

core/commonMain/src/kotlinx/serialization/encoding/Decoding.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ public interface Decoder {
250250

251251
/**
252252
* Decodes the value of type [T] by delegating the decoding process to the given [deserializer].
253-
* For example, `decodeInt` call us equivalent to delegating integer decoding to [Int.serializer][Int.Companion.serializer]:
254-
* `decodeSerializableValue(IntSerializer)`
253+
* For example, `decodeInt` call is equivalent to delegating integer decoding to [Int.serializer][Int.Companion.serializer]:
254+
* `decodeSerializableValue(Int.serializer())`
255255
*/
256256
public fun <T : Any?> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T =
257257
deserializer.deserialize(this)

core/commonMain/src/kotlinx/serialization/encoding/Encoding.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public interface Encoder {
272272

273273
/**
274274
* Encodes the [value] of type [T] by delegating the encoding process to the given [serializer].
275-
* For example, `encodeInt` call us equivalent to delegating integer encoding to [Int.serializer][Int.Companion.serializer]:
275+
* For example, `encodeInt` call is equivalent to delegating integer encoding to [Int.serializer][Int.Companion.serializer]:
276276
* `encodeSerializableValue(Int.serializer())`
277277
*/
278278
public fun <T : Any?> encodeSerializableValue(serializer: SerializationStrategy<T>, value: T) {

docs/builtin-classes.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,8 @@ even if they are numbers in Kotlin, as we can see below.
351351

352352
### Unit and singleton objects
353353

354-
The Kotlin builtin `Unit` type is also serializable.
355-
`Unit` is a Kotlin [singleton object](https://kotlinlang.org/docs/tutorials/kotlin-for-py/objects-and-companion-objects.html),
354+
The Kotlin builtin [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/) type is also serializable.
355+
`Unit` is a Kotlin [singleton object](https://kotlinlang.org/docs/object-declarations.html#object-declarations-overview),
356356
and is handled equally with other Kotlin objects.
357357

358358
Conceptually, a singleton is a class with only one instance, meaning that state does not define the object,

docs/serialization-guide.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ Once the project is set up, we can start serializing some classes.
6666
* <a name='primitive-serializer'></a>[Primitive serializer](serializers.md#primitive-serializer)
6767
* <a name='delegating-serializers'></a>[Delegating serializers](serializers.md#delegating-serializers)
6868
* <a name='composite-serializer-via-surrogate'></a>[Composite serializer via surrogate](serializers.md#composite-serializer-via-surrogate)
69-
* <a name='hand-written-composite-serializer'></a>[Hand-written composite serializer](serializers.md#hand-written-composite-serializer)
69+
* <a name='handwritten-composite-serializer'></a>[Handwritten composite serializer](serializers.md#handwritten-composite-serializer)
7070
* <a name='sequential-decoding-protocol-experimental'></a>[Sequential decoding protocol (experimental)](serializers.md#sequential-decoding-protocol-experimental)
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)
73-
* <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)
73+
* <a name='specifying-a-serializer-on-a-property'></a>[Specifying a serializer on a property](serializers.md#specifying-a-serializer-on-a-property)
74+
* <a name='specifying-a-serializer-for-a-particular-type'></a>[Specifying a serializer for a particular type](serializers.md#specifying-a-serializer-for-a-particular-type)
7575
* <a name='specifying-serializers-for-a-file'></a>[Specifying serializers for a file](serializers.md#specifying-serializers-for-a-file)
76-
* <a name='specifying-serializer-globally-using-typealias'></a>[Specifying serializer globally using typealias](serializers.md#specifying-serializer-globally-using-typealias)
76+
* <a name='specifying-a-serializer-globally-using-a-typealias'></a>[Specifying a serializer globally using a typealias](serializers.md#specifying-a-serializer-globally-using-a-typealias)
7777
* <a name='custom-serializers-for-a-generic-type'></a>[Custom serializers for a generic type](serializers.md#custom-serializers-for-a-generic-type)
7878
* <a name='format-specific-serializers'></a>[Format-specific serializers](serializers.md#format-specific-serializers)
7979
* <a name='simultaneous-use-of-plugin-generated-and-custom-serializers'></a>[Simultaneous use of plugin-generated and custom serializers](serializers.md#simultaneous-use-of-plugin-generated-and-custom-serializers)

docs/serializers.md

+41-28
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ In this chapter we'll take a look at serializers in more detail, and we'll see h
1919
* [Primitive serializer](#primitive-serializer)
2020
* [Delegating serializers](#delegating-serializers)
2121
* [Composite serializer via surrogate](#composite-serializer-via-surrogate)
22-
* [Hand-written composite serializer](#hand-written-composite-serializer)
22+
* [Handwritten composite serializer](#handwritten-composite-serializer)
2323
* [Sequential decoding protocol (experimental)](#sequential-decoding-protocol-experimental)
2424
* [Serializing 3rd party classes](#serializing-3rd-party-classes)
2525
* [Passing a serializer manually](#passing-a-serializer-manually)
26-
* [Specifying serializer on a property](#specifying-serializer-on-a-property)
27-
* [Specifying serializer for a particular type](#specifying-serializer-for-a-particular-type)
26+
* [Specifying a serializer on a property](#specifying-a-serializer-on-a-property)
27+
* [Specifying a serializer for a particular type](#specifying-a-serializer-for-a-particular-type)
2828
* [Specifying serializers for a file](#specifying-serializers-for-a-file)
29-
* [Specifying serializer globally using typealias](#specifying-serializer-globally-using-typealias)
29+
* [Specifying a serializer globally using a typealias](#specifying-a-serializer-globally-using-a-typealias)
3030
* [Custom serializers for a generic type](#custom-serializers-for-a-generic-type)
3131
* [Format-specific serializers](#format-specific-serializers)
3232
* [Simultaneous use of plugin-generated and custom serializers](#simultaneous-use-of-plugin-generated-and-custom-serializers)
@@ -165,9 +165,11 @@ fun main() {
165165

166166
> You can get the full code [here](../guide/example/example-serializer-04.kt).
167167
168-
<!--- TEST
168+
```text
169169
PrimitiveDescriptor(kotlin.Int)
170-
-->
170+
```
171+
172+
<!--- TEST -->
171173

172174
### Constructing collection serializers
173175

@@ -191,9 +193,11 @@ fun main() {
191193

192194
> You can get the full code [here](../guide/example/example-serializer-05.kt).
193195
194-
<!--- TEST
196+
```text
195197
kotlin.collections.ArrayList(PrimitiveDescriptor(kotlin.String))
196-
-->
198+
```
199+
200+
<!--- TEST -->
197201

198202
### Using top-level serializer function
199203

@@ -217,14 +221,17 @@ fun main() {
217221

218222
> You can get the full code [here](../guide/example/example-serializer-06.kt).
219223
220-
<!--- TEST
224+
```text
221225
kotlin.collections.LinkedHashMap(PrimitiveDescriptor(kotlin.String), Color(rgb: kotlin.Int))
222-
-->
226+
```
227+
228+
<!--- TEST -->
223229

224230
## Custom serializers
225231

226232
A plugin-generated serializer is convenient, but it may not produce the JSON we want
227-
for such a class as `Color`. Let's study alternatives.
233+
for such a class as `Color`.
234+
Let's study the alternatives.
228235

229236
### Primitive serializer
230237

@@ -254,7 +261,7 @@ object ColorAsStringSerializer : KSerializer<Color> {
254261
}
255262
```
256263

257-
Serializer has three required pieces.
264+
A serializer has three required pieces.
258265

259266
* The [serialize][SerializationStrategy.serialize] function implements [SerializationStrategy].
260267
It receives an instance of [Encoder] and a value to serialize.
@@ -419,10 +426,10 @@ class ColorIntArraySerializer : KSerializer<Color> {
419426
Note that we can't use default `Color.serializer().descriptor` here because formats that rely
420427
on the schema may think that we would call `encodeInt` instead of `encodeSerializableValue`.
421428
Neither we can use `IntArraySerializer().descriptor` directly — otherwise, formats that handle int arrays specially
422-
can't tell if `value` is really a `IntArray` or a `Color`. Don't worry, this optimization would still kick in
423-
when serializing actual underlying int array.
429+
can't tell if `value` is really an `IntArray` or a `Color`.
430+
Don't worry, this optimization would still kick in when serializing the actual underlying int array.
424431

425-
> Example of how format can treat arrays specially is shown in the [formats guide](formats.md#format-specific-types).
432+
> An example of how a format can treat arrays specially is shown in the [formats guide](formats.md#format-specific-types).
426433
427434
Now we can use the serializer:
428435

@@ -518,7 +525,7 @@ fun main() {
518525

519526
<!--- TEST -->
520527

521-
### Hand-written composite serializer
528+
### Handwritten composite serializer
522529

523530
There are some cases where a surrogate solution does not fit. Perhaps we want to avoid the performance
524531
implications of additional allocation, or we want a configurable/dynamic set of properties for the
@@ -617,10 +624,10 @@ As before, we got the `Color` class represented as a JSON object with three keys
617624
### Sequential decoding protocol (experimental)
618625
619626
The implementation of the `deserialize` function from the previous section works with any format. However,
620-
some formats either always store all the complex data in order, or only do so sometimes (JSON always stores
621-
collections in order). With these formats the complex protocol of calling `decodeElementIndex` in the loop is
622-
not needed, and a faster implementation can be used if the [CompositeDecoder.decodeSequentially] function returns `true`.
623-
The plugin-generated serializers are actually conceptually similar to the below code.
627+
some formats either always store all the complex data in order or only do so sometimes (JSON always stores
628+
collections in order). With these formats the complex protocol of calling `decodeElementIndex` in a loop is
629+
unnecessary, and a faster implementation can be used if the [CompositeDecoder.decodeSequentially] function returns `true`.
630+
The plugin-generated serializers are actually conceptually similar to the code below.
624631
625632
<!--- INCLUDE
626633
object ColorAsObjectSerializer : KSerializer<Color> {
@@ -715,9 +722,15 @@ We cannot bind the `DateAsLongSerializer` serializer to the `Date` class with th
715722
because we don't control the `Date` source code. There are several ways to work around that.
716723

717724
### Passing a serializer manually
718-
719-
All `encodeToXxx` and `decodeFromXxx` functions have an overload with the first serializer parameter.
720-
When a non-serializable class, like `Date`, is the top-level class being serialized, we can use those.
725+
726+
The `encodeToXxx` and `decodeFromXxx` functions offer overloaded versions
727+
that accept either a [SerializationStrategy] or [DeserializationStrategy] as their first parameter, respectively.
728+
This feature allows you
729+
to provide a custom serializer for types that aren't annotated with [`@Serializable`][Serializable] by default.
730+
731+
This approach is particularly useful
732+
when working with non-serializable classes like `Date` as the top-level object being serialized.
733+
Here's an example:
721734

722735
```kotlin
723736
fun main() {
@@ -734,7 +747,7 @@ fun main() {
734747

735748
<!--- TEST -->
736749

737-
### Specifying serializer on a property
750+
### Specifying a serializer on a property
738751

739752
When a property of a non-serializable class, like `Date`, is serialized as part of a serializable class we must supply
740753
its serializer or the code will not compile. This is accomplished using the [`@Serializable`][Serializable] annotation on the property.
@@ -774,7 +787,7 @@ The `stableReleaseDate` property is serialized with the serialization strategy t
774787

775788
<!--- TEST -->
776789

777-
### Specifying serializer for a particular type
790+
### Specifying a serializer for a particular type
778791

779792
[`@Serializable`][Serializable] annotation can also be applied directly to the types.
780793
This is handy when a class that requires a custom serializer, such as `Date`, happens to be a generic type argument.
@@ -854,7 +867,7 @@ fun main() {
854867

855868
<!--- TEST -->
856869

857-
### Specifying serializer globally using typealias
870+
### Specifying a serializer globally using a typealias
858871

859872
kotlinx.serialization tends to be the always-explicit framework when it comes to serialization strategies: normally,
860873
they should be explicitly mentioned in `@Serializable` annotation. Therefore, we do not provide any kind of global serializer
@@ -1103,7 +1116,7 @@ class ProgrammingLanguage(
11031116
To provide a context, we define a [SerializersModule] instance that describes which serializers shall be used
11041117
at run-time to serialize which contextually-serializable classes. This is done using the
11051118
[SerializersModule {}][SerializersModule()] builder function, which provides the [SerializersModuleBuilder] DSL to
1106-
register serializers. In the below example we use the [contextual][_contextual] function with the serializer. The corresponding
1119+
register serializers. In the example below we use the [contextual][_contextual] function with the serializer. The corresponding
11071120
class this serializer is defined for is fetched automatically via the `reified` type parameter.
11081121
11091122
```kotlin
@@ -1201,7 +1214,7 @@ This gets all the `Project` properties serialized:
12011214

12021215
<!--- TEST -->
12031216

1204-
### External serialization uses properties
1217+
### External serialization uses properties
12051218

12061219
As we saw earlier, the regular `@Serializable` annotation creates a serializer so that
12071220
[Backing fields are serialized](basic-serialization.md#backing-fields-are-serialized). _External_ serialization using

0 commit comments

Comments
 (0)