Azur Haljeta
12/08/2020, 8:09 PMJsonObject
without unboxing the whole object?Azur Haljeta
12/08/2020, 8:09 PMAzur Haljeta
12/08/2020, 8:09 PMprivate val content: Map<String, JsonElement>
russhwolf
12/08/2020, 10:23 PMcontent
is private but because JsonObject
implements Map
, you can call toMutableMap()
on it and make your changes there, then create a new object from that mapPaul Griffith
12/09/2020, 5:22 PMMap
you can just use the map + operator; it'd be pretty easy to write an extension that does something like this:
val updated = JsonObject(existing + mapOf("abc" to JsonPrimitive("def")))
The JsonPrimitive
call is a little rough - maybe use buildJsonObject
for the new key(s) you're addingPaul Griffith
12/09/2020, 5:25 PMfun JsonObject.update(keys: JsonObjectBuilder.() -> Unit) = JsonObject(this + buildJsonObject(keys))
in use becomes:
existing.update {
put("abc", "def")
}
update
is probably the wrong term since you're returning a new instance, not modifying the existing, but 🤷Azur Haljeta
12/11/2020, 7:45 PMJsonObject
in question. They rather create a new instance.
Your suggestions are nice workarounds.
But is this API supposed to be such closed that workarounds are needed? I worked a lot with Gson
and org.json
libraries which have such basic features out of the box.
If this isn't intended, maybe I can open a PR.russhwolf
12/11/2020, 7:49 PM