Skip to content
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

How to add / remove / edit value of kotlinx.serialization.json.JsonObject #1593

Closed
MeNiks opened this issue Jul 9, 2021 · 2 comments
Closed
Labels

Comments

@MeNiks
Copy link

MeNiks commented Jul 9, 2021

I am currently struggling for best way to add property to existing jsonObject(kotlinx.serialization.json.JsonObject)

Even i have found that JsonObject internally has private immutable map

private val content: Map<String, JsonElement>
Scenario : Suppose i already have one jsonobject with 50 properties in it and considering jsonobject at any key can have any number of depth

For such situation how can we efficiently perform below operaions :

Adding primitive properties
Updating primitive properties
Deleting primitive properties
FYI : Even i have found there is class called JsonObjectBuilder but it do not take existing JsonElement / JsonObject / JsonArray infact it will newly create another jsonObject

Thanks for giving your time to read till here 😀

Stack Overflow Link

@MeNiks MeNiks added the feature label Jul 9, 2021
@sandwwraith
Copy link
Member

I see stackoverflow already has correct answer: yeah, for bulk operation it's better to convert content to MutableMap first for better performance. For one-time operations, you can use standard collection extensions like + e.g. JsonObject(oldObject + ("foo" to JsonPrimitive(42)) (it creates a copy of the map)

@MeNiks
Copy link
Author

MeNiks commented Jul 10, 2021

@sandwwraith Thanks for the reply as of now i have created this extension function :
https://gist.github.com/MeNiks/597b761f3b2ea8ec511d5d40a3213399

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants