Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions formats/json/api/kotlinx-serialization-json.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ sealed class kotlinx.serialization.json/Json : kotlinx.serialization/StringForma
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]
final fun parseToJsonElement(kotlin/String): kotlinx.serialization.json/JsonElement // kotlinx.serialization.json/Json.parseToJsonElement|parseToJsonElement(kotlin.String){}[0]
final inline fun <#A1: reified kotlin/Any?> decodeFromString(kotlin/String): #A1 // kotlinx.serialization.json/Json.decodeFromString|decodeFromString(kotlin.String){0§<kotlin.Any?>}[0]
final inline fun <#A1: reified kotlin/Any?> encodeToString(#A1): kotlin/String // kotlinx.serialization.json/Json.encodeToString|encodeToString(0:0){0§<kotlin.Any?>}[0]

final object Default : kotlinx.serialization.json/Json // kotlinx.serialization.json/Json.Default|null[0]
}
Expand Down
58 changes: 42 additions & 16 deletions formats/json/commonMain/src/kotlinx/serialization/json/Json.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,6 @@ public sealed class Json(
}
}

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

/**
* Deserializes the given JSON [string] into a value of type [T] using the given [deserializer].
Expand Down Expand Up @@ -194,6 +178,48 @@ public sealed class Json(
public fun parseToJsonElement(@FormatLanguage("json", "", "") string: String): JsonElement {
return decodeFromString(JsonElementSerializer, string)
}

/**
* Following functions are copied from extensions on StringFormat
* to streamline experience for newcomers, since IDE does not star-import kotlinx.serialization.* automatically
*/

/**
* Serializes the [value] of type [T] into an equivalent JSON using serializer
* retrieved from the reified type parameter.
*
* Example of usage:
* ```
* @Serializable
* class Project(val name: String, val language: String)
*
* val data = Project("kotlinx.serialization", "Kotlin")
*
* // Prints {"name":"kotlinx.serialization","language":"Kotlin"}
* println(Json.encodeToString(data))
* ```
*
* @throws [SerializationException] if the given value cannot be serialized to JSON.
*/
public inline fun <reified T> encodeToString(value: T): String =
encodeToString(serializersModule.serializer(), value)

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

/**
Expand Down