Kotlinx.serialization part6

Peter Nagy
2 min readJun 19, 2021

This series is about Kotlinx serialization library.

In the Part1 I wrote about the basic usage of the Kotlinx.serialization.

In the Part2 I wrote about how can we configure the Kotlinx.serilaization.

In the Part3 I wrote about Custom serialisers and data classes.

In the Part4 I wrote an exact example how can we use custom serialiser with 3rd party classes.

In the Part5 I wrote about how to handle an optional field.

Now I want to write about how can we use a JSON object after when we deserialise our JSON string.

When we are using serialisation at network calls very often we serialise and deserialise Java / Kotlin objects. But sometimes we need the raw JSON string as an input for a 3rd party lib or we want to store it.

I wrote JSON string can we use it like a string ?

Answer is NO!!

Why ?
Because it will use quotes in the string:

{"testName": "test", 
"testObject": {
"testSubName": "test2,
"testNumber": 123
}
}

We can see it is not a real string. If our testObject is String in that case we will got a big exception when we deserialise this JSON (exception will be: string expected however object begin or something…).

Solution1:

If we want to store it like a String object, then we should send it like a String. However in that case all quotes will be quote + escape character.

Solution2:

We can use JSONObject or JSONElement.

@Serializable
data class JsonTest(val fruit: String, val rawJSON: JsonElement)
val jsonString = "{\"fruit\": \"apple\", \"rawJSON\": {\"number\": 48.321659646260812, \"text\": \"Hello\"}}"val result = jsonSerializer.decodeFromString<JsonTest>(jsonString)

After it when we check the result.rawJSON -> {“number”:48.321659646260812,”text”:”Hello”}

So first of all we can be happy we can handle a raw JSON and we can save and store raw JSON string or pass it to a 3rd party app.

Ok let's see it in other aspect. When we have a JSON string we can decode and encode and decode again. What is we expect, it must be same.

@Serializable
data class JsonTest(val fruit: String, val rawJSON: JsonElement)
val jsonString = "{\"fruit\": \"apple\", \"rawJSON\": {\"number\": 48.321659646260812, \"text\": \"Hello\"}}"

val jsonSerializer = Json { }
val result = jsonSerializer.decodeFromString<JsonTest>(jsonString)
val text = jsonSerializer.encodeToJsonElement(result).toString()
val result2 = jsonSerializer.decodeFromString<JsonTest>(text)

rawJSON in result = {“number”:48.321659646260812,”text”:”Hello”}

however rawJSON in result2 = {“number”:48.32165964626081,”text”:”Hello”}

What was happened ? During encoding Kotlinx.serializer was parsed the objects and by default a floating point number is double in that case.

If we want to save JSON strings and later send to a backend and data could have floating point numbers like BigDecimal then it is better to store data like an object (Room, SQLite, etc) instead of JSON.

Thanks for reading my article! If you found this post useful? Kindly tap the 👏 button below and follow :) .

--

--