Skip to content
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 IList<T> and related APIs to JsonObject. #103645

Merged
merged 4 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,8 @@ public void SetAt(int index, TKey key, TValue value)
}

// The key doesn't match that index. If it exists elsewhere in the collection, fail.
uint _ = 0, collisionCount = 0;
if (IndexOf(key, ref _, ref collisionCount) >= 0)
uint hashCode = 0, collisionCount = 0;
if (IndexOf(key, ref hashCode, ref collisionCount) >= 0)
{
ThrowHelper.ThrowDuplicateKey(key);
}
Expand All @@ -820,6 +820,7 @@ public void SetAt(int index, TKey key, TValue value)
// (we could check for this, but in a properly balanced dictionary the chances should
// be low for a match, so it's not worth it).
RemoveEntryFromBucket(index);
e.HashCode = hashCode;
e.Key = key;
e.Value = value;
PushEntryIntoBucket(ref e, index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,25 @@ public void OrderedDictionary_Generic_SetAt_GetAt_Roundtrip(int count)
}
}

[Fact]
public void OrderedDictionary_SetAt_KeyValuePairSubsequentlyAvailable()
{
TKey key0 = CreateTKey(0), key1 = CreateTKey(1);
TValue value0 = CreateTValue(0), value1 = CreateTValue(1);

var dict = new OrderedDictionary<TKey, TValue>
{
[key0] = value0,
};

dict.SetAt(index: 0, key1, value1);

Assert.Equal(1, dict.Count);
Assert.Equal([new(key1, value1)], dict);
Assert.False(dict.ContainsKey(key0));
Assert.True(dict.ContainsKey(key1));
}

#endregion

#region Remove(..., out TValue)
Expand Down
22 changes: 16 additions & 6 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -706,11 +706,11 @@ internal JsonNode() { }
public System.Text.Json.Nodes.JsonValue AsValue() { throw null; }
public System.Text.Json.Nodes.JsonNode DeepClone() { throw null; }
public static bool DeepEquals(System.Text.Json.Nodes.JsonNode? node1, System.Text.Json.Nodes.JsonNode? node2) { throw null; }
public string GetPropertyName() { throw null; }
public int GetElementIndex() { throw null; }
public string GetPath() { throw null; }
public string GetPropertyName() { throw null; }
public System.Text.Json.JsonValueKind GetValueKind() { throw null; }
public virtual T GetValue<T>() { throw null; }
public JsonValueKind GetValueKind() { throw null; }
public int GetElementIndex() { throw null; }
public static explicit operator bool (System.Text.Json.Nodes.JsonNode value) { throw null; }
public static explicit operator byte (System.Text.Json.Nodes.JsonNode value) { throw null; }
public static explicit operator char (System.Text.Json.Nodes.JsonNode value) { throw null; }
Expand Down Expand Up @@ -801,7 +801,7 @@ internal JsonNode() { }
public static System.Threading.Tasks.Task<System.Text.Json.Nodes.JsonNode?> ParseAsync(System.IO.Stream utf8Json, System.Text.Json.Nodes.JsonNodeOptions? nodeOptions = default(System.Text.Json.Nodes.JsonNodeOptions?), System.Text.Json.JsonDocumentOptions documentOptions = default(System.Text.Json.JsonDocumentOptions), System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("Creating JsonValue instances with non-primitive types requires generating code at runtime.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Creating JsonValue instances with non-primitive types is not compatible with trimming. It can result in non-primitive types being serialized, which may have their members trimmed.")]
public void ReplaceWith<T>(T value) { throw null; }
public void ReplaceWith<T>(T value) { }
public string ToJsonString(System.Text.Json.JsonSerializerOptions? options = null) { throw null; }
public override string ToString() { throw null; }
public abstract void WriteTo(System.Text.Json.Utf8JsonWriter writer, System.Text.Json.JsonSerializerOptions? options = null);
Expand All @@ -811,25 +811,35 @@ public partial struct JsonNodeOptions
private int _dummyPrimitive;
public bool PropertyNameCaseInsensitive { readonly get { throw null; } set { } }
}
public sealed partial class JsonObject : System.Text.Json.Nodes.JsonNode, System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>, System.Collections.Generic.IDictionary<string, System.Text.Json.Nodes.JsonNode?>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>, System.Collections.IEnumerable
public sealed partial class JsonObject : System.Text.Json.Nodes.JsonNode, System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>, System.Collections.Generic.IDictionary<string, System.Text.Json.Nodes.JsonNode?>, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>, System.Collections.Generic.IList<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>, System.Collections.IEnumerable
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
public JsonObject(System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>> properties, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { }
public JsonObject(System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { }
public int Count { get { throw null; } }
bool System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>.IsReadOnly { get { throw null; } }
System.Collections.Generic.ICollection<string> System.Collections.Generic.IDictionary<string, System.Text.Json.Nodes.JsonNode?>.Keys { get { throw null; } }
System.Collections.Generic.ICollection<System.Text.Json.Nodes.JsonNode?> System.Collections.Generic.IDictionary<string, System.Text.Json.Nodes.JsonNode?>.Values { get { throw null; } }
System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?> System.Collections.Generic.IList<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>.this[int index] { get { throw null; } set { } }
public void Add(System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?> property) { }
public void Add(string propertyName, System.Text.Json.Nodes.JsonNode? value) { }
public void Clear() { }
public bool ContainsKey(string propertyName) { throw null; }
public static System.Text.Json.Nodes.JsonObject? Create(System.Text.Json.JsonElement element, System.Text.Json.Nodes.JsonNodeOptions? options = default(System.Text.Json.Nodes.JsonNodeOptions?)) { throw null; }
public System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?> GetAt(int index) { throw null; }
public System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>> GetEnumerator() { throw null; }
public int IndexOf(string propertyName) { throw null; }
public void Insert(int index, string propertyName, System.Text.Json.Nodes.JsonNode? value) { }
public bool Remove(string propertyName) { throw null; }
public void RemoveAt(int index) { }
public void SetAt(int index, string propertyName, System.Text.Json.Nodes.JsonNode? value) { }
public void SetAt(int index, System.Text.Json.Nodes.JsonNode? value) { }
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
bool System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>.Contains(System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode> item) { throw null; }
void System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>.CopyTo(System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode>[] array, int index) { }
bool System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>.Remove(System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode> item) { throw null; }
bool System.Collections.Generic.IDictionary<string, System.Text.Json.Nodes.JsonNode?>.TryGetValue(string propertyName, out System.Text.Json.Nodes.JsonNode? jsonNode) { throw null; }
bool System.Collections.Generic.IDictionary<string, System.Text.Json.Nodes.JsonNode?>.TryGetValue(string propertyName, out System.Text.Json.Nodes.JsonNode jsonNode) { throw null; }
int System.Collections.Generic.IList<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>.IndexOf(System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode> item) { throw null; }
void System.Collections.Generic.IList<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>.Insert(int index, System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode> item) { }
void System.Collections.Generic.IList<System.Collections.Generic.KeyValuePair<string, System.Text.Json.Nodes.JsonNode?>>.RemoveAt(int index) { }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; }
public bool TryGetPropertyValue(string propertyName, out System.Text.Json.Nodes.JsonNode? jsonNode) { throw null; }
public override void WriteTo(System.Text.Json.Utf8JsonWriter writer, System.Text.Json.JsonSerializerOptions? options = null) { }
Expand Down
16 changes: 13 additions & 3 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
<Compile Include="System\Text\Json\JsonHelpers.cs" />
<Compile Include="System\Text\Json\JsonHelpers.Date.cs" />
<Compile Include="System\Text\Json\JsonHelpers.Escaping.cs" />
<Compile Include="System\Text\Json\JsonPropertyDictionary.cs" />
<Compile Include="System\Text\Json\JsonPropertyDictionary.KeyCollection.cs" />
<Compile Include="System\Text\Json\JsonPropertyDictionary.ValueCollection.cs" />
<Compile Include="System\Text\Json\JsonTokenType.cs" />
<Compile Include="System\Text\Json\Nodes\JsonArray.cs" />
<Compile Include="System\Text\Json\Nodes\JsonArray.IList.cs" />
Expand All @@ -85,6 +82,7 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
<Compile Include="System\Text\Json\Nodes\JsonNodeOptions.cs" />
<Compile Include="System\Text\Json\Nodes\JsonObject.cs" />
<Compile Include="System\Text\Json\Nodes\JsonObject.IDictionary.cs" />
<Compile Include="System\Text\Json\Nodes\JsonObject.IList.cs" />
<Compile Include="System\Text\Json\Nodes\JsonValue.CreateOverloads.cs" />
<Compile Include="System\Text\Json\Nodes\JsonValue.cs" />
<Compile Include="System\Text\Json\Nodes\JsonValueOfTCustomized.cs" />
Expand Down Expand Up @@ -354,6 +352,12 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
<Compile Include="$(CoreLibSharedDir)System\Reflection\NullabilityInfoContext.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp' or $([MSBuild]::VersionLessThan('$(TargetFrameworkVersion)', '9.0'))">
<Compile Include="System\Text\Json\OrderedDictionary.cs" />
<Compile Include="System\Text\Json\OrderedDictionary.KeyCollection.cs" />
<Compile Include="System\Text\Json\OrderedDictionary.ValueCollection.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionGreaterThanOrEquals('$(TargetFrameworkVersion)', '7.0'))">
<Compile Include="System\Text\Json\Serialization\Converters\Value\Int128Converter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Value\UInt128Converter.cs" />
Expand All @@ -376,6 +380,12 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
<Compile Include="System\Text\Json\Reader\JsonReaderHelper.netstandard.cs" />
</ItemGroup>

<ItemGroup>
<None Include="System\Text\Json\OrderedDictionary.cs" />
<None Include="System\Text\Json\OrderedDictionary.KeyCollection.cs" />
<None Include="System\Text\Json\OrderedDictionary.ValueCollection.cs" />
</ItemGroup>

<!-- Application tfms (.NETCoreApp, .NETFramework) need to use the same or higher version of .NETStandard's dependencies. -->
<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Text.Encodings.Web\src\System.Text.Encodings.Web.csproj" />
Expand Down

This file was deleted.

Loading
Loading