Are there any kotlin libraries like SwiftyJSON for...
# android
Are there any kotlin libraries like SwiftyJSON for swift. I'm looking at JSON parsing libraries and they all seem to need a defined class to parse json strings to. SwiftyJSON was pretty useful as I didnt need to declare data classes and could just build objects from http responses with out having to decode each request i made. Ah looks like i can do it with
Copy code
Copy code
var map: Map<String, Any> = HashMap()
map = Gson().fromJson(jsonString, map.javaClass)
You could try the kotlix.serialization library, you can use the JsonObject and JsonArray classes to deserialize arbitrary json payloads
that being said, you will end up with chains of nullables if you go down that solution, while kotlinx.serialization.JsonElement will throw exceptions instead… nothing works quite like SwiftyJSON
ūüĎć 1
although building your own DSL on either might be doable
kotlinx.serialization, it you don't mind a bit of experimental stuff. Otherwise I recommend Moshi for Kotlin.
Moshi falls into the category of "define a data type and parse into it"
which I think is a good solution - usually the best one - but apparently not what OP was looking for
Moshi can actually handle arbitrary data. Deserialize to
and it'll represent objects as
, and arrays as
, and it will box primitives. Kinda awkward to pull information out but nice for integrating with libraries like Firebase that expect such a format.