Close response properly in core utility parseAs() (#9591)

This commit is contained in:
stevenyomi 2025-07-08 17:30:57 +00:00 committed by Draff
parent 603fd58fff
commit 2142cb32c2
Signed by: Draff
GPG Key ID: E8A89F3211677653

View File

@ -10,19 +10,19 @@ import uy.kohesive.injekt.injectLazy
val jsonInstance: Json by injectLazy()
/**
* Parses and serializes the String as the type <T>.
* Parses JSON string into an object of type [T].
*/
inline fun <reified T> String.parseAs(json: Json = jsonInstance): T =
json.decodeFromString(this)
/**
* Parse and serialize the response body as the type <T>.
* Parses the response body into an object of type [T].
*/
inline fun <reified T> Response.parseAs(json: Json = jsonInstance): T =
json.decodeFromStream(body.byteStream())
use { json.decodeFromStream(body.byteStream()) }
/**
* Serializes the object to a JSON String.
* Serializes the object to a JSON string.
*/
inline fun <reified T> T.toJsonString(json: Json = jsonInstance): String =
json.encodeToString(this)