-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: Add System.Text.Json.Nodes.JsonArray.Remove(All|Range) #107962
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
Sounds reasonable. One potential alternative that has been partially explored in #76375 is exposing extension methods or DIMs for |
As we are looking at these APIs - do we also want to add APIs to allow slicing in general? JsonArray array = ...;
var slice = array[5..^1]; |
Because |
namespace System.Text.Json.Nodes;
public class JsonArray
{
public int RemoveAll(Func<JsonNode, bool> match);
public void RemoveRange(int index, int count);
} |
Background and motivation
System.Text.Json.Nodes.JsonArray is a
List<T>
-like type that represents an array of JSON values; it hasRemove(JsonNode)
andRemoveAt(int index)
methods that let you remove a single element. In a use we had, we needed to trim a number of values out the array, i.e. "please only return the first 10", which required us to either repeatedly call the Remove methods in a loop, or try to create a new array entirely from scratch. The latter was tricky since the nodes have a Parent object pointer, so you can't create a new array without detaching the nodes from the previous array first. See this discussion for various workarounds that were discussed.The original concern with the workarounds was performance: in my original case since we were potentially trimming a larger array down to a smaller array, we didn't want the overall cost to be above O(n) of the original size of the JSON object. Thus, repeatedly calling RemoveAt() could be O(n^2) unless we were careful to remove from the end in reverse order. Although the workarounds weren't bad, it meant what should otherwise be a simple operation became more complicated.
API Proposal
The following two methods are added
These signatures mirror the API shape of
List<T>
so they should be familiar with users of the API. Furthermore, since JsonArray is implemented under the covers as aList<T>
, this ensures implementation should be pretty easy too.API Usage
Alternative Designs
No response
Risks
Since I'm proposing to reuse the existing patterns of
List<T>
, it means this carries along any regret we have of those APIs. I'm not aware of any specific problems with them, but they may of course exist!The text was updated successfully, but these errors were encountered: