[Question] How to convert JsonObject to Map<String, Any>
See original GitHub issueWhat is your use-case and why do you need this feature? I am migrating from GSon to Kotlin.Serialization. One of the uses cases of GSon was used in storing data to Firebase Database.
The class that saves the data does not have the original class/type information, instead it has access to the data as String (json)
The original code will deserialize the String into Map<String, Any>
val map = Gson().fromJson(jsonString, object : TypeToken<HashMap<String, Any>>() {}.type)
ref.setValue(map)
Using Kotlin.Serialization results in Map<String, JsonElement>, and when sent to Firebase, each property becomes an object with content
and extra “metadata” field (ie: string=true
)
val map = Json.parseToJsonElement(jsonString).jsonObject.toMap()
ref.setValue(map)
Describe the solution you’d like I must be missing something simple, as I am unable to convert JsonObject to Map<String, Any>.
Is there an overload of toMap
that would give the expected result?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Thanks for the clarification.
Please consider adding this feature for future release. I believe this is a common use case, especially given Firebase API uses Map<String, Any>
There’s no built-in method for that, but it can be easily written using recursion. Maybe https://github.com/Kotlin/kotlinx.serialization/issues/746 has some clues