Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Json to Map<String,String> kotlin multiplatform

My resialized is "{"2":"Hello","Tesst":"Value"}"

I tried to parse this string to Map<String,String>

            val resialized = readFile(createStorageDirectoryPath(getManifestFilePath()), MANIFEST_FILE_NAME, errorOut)
                manifest = Json.decodeFromString(/*serializer*/, resialized)

How I create a serializer for Map<String,String>

like image 783
Anhdevit Avatar asked Jul 21 '26 07:07

Anhdevit


1 Answers

You can use an other version of decodeFromString which will take care of deserializer by itself.

import kotlinx.serialization.decodeFromString

val res = Json.decodeFromString<Map<String, String>>("{\"2\":\"Hello\",\"Tesst\":\"Value\"}")

It's marked with ExperimentalSerializationApi, but I had no problems using it for the last year. This method is recommended by documentation.

like image 93
Philip Dukhov Avatar answered Jul 24 '26 15:07

Philip Dukhov