Are there any kotlin libraries like SwiftyJSON for...
# android
j
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
Klaxon().parse<Map<String,Any>>(jsonString)!!
or
Copy code
var map: Map<String, Any> = HashMap()
map = Gson().fromJson(jsonString, map.javaClass)
l
You could try the kotlix.serialization library, you can use the JsonObject and JsonArray classes to deserialize arbitrary json payloads
e
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
p
kotlinx.serialization, it you don't mind a bit of experimental stuff. Otherwise I recommend Moshi for Kotlin.
e
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
n
Moshi can actually handle arbitrary data. Deserialize to
Any?
and it'll represent objects as
Map
, and arrays as
List
, and it will box primitives. Kinda awkward to pull information out but nice for integrating with libraries like Firebase that expect such a format.