-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
JsonObject
should support property order manipulation
#102932
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
This is good in order to support serialization scenarios. Note that JSON Schema is set up to process these keywords first, regardless of where they appear in a document. |
I feel this is the wrong place to support this. For me, it is a serialization concern not a JSON Object model concern. In some ways it is a thin-end-of-the-wedge problem; as soon as we start to suggest that JSON objects have a notion of "property ordering" outside of the serialized representation, people will start depending on it. We already see subtle bugs from people depending on the fact that |
Unlike Dictionary, the |
So I would have less discomfort if that was front-and-centre - i.e. this is not a (And the fact it is sometimes not-yet-serialized is a laziness optimization 😀) |
Every model of a JSON object necessarily encapsulates a notion of property ordering, whether intentional or incidental. I wouldn't consider that a departure from the spirit of the JSON specification though; it is not that objects should not be orderable but rather that different permutations of the same object must be semantically equivalent. The latter should still be the case (cf. the implementation of the |
This is true. And also true of e.g. dictionary enumeration. But we are careful to conceptually separate "regular dictionary" and "ordered dictionary" in the type system. The conceptual change with this API is that we are transitioning from the former to the latter. Just for clarity - I'm not wholly opposed, just "uncomfortable". |
Like I said, unlike Dictionary JsonObject guarantees order of enumeration. It is very much implemented as an ordered dictionary. |
namespace System.Text.Json.Nodes;
public sealed partial class JsonObject : IDictionary<string, JsonNode?>, IList<KeyValuePair<string, JsonNode?>>
{
public int IndexOf(string key);
public void Insert(int index, string key, JsonNode? value);
public void RemoveAt(int index);
} |
Based on the discussion, wouldn't it be clearer to say: "We should also explicitly implement |
In addition to the APIs as approved #102932 (comment) I propose the inclusion of the following methods: public sealed partial class JsonObject : IDictionary<string, JsonNode?>, IList<KeyValuePair<string, JsonNode?>>
{
public KeyValuePair<string, JsonNode?> GetAt(int index);
public void SetAt(int index, JsonValue? value);
public void SetAt(int index, string propertyName, JsonValue? value);
} These APIs reflect the API surface in the newly added OrderedDictionary and solve a use case that would otherwise require making an explicit cast of the object to an |
The
JsonObject
class implementsIDictionary<string, JsonNode?>
, however as of today the type does permit any manipulation of the order of its properties (even though order is being tracked internally). While this is consistent with the semantics of JSON where order of properties is not significant, it is often the case that one wants to arrange the order out of human readability concerns. For example, it is common for JSON schema docs to place the$schema
and$id
properties at the start of the object.We should update
JsonObject
so that properties can be inserted at specific locations.API Proposal
API Usage
Alternative Designs
We can't have
JsonObject
implementIList<KeyValuePair<string, JsonNode?>
(at least not without explicit implementation) since the baseJsonNode
class already exposes an indexer for ints with different signature and semantics.The text was updated successfully, but these errors were encountered: