7
7
package kotlinx.serialization
8
8
9
9
import kotlinx.serialization.descriptors.*
10
+ import kotlinx.serialization.encoding.*
10
11
import kotlin.reflect.*
11
12
12
13
/* *
@@ -132,6 +133,51 @@ public annotation class Required
132
133
// @Retention(AnnotationRetention.RUNTIME) still runtime, but KT-41082
133
134
public annotation class Transient
134
135
136
+ /* *
137
+ * Controls whether the target property is serialized when its value is equal to a default value,
138
+ * regardless of the format settings.
139
+ * Does not affect decoding and deserialization process.
140
+ *
141
+ * Example of usage:
142
+ * ```
143
+ * @Serializable
144
+ * data class Foo(
145
+ * @EncodeDefault(ALWAYS) val a: Int = 42,
146
+ * @EncodeDefault(NEVER) val b: Int = 43,
147
+ * val c: Int = 44
148
+ * )
149
+ *
150
+ * Json { encodeDefaults = false }.encodeToString((Foo()) // {"a": 42}
151
+ * Json { encodeDefaults = true }.encodeToString((Foo()) // {"a": 42, "c":44}
152
+ * ```
153
+ *
154
+ * @see EncodeDefault.Mode.ALWAYS
155
+ * @see EncodeDefault.Mode.NEVER
156
+ */
157
+ @Target(AnnotationTarget .PROPERTY )
158
+ @ExperimentalSerializationApi
159
+ public annotation class EncodeDefault (val mode : Mode = Mode .ALWAYS ) {
160
+ /* *
161
+ * Strategy for the [EncodeDefault] annotation.
162
+ */
163
+ @ExperimentalSerializationApi
164
+ public enum class Mode {
165
+ /* *
166
+ * Configures serializer to always encode the property, even if its value is equal to its default.
167
+ * For annotated properties, format settings are not taken into account and
168
+ * [CompositeEncoder.shouldEncodeElementDefault] is not invoked.
169
+ */
170
+ ALWAYS ,
171
+
172
+ /* *
173
+ * Configures serializer not to encode the property if its value is equal to its default.
174
+ * For annotated properties, format settings are not taken into account and
175
+ * [CompositeEncoder.shouldEncodeElementDefault] is not invoked.
176
+ */
177
+ NEVER
178
+ }
179
+ }
180
+
135
181
/* *
136
182
* Meta-annotation that commands the compiler plugin to handle the annotation as serialization-specific.
137
183
* Serialization-specific annotations are preserved in the [SerialDescriptor] and can be retrieved
@@ -143,23 +189,23 @@ public annotation class Transient
143
189
public annotation class SerialInfo
144
190
145
191
/* *
146
- * Instructs the plugin to use [ContextSerializer ] on a given property or type.
192
+ * Instructs the plugin to use [ContextualSerializer ] on a given property or type.
147
193
* Context serializer is usually used when serializer for type can only be found in runtime.
148
- * It is also possible to apply [ContextSerializer ] to every property of the given type,
194
+ * It is also possible to apply [ContextualSerializer ] to every property of the given type,
149
195
* using file-level [UseContextualSerialization] annotation.
150
196
*
151
- * @see ContextSerializer
197
+ * @see ContextualSerializer
152
198
* @see UseContextualSerialization
153
199
*/
154
200
@Target(AnnotationTarget .PROPERTY , AnnotationTarget .TYPE )
155
201
@Retention(AnnotationRetention .BINARY )
156
202
public annotation class Contextual
157
203
158
204
/* *
159
- * Instructs the plugin to use [ContextSerializer ] for every type in the current file that is listed in the [forClasses].
205
+ * Instructs the plugin to use [ContextualSerializer ] for every type in the current file that is listed in the [forClasses].
160
206
*
161
207
* @see Contextual
162
- * @see ContextSerializer
208
+ * @see ContextualSerializer
163
209
*/
164
210
@Target(AnnotationTarget .FILE )
165
211
@Retention(AnnotationRetention .BINARY )
0 commit comments