-
Notifications
You must be signed in to change notification settings - Fork 625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update JSON builders to provide more mutable/iterable operations #2308
Comments
Upvote. I found mutable JSON builders would simplify a lot of code in my project and adding them shouldn't require much work. For now I have to store everything inside Hope the developers can release the newer package ASAP. |
After further thought I'm much more keen on the idea of introducing Should I make a new issue for this request @sandwwraith, or can I re-write this one? |
via @RefactoringCandidate. This talks about Kotlin/kotlinx.serialization#2308 possible future impact on this function
Upvote too, it's very inconvenient to modify json without model class. |
I've returned to this problem briefly, so here are my observations:
buildJsonObject {
addAll(someSource)
if (someCondition) this["foo"] = JsonPrimitive("bar")
if (this.size > 10) this.remove("some")
someCollection.filterTo(this) { ... }
} This is something that looks nice, but I do not know if this is really in demand. After all, you can do very same things just with
class X {
var size = 11
fun doStuff(): JsonObject {
return buildJsonObject {
put("foo", "bar")
if (size > 10) put("additionalSize", size - 10)
}
}
} Before this change,
To conclude, making |
What is your use-case and why do you need this feature?
The JSON array and object builders are very useful, but their operations are restricted to only add elements. This makes it more difficult to perform additional mutations (like removing elements), or collection-based operations like iterating over the values.
Implementing all of the possible List-specific and Map-specific operations can be easily achieved via delegation.
Missing operations require in unclear workarounds
For example, there's no a clear way of how to remove elements at the moment. Without a default method method, it's easy to
come up with complicated workarounds that aren't easy to read, or scale
accidentally write code that looks correct, but introduces bugs
or (for Kotlin / KxS beginners) get confused and give up!
Mutable/immutable JSON element mismatch
There is also a mis-match between the JSON builders and the read-only classes they build.
JsonObject
implementsMap<String, JsonElement>
via delegationkotlinx.serialization/formats/json/commonMain/src/kotlinx/serialization/json/JsonElement.kt
Lines 191 to 194 in 7a35a2d
JsonArray
implementsList<JsonElement>
via delegationkotlinx.serialization/formats/json/commonMain/src/kotlinx/serialization/json/JsonElement.kt
Line 220 in 7a35a2d
It makes sense to update the JSON builders so they work similarly, which helps with understanding and reduces the difference between the two.
Sharing methods
It would also help understanding if
JsonObject
/JsonArray
shares as much functionality as possible withMutableMap
/MutableList
, because learning one helps with learning to use the other.Describe the solution you'd like
MutableJsonArray
andMutableJsonObject
classesMutableJsonArray
extendsJsonArray
and implementsMutableList<JsonElement>
using delegationMutableJsonObject
extendsJsonObject
and implementsMutableMap<String, JsonElement>
using delegationJsonArrayBuilder
, and replace it with the newMutableJsonArray
JsonObjectBuilder
, and replace it with the newMutableJsonObject
Example:
I also see two further improvements:
Mutable variants
Currently the JSON object/array builders are
internal
and are only used for the builder operations, but why is this? Since they would become specialised implementations of a mutable collection, why not make them publicly accessible? I propose the following:JsonArrayBuilder
toMutableJsonArray
,JsonObjectBuilder
toMutableJsonObject
,public
.This would allow users to create and define mutable JSON objects and arrays, using a class that would also be shared with the
buildJsonObject {}
andbuildJsonArray {}
helpers.Update extension functions?
If JsonArrayBuilder implements
MutableList<JsonElement>
, then all of theJsonArrayBuilder
extension functions could be adapted to extendMutableList<JsonElement>
instead ofJsonArrayBuilder
.This would improve the usability of KxS in more situations, making the KxS more convenient to use.
See also
The text was updated successfully, but these errors were encountered: