@@ -49,8 +49,6 @@ import kotlin.reflect.*
49
49
* }
50
50
* ```
51
51
*/
52
- @Suppress(" FunctionName" )
53
- @OptIn(ExperimentalSerializationApi ::class )
54
52
public fun buildClassSerialDescriptor (
55
53
serialName : String ,
56
54
vararg typeParameters : SerialDescriptor ,
@@ -69,7 +67,7 @@ public fun buildClassSerialDescriptor(
69
67
}
70
68
71
69
/* *
72
- * Factory to create a trivial primitive descriptors.
70
+ * Factory to create trivial primitive descriptors. [serialName] must be non-blank and unique .
73
71
* Primitive descriptors should be used when the serialized form of the data has a primitive form, for example:
74
72
* ```
75
73
* object LongAsStringSerializer : KSerializer<Long> {
@@ -86,16 +84,16 @@ public fun buildClassSerialDescriptor(
86
84
* }
87
85
* ```
88
86
*/
87
+ @Suppress(" FunctionName" )
89
88
public fun PrimitiveSerialDescriptor (serialName : String , kind : PrimitiveKind ): SerialDescriptor {
90
89
require(serialName.isNotBlank()) { " Blank serial names are prohibited" }
91
90
return PrimitiveDescriptorSafe (serialName, kind)
92
91
}
93
92
94
93
/* *
95
94
* Factory to create a new descriptor that is identical to [original] except that the name is equal to [serialName].
96
- * Should be used when you want to serialize a type as another non-primitive type.
97
- * Don't use this if you want to serialize a type as a primitive value, use [PrimitiveSerialDescriptor] instead.
98
- *
95
+ * Usually used when you want to serialize a type as another type, delegating implementation of `serialize` and `deserialize`.
96
+ *
99
97
* Example:
100
98
* ```
101
99
* @Serializable(CustomSerializer::class)
@@ -115,27 +113,24 @@ public fun PrimitiveSerialDescriptor(serialName: String, kind: PrimitiveKind): S
115
113
* }
116
114
* ```
117
115
*/
118
- @ExperimentalSerializationApi
119
116
public fun SerialDescriptor (serialName : String , original : SerialDescriptor ): SerialDescriptor {
120
117
require(serialName.isNotBlank()) { " Blank serial names are prohibited" }
121
- require(original.kind !is PrimitiveKind ) { " For primitive descriptors please use 'PrimitiveSerialDescriptor' instead" }
122
118
require(serialName != original.serialName) { " The name of the wrapped descriptor ($serialName ) cannot be the same as the name of the original descriptor (${original.serialName} )" }
123
-
119
+ if (original.kind is PrimitiveKind ) checkNameIsNotAPrimitive(serialName)
120
+
124
121
return WrappedSerialDescriptor (serialName, original)
125
122
}
126
123
127
- @OptIn(ExperimentalSerializationApi ::class )
128
124
internal class WrappedSerialDescriptor (override val serialName : String , original : SerialDescriptor ) : SerialDescriptor by original
129
125
130
126
/* *
131
127
* An unsafe alternative to [buildClassSerialDescriptor] that supports an arbitrary [SerialKind].
132
128
* This function is left public only for migration of pre-release users and is not intended to be used
133
- * as generally- safe and stable mechanism. Beware that it can produce inconsistent or non spec-compliant instances.
129
+ * as a generally safe and stable mechanism. Beware that it can produce inconsistent or non- spec-compliant instances.
134
130
*
135
- * If you end up using this builder, please file an issue with your use-case in kotlinx.serialization issue tracker.
131
+ * If you end up using this builder, please file an issue with your use-case to the kotlinx.serialization issue tracker.
136
132
*/
137
133
@InternalSerializationApi
138
- @OptIn(ExperimentalSerializationApi ::class )
139
134
public fun buildSerialDescriptor (
140
135
serialName : String ,
141
136
kind : SerialKind ,
@@ -152,14 +147,32 @@ public fun buildSerialDescriptor(
152
147
153
148
/* *
154
149
* Retrieves descriptor of type [T] using reified [serializer] function.
150
+ *
151
+ * Example:
152
+ * ```
153
+ * serialDescriptor<List<String>>() // Returns kotlin.collections.ArrayList(PrimitiveDescriptor(kotlin.String))
154
+ * ```
155
155
*/
156
156
public inline fun <reified T > serialDescriptor (): SerialDescriptor = serializer<T >().descriptor
157
157
158
158
/* *
159
- * Retrieves descriptor of type associated with the given [KType][type]
159
+ * Retrieves descriptor of a type associated with the given [KType][type].
160
+ *
161
+ * Example:
162
+ * ```
163
+ * val type = typeOf<List<String>>()
164
+ *
165
+ * serialDescriptor(type) // Returns kotlin.collections.ArrayList(PrimitiveDescriptor(kotlin.String))
166
+ * ```
160
167
*/
161
168
public fun serialDescriptor (type : KType ): SerialDescriptor = serializer(type).descriptor
162
169
170
+ /* The rest of the functions intentionally left experimental for later stabilization
171
+ It is unclear whether they should be left as-is,
172
+ or moved to ClassSerialDescriptorBuilder (because this is the main place for them to be used),
173
+ or simply deprecated in favor of ListSerializer(Element.serializer()).descriptor
174
+ */
175
+
163
176
/* *
164
177
* Creates a descriptor for the type `List<T>` where `T` is the type associated with [elementDescriptor].
165
178
*/
@@ -227,9 +240,10 @@ public val SerialDescriptor.nullable: SerialDescriptor
227
240
* Returns non-nullable serial descriptor for the type if this descriptor has been auto-generated (plugin
228
241
* generated descriptors) or created with `.nullable` extension on a descriptor or serializer.
229
242
*
230
- * Otherwise, returns this.
243
+ * Otherwise, returns ` this` .
231
244
*
232
- * It may return nullable descriptor if this descriptor has been created manually as nullable by directly implementing SerialDescriptor interface.
245
+ * It may return a nullable descriptor
246
+ * if `this` descriptor has been created manually as nullable by directly implementing SerialDescriptor interface.
233
247
*
234
248
* @see SerialDescriptor.nullable
235
249
* @see KSerializer.nullable
0 commit comments