-
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
add function for removing entry in JsonObjectBuilder
#2191
base: dev
Are you sure you want to change the base?
Conversation
Could you please provide a rationale and the reasoning for the proposed API? |
Firstly, I would expect from a builder class, that it can remove stuff, if there is no technological barrier against it. For example: Another example here would be, that you want to have an identifier at the beginning of the JSON, but it is not required in all cases, if the content added later would lead to a conflict-free store. Every of these cases would lead to much cleaner code, if the builder has the ability to simply remove entries after they got added. I would appreciate feedback if I am wrong about the functionality. |
The issue with the proposed PR is that it doesn't solve any real-world problems, only theoretical ones.
Unfortunately, this is different from how we are inclined to think about API addition. We need a compelling reason to add stuff, not the other way around, and the score of the reasoning starts at negative values. It would be nice to provide builders that comply with such expectations and where For example, |
One reason is that there's not a clear way of how to achieve this at the moment. Without a clear helper method, it's easy to
I agree, |
I think the original objects should be mutable , JsonObject , I have a heirarchy of objects , I navigate deeply into nested object using a Json Pointer , Json Pointer , a class I created which holds segments like "obj1/obj2/key" in List and then navigates to it , If I want to modify that pointer I will not only create a copy of this object , I will have to go up in the heirarchy and create copies of objects and change it , to insert a single key at that pointer I think just like there's a List and MutableList there should be JsonObject and MutableJsonObject Currently I use internal fun Map<String, JsonElement>.withValue(key: String, value: JsonElement): JsonObject {
return JsonObject(toMutableMap().apply { put(key, value) })
}
internal fun Map<String, JsonElement>.withValue(pointer: JsonPointer, value: JsonElement): JsonObject {
if (pointer.segments.size == 1) return withValue(pointer.segments.first(), value)
val root = toMutableMap()
val parent = JsonPointer(pointer.segments.dropLast(1)).navigateToMutable(root)
parent[pointer.segments.last()] = value
return JsonObject(root)
}
fun navigateToMutable(elements: MutableMap<String, JsonElement>): MutableMap<String, JsonElement> {
var current = elements
for (segment in segments) {
current = (current[segment] as? JsonObject)?.toMutableMap() ?: mutableMapOf<String, JsonElement>().also {
current[segment] = JsonObject(it)
}
}
return current
} |
I've created a new issue to take this proposal further: #2308 |
Adds
JsonObjectBuilder.remove(key: String)
to remove an entry from aJsonObjectBuilder
instance.My first PR to this repo, so I hope I did everything right. :D