Skip to content

Commit afdc830

Browse files
authored
Stabilize @EncodeDefault annotation and its modes. (#3106)
It is widely popular and de-facto is a stable API.
1 parent 6488fef commit afdc830

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,10 @@ public annotation class Transient
199199
*/
200200
@MustBeDocumented
201201
@Target(AnnotationTarget.PROPERTY)
202-
@ExperimentalSerializationApi
203202
public annotation class EncodeDefault(val mode: Mode = Mode.ALWAYS) {
204203
/**
205204
* Strategy for the [EncodeDefault] annotation.
206205
*/
207-
@ExperimentalSerializationApi
208206
public enum class Mode {
209207
/**
210208
* Configures serializer to always encode the property, even if its value is equal to its default.

docs/basic-serialization.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,13 @@ See JSON's [Encoding defaults](json.md#encoding-defaults) section on how this be
448448
Additionally, this behavior can be controlled without taking format settings into account.
449449
For that purposes, [EncodeDefault] annotation can be used:
450450

451+
<!--- INCLUDE
452+
import kotlinx.serialization.EncodeDefault.Mode.NEVER
453+
-->
454+
455+
451456
```kotlin
452457
@Serializable
453-
@OptIn(ExperimentalSerializationApi::class) // EncodeDefault is an experimental annotation for now
454458
data class Project(
455459
val name: String,
456460
@EncodeDefault val language: String = "Kotlin"
@@ -461,12 +465,10 @@ This annotation instructs the framework to always serialize property, regardless
461465
It's also possible to tweak it into the opposite behavior using [EncodeDefault.Mode] parameter:
462466

463467
```kotlin
464-
465468
@Serializable
466-
@OptIn(ExperimentalSerializationApi::class) // EncodeDefault is an experimental annotation for now
467469
data class User(
468470
val name: String,
469-
@EncodeDefault(EncodeDefault.Mode.NEVER) val projects: List<Project> = emptyList()
471+
@EncodeDefault(NEVER) val projects: List<Project> = emptyList()
470472
)
471473

472474
fun main() {

guide/example/example-classes-10.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ package example.exampleClasses10
44
import kotlinx.serialization.*
55
import kotlinx.serialization.json.*
66

7+
import kotlinx.serialization.EncodeDefault.Mode.NEVER
8+
79
@Serializable
8-
@OptIn(ExperimentalSerializationApi::class) // EncodeDefault is an experimental annotation for now
910
data class Project(
1011
val name: String,
1112
@EncodeDefault val language: String = "Kotlin"
1213
)
1314

14-
1515
@Serializable
16-
@OptIn(ExperimentalSerializationApi::class) // EncodeDefault is an experimental annotation for now
1716
data class User(
1817
val name: String,
19-
@EncodeDefault(EncodeDefault.Mode.NEVER) val projects: List<Project> = emptyList()
18+
@EncodeDefault(NEVER) val projects: List<Project> = emptyList()
2019
)
2120

2221
fun main() {

0 commit comments

Comments
 (0)