-
Notifications
You must be signed in to change notification settings - Fork 4.8k
JsonObject implementation and tests #40206
Changes from all commits
711a5aa
437b8ba
7887ad4
0cd91e1
17244e9
292a28b
86bfe77
5faf9a9
255851f
42b50f5
46aff04
a384394
ca34f43
180b478
02b805f
c2f0e48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,12 @@ | |
|
|
||
| namespace System.Text.Json | ||
| { | ||
| public enum DuplicatePropertyNameHandling | ||
| { | ||
| Replace = 0, | ||
| Ignore = 1, | ||
| Error = 2, | ||
| } | ||
| public sealed partial class JsonBoolean : System.Text.Json.JsonNode, System.IEquatable<System.Text.Json.JsonBoolean> | ||
| { | ||
| public JsonBoolean() { } | ||
|
|
@@ -260,6 +266,56 @@ public void SetUInt64(ulong value) { } | |
| [System.CLSCompliantAttribute(false)] | ||
| public bool TryGetUInt64(out ulong value) { throw null; } | ||
| } | ||
| public sealed partial class JsonObject : System.Text.Json.JsonNode, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, System.Text.Json.JsonNode>>, System.Collections.IEnumerable | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO JsonObject should implement
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kasiabulat are there any methods from IDictionary that we would be missing if we wanted to implement that? Are there any Sort methods in IDictionary?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no extension methods accepting IDictionary / IList instead of IEnumerable. LINQ queries work faster with IList and that's why making JsonArray implement it seemed valuable. No queries work faster with IDictionary - that's also why we thought implementing it by JsonObject would be unnecessary. To implement IDictionary we are lacking:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI use explicit implementation for the dictionary APIs people will likely never use - https://softwareengineering.stackexchange.com/questions/136319/whats-the-difference-between-implementing-an-interface-explicitly-or-implicitly
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think my point was that unless there were compelling extension methods or other specific use cases that it probably didn't need to implement the IDictionary interface. e.g. that one should be able to say "It implements IDictionary and IReadOnlyDictionary because [reason]" where "[reason]" is something more concrete than "because it's logically a dictionary" or "because it already had all the right methods". Same result, different presentation: If no one will ever use the fact that it's an I(ReadOnly)Dictionary then it didn't need to assert the interface.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amoungst other benefits, IDictionary allows a collection initializer to be used: var o = new JsonObject
{
["Name"] = "James",
["Radness"] = 98,
}
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That doesn't require IDictionary. Just IEnumerable + suitable Add methods, which it already has 😄 |
||
| { | ||
| public JsonObject(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, System.Text.Json.JsonNode>> jsonProperties, System.Text.Json.DuplicatePropertyNameHandling duplicatePropertyNameHandling = System.Text.Json.DuplicatePropertyNameHandling.Replace) { } | ||
| public JsonObject(System.Text.Json.DuplicatePropertyNameHandling duplicatePropertyNameHandling = System.Text.Json.DuplicatePropertyNameHandling.Replace) { } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't have expected Do we think there will be a need for this setting when creating a JsonObject itself via the ctor? What scenario would it be used for?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 People who are manually creating a JsonObject in code can choose whether to use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So actually, my Maybe we could have those options both in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Can you give an example/sample where you think it would be useful to have
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example something containing interactions with user? var properties = new JsonObject(DuplicatePropertyNameHandling.Error);
while(PropertiesReady())
{
string userProvidedPropertyName = Console.ReadLine();
string userProvidedPropertyValue = Console.ReadLine();
try
{
properties.Add(userProvidedPropertyName, userProvidedPropertyValue);
}
catch (ArgumentException)
{
Console.WriteLine(string.Format("Property name `{0}` already exists. Please provide properties with unique names.", userProvidedPropertyName));
}
}
Mailbox.SendData(properties.AsJsonElement()); |
||
| public System.Text.Json.JsonNode this[string propertyName] { get { throw null; } set { } } | ||
| public System.Collections.Generic.ICollection<string> PropertyNames { get { throw null; } } | ||
| public System.Collections.Generic.ICollection<System.Text.Json.JsonNode> PropertyValues { get { throw null; } } | ||
| public void Add(System.Collections.Generic.KeyValuePair<string, System.Text.Json.JsonNode> jsonProperty) { } | ||
| public void Add(string propertyName, bool propertyValue) { } | ||
|
kasiabulat marked this conversation as resolved.
|
||
| public void Add(string propertyName, byte propertyValue) { } | ||
| public void Add(string propertyName, System.DateTime propertyValue) { } | ||
| public void Add(string propertyName, System.DateTimeOffset propertyValue) { } | ||
| public void Add(string propertyName, decimal propertyValue) { } | ||
| public void Add(string propertyName, double propertyValue) { } | ||
| public void Add(string propertyName, System.Guid propertyValue) { } | ||
| public void Add(string propertyName, short propertyValue) { } | ||
| public void Add(string propertyName, int propertyValue) { } | ||
| public void Add(string propertyName, long propertyValue) { } | ||
| public void Add(string propertyName, System.ReadOnlySpan<char> propertyValue) { } | ||
| [System.CLSCompliantAttribute(false)] | ||
| public void Add(string propertyName, sbyte propertyValue) { } | ||
| public void Add(string propertyName, float propertyValue) { } | ||
| public void Add(string propertyName, string propertyValue) { } | ||
| public void Add(string propertyName, System.Text.Json.JsonNode propertyValue) { } | ||
| [System.CLSCompliantAttribute(false)] | ||
| public void Add(string propertyName, ushort propertyValue) { } | ||
| [System.CLSCompliantAttribute(false)] | ||
| public void Add(string propertyName, uint propertyValue) { } | ||
| [System.CLSCompliantAttribute(false)] | ||
| public void Add(string propertyName, ulong propertyValue) { } | ||
| public void AddRange(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, System.Text.Json.JsonNode>> jsonProperties) { } | ||
| public bool ContainsProperty(string propertyName) { throw null; } | ||
|
kasiabulat marked this conversation as resolved.
|
||
| public System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string, System.Text.Json.JsonNode>> GetEnumerator() { throw null; } | ||
| public System.Text.Json.JsonObject GetJsonObjectPropertyValue(string propertyName) { throw null; } | ||
| public System.Text.Json.JsonNode GetPropertyValue(string propertyName) { throw null; } | ||
| public bool Remove(string propertyName) { throw null; } | ||
| System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } | ||
| public bool TryGetJsonObjectPropertyValue(string propertyName, out System.Text.Json.JsonObject jsonObject) { throw null; } | ||
| public bool TryGetPropertyValue(string propertyName, out System.Text.Json.JsonNode jsonNode) { throw null; } | ||
| } | ||
| public partial struct JsonObjectEnumerator : System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string, System.Text.Json.JsonNode>>, System.Collections.IEnumerator, System.IDisposable | ||
| { | ||
| private object _dummy; | ||
| public JsonObjectEnumerator(System.Text.Json.JsonObject jsonObject) { throw null; } | ||
| public System.Collections.Generic.KeyValuePair<string, System.Text.Json.JsonNode> Current { get { throw null; } } | ||
| object System.Collections.IEnumerator.Current { get { throw null; } } | ||
| public void Dispose() { } | ||
| public bool MoveNext() { throw null; } | ||
| public void Reset() { } | ||
| } | ||
| public readonly partial struct JsonProperty | ||
| { | ||
| private readonly object _dummy; | ||
|
|
@@ -324,6 +380,9 @@ public JsonSerializerOptions() { } | |
| public sealed partial class JsonString : System.Text.Json.JsonNode, System.IEquatable<System.Text.Json.JsonString> | ||
| { | ||
| public JsonString() { } | ||
| public JsonString(System.DateTime value) { } | ||
| public JsonString(System.DateTimeOffset value) { } | ||
| public JsonString(System.Guid value) { } | ||
| public JsonString(System.ReadOnlySpan<char> value) { } | ||
| public JsonString(string value) { } | ||
| public string Value { get { throw null; } set { } } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| namespace System.Text.Json | ||
| { | ||
| /// <summary> | ||
| /// Specifies how duplicate property names are handled when added to JSON object. | ||
| /// </summary> | ||
| public enum DuplicatePropertyNameHandling | ||
| { | ||
| /// <summary> | ||
| /// Replace the existing value when there is a duplicate property. The value of the last property in the JSON object will be used. | ||
| /// </summary> | ||
| Replace, | ||
| /// <summary> | ||
| /// Ignore the new value when there is a duplicate property. The value of the first property in the JSON object will be used. | ||
| /// </summary> | ||
| Ignore, | ||
| /// <summary> | ||
| /// Throw an <exception cref="ArgumentException"/> when a duplicate property is encountered. | ||
| /// </summary> | ||
| Error, | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.