Best way to remove a number of elements from a JsonArray #106726
-
We have a piece of code we're trying to write that is parses JSON into a JsonArray, and needs to trim off a bunch of entries from the JsonArray at the end. For example, the JsonArray might a few hundred entries, but we want to trim it down to just 10. What's the fastest way to do this? Ideally JsonArray would have a RemoveRange method but it doesn't. JsonArray has a RemoveAt which we can call in a loop but that seems a bit silly. We saw there's a ReplaceWith() method, and the hope was we can do something like:
But that creation of a new JsonArray fails since the nodes we're trying to add already have the old array as a parent. It seems either you have to DeepClone() your entries in the array (ick!) or only replace arrays with empty arrays. 😄 (I'm also suspicious of ReplaceWith() in general since if you're trying to replace the root node it looks like it'll just immediately exit which might be a bug?) Is there some good way to do this? Otherwise is there any reason we can't add a RemoveRange() method, or a method on JsonArray that rather than accepting a JsonArray accepts an array of elements to replace? Tagging @dotnet/area-system-text-json and @fanyang-mono. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
JsonArray implements |
Beta Was this translation helpful? Give feedback.
-
No reason that I'm aware of. Would you like to file an API proposal? |
Beta Was this translation helpful? Give feedback.
I filed an API proposal, and then forgot to link it here: #107962