https://kotlinlang.org logo
#announcements
Title
# announcements
a

Andrew Gazelka

07/17/2019, 12:33 AM
I have used Klaxon for JSON parsing for a bit but I can’t find if there is a function to merge JSON documents. Anyone know if there is one? If there isn’t anyone know of a good lib for this?
guess I can just use
Copy code
fun merge(initial: JsonObject, toMerge: JsonObject, overwrite: Boolean = true): JsonObject {

    val result = initial.copy()
    for ((k, v) in toMerge) {
        val current = result[k]
        if (current == null) {
            result[k] = v
        } else {
            if (current is JsonObject) {
                assert(v is JsonObject)
                result[k] = merge(current, v as JsonObject, overwrite) // TODO: is this bad code
            } else if (overwrite) {
                result[k] = v
            }
        }
    }
    return result
}
e

elect

07/17/2019, 8:54 AM
the author is here on slack, you may want to ask him directly
4 Views