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 1 commit
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
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
14 changes: 11 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,16 @@ 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\JsonPropertyDictionary.cs" />
<Compile Include="System\Text\Json\JsonPropertyDictionary.KeyCollection.cs" />
<Compile Include="System\Text\Json\JsonPropertyDictionary.ValueCollection.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and $([MSBuild]::VersionGreaterThanOrEquals('$(TargetFrameworkVersion)', '9.0'))">
<Compile Include="System\Text\Json\JsonPropertyDictionary.Net90.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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ namespace System.Text.Json
{
internal sealed partial class JsonPropertyDictionary<T>
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
private KeyCollection? _keyCollection;

public IList<string> GetKeyCollection()
{
return _keyCollection ??= new KeyCollection(this);
}

private sealed class KeyCollection : IList<string>
{
private readonly JsonPropertyDictionary<T> _parent;
Expand All @@ -30,7 +23,7 @@ public KeyCollection(JsonPropertyDictionary<T> jsonObject)

public string this[int index]
{
get => _parent.List[index].Key;
get => _parent.GetAt(index).Key;
set => throw ThrowHelper.GetNotSupportedException_CollectionIsReadOnly();
}

Expand All @@ -46,7 +39,7 @@ IEnumerator IEnumerable.GetEnumerator()

public void Clear() => ThrowHelper.ThrowNotSupportedException_CollectionIsReadOnly();

public bool Contains(string propertyName) => _parent.ContainsProperty(propertyName);
public bool Contains(string propertyName) => _parent.ContainsKey(propertyName);

public void CopyTo(string[] propertyNameArray, int index)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;

namespace System.Text.Json
{
/// <summary>
/// Defines an ordered dictionary for storing JSON property metadata.
/// </summary>
internal sealed class JsonPropertyDictionary<T>(StringComparer comparer, int capacity = 0)
: OrderedDictionary<string, T>(capacity, comparer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ namespace System.Text.Json
{
internal sealed partial class JsonPropertyDictionary<T>
{
private ValueCollection? _valueCollection;

public IList<T> GetValueCollection()
{
return _valueCollection ??= new ValueCollection(this);
}

private sealed class ValueCollection : IList<T>
{
private readonly JsonPropertyDictionary<T> _parent;
Expand All @@ -30,7 +23,7 @@ public ValueCollection(JsonPropertyDictionary<T> jsonObject)

public T this[int index]
{
get => _parent.List[index].Value;
get => _parent.GetAt(index).Value;
set => throw ThrowHelper.GetNotSupportedException_CollectionIsReadOnly();
}

Expand All @@ -42,13 +35,25 @@ IEnumerator IEnumerable.GetEnumerator()
}
}

public void Add(T jsonNode) => ThrowHelper.ThrowNotSupportedException_CollectionIsReadOnly();
public void Add(T value) => ThrowHelper.ThrowNotSupportedException_CollectionIsReadOnly();

public void Clear() => ThrowHelper.ThrowNotSupportedException_CollectionIsReadOnly();

public bool Contains(T jsonNode) => _parent.ContainsValue(jsonNode);
public bool Contains(T value)
{
EqualityComparer<T> comparer = _parent._valueComparer;
foreach (KeyValuePair<string, T> item in _parent._propertyList)
{
if (comparer.Equals(item.Value, value))
{
return true;
}
}

return false;
}

public void CopyTo(T[] nodeArray, int index)
public void CopyTo(T[] destination, int index)
{
if (index < 0)
{
Expand All @@ -57,12 +62,12 @@ public void CopyTo(T[] nodeArray, int index)

foreach (KeyValuePair<string, T> item in _parent)
{
if (index >= nodeArray.Length)
if (index >= destination.Length)
{
ThrowHelper.ThrowArgumentException_ArrayTooSmall(nameof(nodeArray));
ThrowHelper.ThrowArgumentException_ArrayTooSmall(nameof(destination));
}

nodeArray[index++] = item.Value;
destination[index++] = item.Value;
}
}

Expand All @@ -74,9 +79,9 @@ public IEnumerator<T> GetEnumerator()
}
}

bool ICollection<T>.Remove(T node) => throw ThrowHelper.GetNotSupportedException_CollectionIsReadOnly();
bool ICollection<T>.Remove(T value) => throw ThrowHelper.GetNotSupportedException_CollectionIsReadOnly();
public int IndexOf(T item) => throw ThrowHelper.GetNotSupportedException_CollectionIsReadOnly();
public void Insert(int index, T item) => throw ThrowHelper.GetNotSupportedException_CollectionIsReadOnly();
public void Insert(int index, T value) => throw ThrowHelper.GetNotSupportedException_CollectionIsReadOnly();
public void RemoveAt(int index) => throw ThrowHelper.GetNotSupportedException_CollectionIsReadOnly();
}
}
Expand Down
Loading