Skip to content

Commit 1675987

Browse files
authored
Deprecate SerializersModuleCollector.polymorphicDefault and PolymorphicModuleBuilder.default (#2076)
Replaced with default polymorphicDefaultDeserializer and defaultDeserializer respectively. Remove experimentality from SerializersModuleCollector.polymorphicDefaultSerializer. This is a follow-up for #1686 — finishing migration path
1 parent 95d5b4f commit 1675987

File tree

3 files changed

+52
-33
lines changed

3 files changed

+52
-33
lines changed

core/commonMain/src/kotlinx/serialization/modules/PolymorphicModuleBuilder.kt

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,19 @@ public class PolymorphicModuleBuilder<in Base : Any> @PublishedApi internal cons
3434
* Adds a default serializers provider associated with the given [baseClass] to the resulting module.
3535
* [defaultDeserializerProvider] is invoked when no polymorphic serializers associated with the `className`
3636
* were found. `className` could be `null` for formats that support nullable class discriminators
37-
* (currently only [Json] with [useArrayPolymorphism][JsonBuilder.useArrayPolymorphism] set to `false`)
37+
* (currently only `Json` with `JsonBuilder.useArrayPolymorphism` set to `false`)
38+
*
39+
* Default deserializers provider affects only deserialization process. To affect serialization process, use
40+
* [SerializersModuleBuilder.polymorphicDefaultSerializer].
3841
*
3942
* [defaultDeserializerProvider] can be stateful and lookup a serializer for the missing type dynamically.
4043
*
4144
* Typically, if the class is not registered in advance, it is not possible to know the structure of the unknown
4245
* type and have a precise serializer, so the default serializer has limited capabilities.
43-
* To have a structural access to the unknown data, it is recommended to use [JsonTransformingSerializer]
44-
* or [JsonContentPolymorphicSerializer] classes.
46+
* If you're using `Json` format, you can get a structural access to the unknown data using `JsonContentPolymorphicSerializer`.
4547
*
46-
* Default deserializers provider affects only deserialization process.
48+
* @see SerializersModuleBuilder.polymorphicDefaultSerializer
4749
*/
48-
@ExperimentalSerializationApi
4950
public fun defaultDeserializer(defaultDeserializerProvider: (className: String?) -> DeserializationStrategy<Base>?) {
5051
require(this.defaultDeserializerProvider == null) {
5152
"Default deserializer provider is already registered for class $baseClass: ${this.defaultDeserializerProvider}"
@@ -55,26 +56,27 @@ public class PolymorphicModuleBuilder<in Base : Any> @PublishedApi internal cons
5556

5657
/**
5758
* Adds a default deserializers provider associated with the given [baseClass] to the resulting module.
59+
* This function affect only deserialization process. To avoid confusion, it was deprecated and replaced with [defaultDeserializer].
60+
* To affect serialization process, use [SerializersModuleBuilder.polymorphicDefaultSerializer].
61+
*
5862
* [defaultSerializerProvider] is invoked when no polymorphic serializers associated with the `className`
5963
* were found. `className` could be `null` for formats that support nullable class discriminators
60-
* (currently only [Json] with [useArrayPolymorphism][JsonBuilder.useArrayPolymorphism] set to `false`)
64+
* (currently only `Json` with `JsonBuilder.useArrayPolymorphism` set to `false`)
6165
*
6266
* [defaultSerializerProvider] can be stateful and lookup a serializer for the missing type dynamically.
6367
*
64-
* [defaultSerializerProvider] is named as such for backwards compatibility reasons; it provides deserializers.
65-
*
6668
* Typically, if the class is not registered in advance, it is not possible to know the structure of the unknown
6769
* type and have a precise serializer, so the default serializer has limited capabilities.
68-
* To have a structural access to the unknown data, it is recommended to use [JsonTransformingSerializer]
69-
* or [JsonContentPolymorphicSerializer] classes.
70-
*
71-
* Default deserializers provider affects only deserialization process. To affect serialization process, use
72-
* [SerializersModuleBuilder.polymorphicDefaultSerializer].
70+
* If you're using `Json` format, you can get a structural access to the unknown data using `JsonContentPolymorphicSerializer`.
7371
*
7472
* @see defaultDeserializer
73+
* @see SerializersModuleBuilder.polymorphicDefaultSerializer
7574
*/
76-
@OptIn(ExperimentalSerializationApi::class)
77-
// TODO: deprecate in 1.4
75+
@Deprecated(
76+
"Deprecated in favor of function with more precise name: defaultDeserializer",
77+
ReplaceWith("defaultDeserializer(defaultSerializerProvider)"),
78+
DeprecationLevel.WARNING // Since 1.5.0. Raise to ERROR in 1.6.0, hide in 1.7.0
79+
)
7880
public fun default(defaultSerializerProvider: (className: String?) -> DeserializationStrategy<Base>?) {
7981
defaultDeserializer(defaultSerializerProvider)
8082
}

core/commonMain/src/kotlinx/serialization/modules/SerializersModuleBuilders.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ public class SerializersModuleBuilder @PublishedApi internal constructor() : Ser
9797

9898
/**
9999
* Adds a default serializers provider associated with the given [baseClass] to the resulting module.
100-
* [defaultSerializerProvider] is invoked when no polymorphic serializers for `value` were found.
100+
* [defaultSerializerProvider] is invoked when no polymorphic serializers for `value` in the scope of [baseClass] were found.
101101
*
102-
* This will not affect deserialization.
102+
* Default serializers provider affects only serialization process. To affect deserialization process, use
103+
* [SerializersModuleBuilder.polymorphicDefaultDeserializer].
104+
*
105+
* [defaultSerializerProvider] can be stateful and lookup a serializer for the missing type dynamically.
103106
*/
104-
@ExperimentalSerializationApi
105107
public override fun <Base : Any> polymorphicDefaultSerializer(
106108
baseClass: KClass<Base>,
107109
defaultSerializerProvider: (value: Base) -> SerializationStrategy<Base>?
@@ -112,14 +114,16 @@ public class SerializersModuleBuilder @PublishedApi internal constructor() : Ser
112114
/**
113115
* Adds a default deserializers provider associated with the given [baseClass] to the resulting module.
114116
* [defaultDeserializerProvider] is invoked when no polymorphic serializers associated with the `className`
115-
* were found. `className` could be `null` for formats that support nullable class discriminators
117+
* in the scope of [baseClass] were found. `className` could be `null` for formats that support nullable class discriminators
116118
* (currently only `Json` with `useArrayPolymorphism` set to `false`).
117119
*
118-
* This will not affect serialization.
120+
* Default deserializers provider affects only deserialization process. To affect serialization process, use
121+
* [SerializersModuleBuilder.polymorphicDefaultSerializer].
122+
*
123+
* [defaultDeserializerProvider] can be stateful and lookup a serializer for the missing type dynamically.
119124
*
120125
* @see PolymorphicModuleBuilder.defaultDeserializer
121126
*/
122-
@ExperimentalSerializationApi
123127
public override fun <Base : Any> polymorphicDefaultDeserializer(
124128
baseClass: KClass<Base>,
125129
defaultDeserializerProvider: (className: String?) -> DeserializationStrategy<Base>?

core/commonMain/src/kotlinx/serialization/modules/SerializersModuleCollector.kt

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,29 @@ public interface SerializersModuleCollector {
4747

4848
/**
4949
* Accept a default serializer provider, associated with the [baseClass] for polymorphic serialization.
50+
* [defaultSerializerProvider] is invoked when no polymorphic serializers for `value` in the scope of [baseClass] were found.
5051
*
51-
* This will not affect deserialization.
52+
* Default serializers provider affects only serialization process. Deserializers are accepted in the
53+
* [SerializersModuleCollector.polymorphicDefaultDeserializer] method.
5254
*
53-
* @see SerializersModuleBuilder.polymorphicDefaultSerializer
54-
* @see PolymorphicModuleBuilder.defaultSerializer
55+
* [defaultSerializerProvider] can be stateful and lookup a serializer for the missing type dynamically.
5556
*/
56-
@ExperimentalSerializationApi
5757
public fun <Base : Any> polymorphicDefaultSerializer(
5858
baseClass: KClass<Base>,
5959
defaultSerializerProvider: (value: Base) -> SerializationStrategy<Base>?
6060
)
6161

6262
/**
6363
* Accept a default deserializer provider, associated with the [baseClass] for polymorphic deserialization.
64+
* [defaultDeserializerProvider] is invoked when no polymorphic serializers associated with the `className`
65+
* in the scope of [baseClass] were found. `className` could be `null` for formats that support nullable class discriminators
66+
* (currently only `Json` with `useArrayPolymorphism` set to `false`).
6467
*
65-
* This will not affect serialization.
68+
* Default deserializers provider affects only deserialization process. Serializers are accepted in the
69+
* [SerializersModuleCollector.polymorphicDefaultSerializer] method.
6670
*
67-
* @see SerializersModuleBuilder.polymorphicDefaultDeserializer
68-
* @see PolymorphicModuleBuilder.defaultDeserializer
71+
* [defaultDeserializerProvider] can be stateful and lookup a serializer for the missing type dynamically.
6972
*/
70-
@ExperimentalSerializationApi
7173
public fun <Base : Any> polymorphicDefaultDeserializer(
7274
baseClass: KClass<Base>,
7375
defaultDeserializerProvider: (className: String?) -> DeserializationStrategy<Base>?
@@ -76,12 +78,23 @@ public interface SerializersModuleCollector {
7678
/**
7779
* Accept a default deserializer provider, associated with the [baseClass] for polymorphic deserialization.
7880
*
79-
* This will not affect serialization.
81+
* This function affect only deserialization process. To avoid confusion, it was deprecated and replaced with [polymorphicDefaultDeserializer].
82+
* To affect serialization process, use [SerializersModuleCollector.polymorphicDefaultSerializer].
8083
*
81-
* @see SerializersModuleBuilder.polymorphicDefaultDeserializer
82-
* @see PolymorphicModuleBuilder.defaultDeserializer
84+
* [defaultDeserializerProvider] is invoked when no polymorphic serializers associated with the `className`
85+
* in the scope of [baseClass] were found. `className` could be `null` for formats that support nullable class discriminators
86+
* (currently only `Json` with `useArrayPolymorphism` set to `false`).
87+
*
88+
* [defaultDeserializerProvider] can be stateful and lookup a serializer for the missing type dynamically.
89+
*
90+
* @see SerializersModuleCollector.polymorphicDefaultDeserializer
91+
* @see SerializersModuleCollector.polymorphicDefaultSerializer
8392
*/
84-
// TODO: deprecate in 1.4
93+
@Deprecated(
94+
"Deprecated in favor of function with more precise name: polymorphicDefaultDeserializer",
95+
ReplaceWith("polymorphicDefaultDeserializer(baseClass, defaultDeserializerProvider)"),
96+
DeprecationLevel.WARNING // Since 1.5.0. Raise to ERROR in 1.6.0, hide in 1.7.0
97+
)
8598
public fun <Base : Any> polymorphicDefault(
8699
baseClass: KClass<Base>,
87100
defaultDeserializerProvider: (className: String?) -> DeserializationStrategy<Base>?

0 commit comments

Comments
 (0)