Skip to content

Commit d266e05

Browse files
authored
Add inline reified version of encodeToString as a Json member to to streamline experience for newcomers. (#2853)
decodeFromString was already there for @FormatLanguage reasons. Fixes #2850
1 parent 38977b3 commit d266e05

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

formats/json/api/kotlinx-serialization-json.klib.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ sealed class kotlinx.serialization.json/Json : kotlinx.serialization/StringForma
308308
final fun <#A1: kotlin/Any?> encodeToString(kotlinx.serialization/SerializationStrategy<#A1>, #A1): kotlin/String // kotlinx.serialization.json/Json.encodeToString|encodeToString(kotlinx.serialization.SerializationStrategy<0:0>;0:0){0§<kotlin.Any?>}[0]
309309
final fun parseToJsonElement(kotlin/String): kotlinx.serialization.json/JsonElement // kotlinx.serialization.json/Json.parseToJsonElement|parseToJsonElement(kotlin.String){}[0]
310310
final inline fun <#A1: reified kotlin/Any?> decodeFromString(kotlin/String): #A1 // kotlinx.serialization.json/Json.decodeFromString|decodeFromString(kotlin.String){0§<kotlin.Any?>}[0]
311+
final inline fun <#A1: reified kotlin/Any?> encodeToString(#A1): kotlin/String // kotlinx.serialization.json/Json.encodeToString|encodeToString(0:0){0§<kotlin.Any?>}[0]
311312

312313
final object Default : kotlinx.serialization.json/Json // kotlinx.serialization.json/Json.Default|null[0]
313314
}

formats/json/commonMain/src/kotlinx/serialization/json/Json.kt

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,6 @@ public sealed class Json(
129129
}
130130
}
131131

132-
/**
133-
* Decodes and deserializes the given JSON [string] to the value of type [T] using deserializer
134-
* retrieved from the reified type parameter.
135-
* Example:
136-
* ```
137-
* @Serializable
138-
* data class Project(val name: String, val language: String)
139-
* // Project(name=kotlinx.serialization, language=Kotlin)
140-
* println(Json.decodeFromString<Project>("""{"name":"kotlinx.serialization","language":"Kotlin"}"""))
141-
* ```
142-
*
143-
* @throws SerializationException in case of any decoding-specific error
144-
* @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
145-
*/
146-
public inline fun <reified T> decodeFromString(@FormatLanguage("json", "", "") string: String): T =
147-
decodeFromString(serializersModule.serializer(), string)
148132

149133
/**
150134
* Deserializes the given JSON [string] into a value of type [T] using the given [deserializer].
@@ -194,6 +178,48 @@ public sealed class Json(
194178
public fun parseToJsonElement(@FormatLanguage("json", "", "") string: String): JsonElement {
195179
return decodeFromString(JsonElementSerializer, string)
196180
}
181+
182+
/**
183+
* Following functions are copied from extensions on StringFormat
184+
* to streamline experience for newcomers, since IDE does not star-import kotlinx.serialization.* automatically
185+
*/
186+
187+
/**
188+
* Serializes the [value] of type [T] into an equivalent JSON using serializer
189+
* retrieved from the reified type parameter.
190+
*
191+
* Example of usage:
192+
* ```
193+
* @Serializable
194+
* class Project(val name: String, val language: String)
195+
*
196+
* val data = Project("kotlinx.serialization", "Kotlin")
197+
*
198+
* // Prints {"name":"kotlinx.serialization","language":"Kotlin"}
199+
* println(Json.encodeToString(data))
200+
* ```
201+
*
202+
* @throws [SerializationException] if the given value cannot be serialized to JSON.
203+
*/
204+
public inline fun <reified T> encodeToString(value: T): String =
205+
encodeToString(serializersModule.serializer(), value)
206+
207+
/**
208+
* Decodes and deserializes the given JSON [string] to the value of type [T] using deserializer
209+
* retrieved from the reified type parameter.
210+
* Example:
211+
* ```
212+
* @Serializable
213+
* data class Project(val name: String, val language: String)
214+
* // Project(name=kotlinx.serialization, language=Kotlin)
215+
* println(Json.decodeFromString<Project>("""{"name":"kotlinx.serialization","language":"Kotlin"}"""))
216+
* ```
217+
*
218+
* @throws SerializationException in case of any decoding-specific error
219+
* @throws IllegalArgumentException if the decoded input is not a valid instance of [T]
220+
*/
221+
public inline fun <reified T> decodeFromString(@FormatLanguage("json", "", "") string: String): T =
222+
decodeFromString(serializersModule.serializer(), string)
197223
}
198224

199225
/**

0 commit comments

Comments
 (0)