-
Notifications
You must be signed in to change notification settings - Fork 660
Doc rewrite update 3 - Serialize built in types #3082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: doc-restructuring-master
Are you sure you want to change the base?
Doc rewrite update 3 - Serialize built in types #3082
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added some comments. One thing to be clear about is the distinction between json and serialization in general. Json as example is good, but I would avoid giving the impression that serialization is json-only.
When you serialize Kotlin `Long` values to JSON, JavaScript's native number type can't represent the full range of a Kotlin `Long` type, | ||
leading to precision loss. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worthwhile to note that this is a limitation of JS, not Json that is effectively arbitrary precision? It is somewhat implied by the explanation below, but can be confusing in the distinction.
### Collections | ||
|
||
Kotlin Serialization supports collection types such as [`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/), [`Set`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-set/), and [`Map`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/). | ||
Lists and sets are serialized as JSON arrays, and maps are represented as JSON objects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe explicitly mention that this is specific to the Json format. What about:
The way lists and sets are serialized is format specific, but generally mapped to common structures. For example, in Json lists and sets are serialized as ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
> JSON doesn't natively support complex or composite keys. | ||
> To encode structured objects as map keys, see [Encode structured map keys](serialization-json-configuration.md#encode-structured-map-keys). | ||
> | ||
{style="note"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe mention that other formats may support this in different ways.
|
||
## Duration | ||
|
||
Kotlin's [`Duration`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.time/-duration/) class is serialized to a JSON string using the ISO-8601-2 format: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, this is not json specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, maybe just say "string" instead of "JSON string"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comments
@@ -0,0 +1,378 @@ | |||
[//]: # (title: Serialize built-in types) | |||
|
|||
The Kotlin serialization library supports various built-in types, including basic types such as primitives and strings, composite types, and several standard library classes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've mentioned 'composite types' without explaining what they are. I suggest just leaving 'basic types such as primitives and strings, as well as certain standard library classes'
## Basic types | ||
|
||
Kotlin serialization provides built-in serializers for types that are represented as a single value in serialized data. | ||
This includes, primitives, strings, and enums. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary ,
after 'includes' ?
//sampleStart | ||
@Serializable | ||
class Data( | ||
@Serializable(with=LongAsStringSerializer::class) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer removing explicit with
: @Serializable(LongAsStringSerializer::class)
> For more information, see [Specify serializers for a file](third-party-classes.md#specify-serializers-for-a-file). | ||
> | ||
{style="tip"} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might worth it to add section aboun unsigned numbers here as well, given they're stable in stdlib and IR compiler has been default for years (https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/value-classes.md#unsigned-types-support-json-only). Just don't forget mentioning
Unsigned types are output as unsigned only in JSON format. Other formats such as ProtoBuf and CBOR use built-in serializers that use an underlying signed representation for unsigned types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stating that unsigned is only unsigned in Json is inaccurate, other formats support unsigned directly (and at full range) - maybe something that the protobuf serializer can be extended to do.
``` | ||
{kotlin-runnable="true"} | ||
|
||
## Composite types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say we don't need this 'composite types' notion until maybe the chapter about custom serializers. So you can replace this with 'Standard library types'
Alternatively, you should start with the definition of composite types: "Every type which is composed out of the basic types dicussed above, is called composite".
Besides, one can always write a serializer that represents any complex type as string. So I'm in favor of removing 'composite types' notion for now.
|
||
### Collections | ||
|
||
Kotlin Serialization supports collection types such as [`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/), [`Set`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-set/), and [`Map`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rephrase this slightly. The thing is, we also support concrete implementations (e.g. ArrayList
and LinkedHashSet
), so our users should not have impression that only basic intrefaces are supported. Mutable counterparts should be mentioned as well.
And btw, regular Array
s and primitive Int/Long/...Array
too.
// [{"name":"kotlinx.serialization"},{"name":"kotlinx.coroutines"}] | ||
} | ||
//sampleEnd | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Sets have their own section, maybe it is worth mentioning that
Duplicate entries in Sets do not cause exceptions on deserialization by default and behavior on duplicates is implementation-defined.
|
||
#### Deserialization behavior of collections | ||
|
||
Kotlin uses the declared type to deserialize JSON. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think original
During deserialization, the type of the resulting object is determined by the static type that was specified in the source code—either as the type of the property or as the type parameter of the decoding function.
explains what happens better. It also answers the question "What happens if I specify ArrayList or LinkedList"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although you can mention this in the beginning of the section, add the suggested tip to the Set section and drop this section completely.
|
||
## Duration | ||
|
||
Kotlin's [`Duration`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.time/-duration/) class is serialized to a JSON string using the ISO-8601-2 format: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, maybe just say "string" instead of "JSON string"?
> | ||
{style="tip"} | ||
|
||
## Duration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also have two new serializers for Instant
btw (#2945). They probably should be mentioned here (and requirement for Kotlin 2.2)
This is the third part of the Kotlin Serialization rewrite.
Related YouTract ticket is: KT-80887 [Docs] Create a serialize built-in classes guide for kotlinx.serialization