-
Notifications
You must be signed in to change notification settings - Fork 666
feat: adding the Serialize classes page #3097
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?
Conversation
sandwwraith
left a comment
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.
.
| @@ -0,0 +1,505 @@ | |||
| [//]: # (title: Serialize classes) | |||
|
|
|||
| The [`@Serializable`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serializable/) annotation in Kotlin enables the serialization of all properties in classes defined by the primary constructor. | |||
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.
nice, good point - changed it to properties with backing fields here as well 👍
| @@ -0,0 +1,505 @@ | |||
| [//]: # (title: Serialize classes) | |||
|
|
|||
| The [`@Serializable`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serializable/) annotation in Kotlin enables the serialization of all properties in classes defined by the primary constructor. | |||
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.
nice, good point - changed it to properties with backing fields here as well 👍
sandwwraith
left a comment
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 do not know whether we'll finish whole review before mid-December (I expect next release of serialization there), but we can leave opt-in note for now and delete it later.
Besides that, please fix "Defines..." wording and my other comment and we're good to go
sarahhaggarty
left a comment
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.
Looks great! I have some suggestions for you to consider and a couple clarification questions :)
| @@ -0,0 +1,516 @@ | |||
| [//]: # (title: Serialize classes) | |||
|
|
|||
| The [`@Serializable`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serializable/) annotation enables the default serialization of all class properties with backing fields. | |||
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.
| The [`@Serializable`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serializable/) annotation enables the default serialization of all class properties with backing fields. | |
| By default, the [`@Serializable`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serializable/) annotation serializes all class properties that have backing fields. |
What do you think of this version?
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'm not sure it is good to imply that the annotation serializes. While it is "simpler" it is also misleading, potentially leading to misunderstandings. What about stating it indirectly:
"By default, the @Serializable annotation causes all class properties that have backing fields to be serialized."
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 also think I prefer the original phrasing here — it captures what happens a bit better (as @pdvrieze says, it doesn't perform serialization on its own - it essentially generates the default serializer under the hood, which enables the serialization of class properties with backing fields )
|
|
||
| The [`@Serializable`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serializable/) annotation enables the default serialization of all class properties with backing fields. | ||
| You can further customize this behavior to fit your specific needs. | ||
| This page covers various serialization techniques, showing you how to specify which properties are serialized and how the serialization process is managed. |
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.
| This page covers various serialization techniques, showing you how to specify which properties are serialized and how the serialization process is managed. | |
| This page covers various serialization techniques, showing you how to specify which properties are serialized, and how the serialization process is managed. |
Is the process managed by the library?
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.
The overall process starts with the format (which could be Json or another library provided format), and then parts of it are determined by the actual serializers (either plugin generated or custom). The core library does not actually do much in terms of the process (except define a way in which it should happen). In other words, there is no serialization "loop" that formats plug in to, but rather the format manages/controls the process as restricted/defined by the core library.
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.
and how the serialization process is managed
I think what implied here is that library manages the process, and you can manage it by using different things from library, such as @Transient annotation
| ``` | ||
| {kotlin-runnable="true" id="serialize-annotation-example"} | ||
|
|
||
| ### Serialization of class references |
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.
What do you think about "Serialize class references" as a header? So that the header style matches the others on the page?
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.
This header (and the ones below) provide specifics as to how specific kinds of elements (attributes) are handled, but this is not an active user thing. As such the verb "serialize" is misleading.
| > | ||
| {style="tip"} | ||
|
|
||
| ### Serialization of repeated object references |
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.
What do you think about "Serialize repeated object references" as a header? So that the header style matches the others on the page?
|
|
||
| ## Customize class serialization | ||
|
|
||
| Kotlin Serialization provides several ways to modify how classes are serialized. |
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.
What do you think about "control" instead of "modify"?
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.
As the section leaves out custom serializers (as a reference to more details for this "advanced" option), it is more a matter of adjustment/modification (I would prefer adjustment, but it might be a bit of a "hard" word).
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 also prefer modify here slightly (also this way we can avoid repeating control from the following sentence)
|
|
||
| You can customize these names, called _serial names_, | ||
| with the [`@SerialName`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serial-name/) annotation. | ||
| Use it to make a property name more descriptive in the serialized output: |
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.
Here you say that you should use it to make the property more descriptive. But in the example you abbreviate it. So maybe we should change the wording or the example?
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.
And in reality the key function is compatibility with other serialization systems/existing formats. I think the wording needs to be adjusted. Maybe too advanced would be that it may also be used to ensure a property's serialized name is stable under refactoring/renaming of the underlying kotlin property.
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 made the example match this (ensured that it's it the other way around to be more descriptive)
|
|
||
| ### Validate data in the primary constructor | ||
|
|
||
| After deserialization, the `kotlinx.serialization` plugin runs the class's initializer blocks, |
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.
Could you add a link here to the Kotlin section about initializer blocks? https://kotlinlang.org/docs/classes.html#initializer-blocks
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.
good idea 💯
| You can exclude a property from serialization with the [`@Transient`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-transient/) annotation. | ||
| Transient properties must have a default value. | ||
|
|
||
| If you explicitly specify a value for a transient property in the input, even if it matches the default value, a `JsonDecodingException` is thrown: |
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.
Again, I'm not sure what "input" is referring to exactly here 🤔
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.
The input here is the Json formatted data that is being deserialized (converted into Kotlin datastructures). What is actually going on here is that the serializer does not record transient properties in its descriptor, so when it encounters the property name it is treated as any other unknown attribute, which by default is to throw an exception (there is an option to ignore unknown fields).
This is the fourth part of the Kotlin Serialization rewrite task.
Related YouTract ticket is here: KT-81889 [Docs] Create a Serialize classes page for kotlinx.serialization