Skip to content

Commit

Permalink
Add JsonPropertyDictionary and associated refactoring (#53206)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveharter committed Jun 10, 2021
1 parent 697ac03 commit 43b1f2c
Show file tree
Hide file tree
Showing 14 changed files with 741 additions and 630 deletions.
5 changes: 3 additions & 2 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<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 @@ -61,8 +64,6 @@
<Compile Include="System\Text\Json\Nodes\JsonObject.cs" />
<Compile Include="System\Text\Json\Nodes\JsonObject.Dynamic.cs" />
<Compile Include="System\Text\Json\Nodes\JsonObject.IDictionary.cs" />
<Compile Include="System\Text\Json\Nodes\JsonObject.KeyCollection.cs" />
<Compile Include="System\Text\Json\Nodes\JsonObject.ValueCollection.cs" />
<Compile Include="System\Text\Json\Nodes\JsonValue.CreateOverloads.cs" />
<Compile Include="System\Text\Json\Nodes\JsonValue.cs" />
<Compile Include="System\Text\Json\Nodes\JsonValueNotTrimmable.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,43 @@
using System.Collections;
using System.Collections.Generic;

namespace System.Text.Json.Nodes
namespace System.Text.Json
{
public partial class JsonObject
internal partial class JsonPropertyDictionary<T>
{
private KeyCollection? _keyCollection;

private KeyCollection GetKeyCollection(JsonObject jsonObject)
public ICollection<string> GetKeyCollection()
{
CreateList();
return _keyCollection ??= new KeyCollection(jsonObject);
return _keyCollection ??= new KeyCollection(this);
}

private sealed class KeyCollection : ICollection<string>
{
private readonly JsonObject _jObject;
private readonly JsonPropertyDictionary<T> _parent;

public KeyCollection(JsonObject jsonObject)
public KeyCollection(JsonPropertyDictionary<T> jsonObject)
{
_jObject = jsonObject;
_parent = jsonObject;
}

public int Count => _jObject.Count;
public int Count => _parent.Count;

public bool IsReadOnly => true;

IEnumerator IEnumerable.GetEnumerator()
{
foreach (KeyValuePair<string, JsonNode?> item in _jObject)
foreach (KeyValuePair<string, T?> item in _parent)
{
yield return item.Key;
}
}

public void Add(string propertyName) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();


public void Clear() => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();


public bool Contains(string propertyName) => _jObject.ContainsNode(propertyName);

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

public void CopyTo(string[] propertyNameArray, int index)
{
Expand All @@ -53,7 +49,7 @@ public void CopyTo(string[] propertyNameArray, int index)
ThrowHelper.ThrowArgumentOutOfRangeException_NodeArrayIndexNegative(nameof(index));
}

foreach (KeyValuePair<string, JsonNode?> item in _jObject)
foreach (KeyValuePair<string, T?> item in _parent)
{
if (index >= propertyNameArray.Length)
{
Expand All @@ -66,7 +62,7 @@ public void CopyTo(string[] propertyNameArray, int index)

public IEnumerator<string> GetEnumerator()
{
foreach (KeyValuePair<string, JsonNode?> item in _jObject)
foreach (KeyValuePair<string, T?> item in _parent)
{
yield return item.Key;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,52 @@
using System.Collections;
using System.Collections.Generic;

namespace System.Text.Json.Nodes
namespace System.Text.Json
{
public partial class JsonObject
internal partial class JsonPropertyDictionary<T>
{
private ValueCollection? _valueCollection;

private ValueCollection GetValueCollection(JsonObject jsonObject)
public ICollection<T?> GetValueCollection()
{
CreateList();
return _valueCollection ??= new ValueCollection(jsonObject);
return _valueCollection ??= new ValueCollection(this);
}

private sealed class ValueCollection : ICollection<JsonNode?>
private sealed class ValueCollection : ICollection<T?>
{
private readonly JsonObject _jObject;
private readonly JsonPropertyDictionary<T> _parent;

public ValueCollection(JsonObject jsonObject)
public ValueCollection(JsonPropertyDictionary<T> jsonObject)
{
_jObject = jsonObject;
_parent = jsonObject;
}

public int Count => _jObject.Count;
public int Count => _parent.Count;

public bool IsReadOnly => true;

IEnumerator IEnumerable.GetEnumerator()
{
foreach (KeyValuePair<string, JsonNode?> item in _jObject)
foreach (KeyValuePair<string, T?> item in _parent)
{
yield return item.Value;
}
}

public void Add(JsonNode? jsonNode) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();

public void Add(T? jsonNode) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();

public void Clear() => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();

public bool Contains(T? jsonNode) => _parent.ContainsValue(jsonNode);

public bool Contains(JsonNode? jsonNode) => _jObject.ContainsNode(jsonNode);

public void CopyTo(JsonNode?[] nodeArray, int index)
public void CopyTo(T?[] nodeArray, int index)
{
if (index < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException_NodeArrayIndexNegative(nameof(index));
}

foreach (KeyValuePair<string, JsonNode?> item in _jObject)
foreach (KeyValuePair<string, T?> item in _parent)
{
if (index >= nodeArray.Length)
{
Expand All @@ -63,15 +60,15 @@ public void CopyTo(JsonNode?[] nodeArray, int index)
}
}

public IEnumerator<JsonNode?> GetEnumerator()
public IEnumerator<T?> GetEnumerator()
{
foreach (KeyValuePair<string, JsonNode?> item in _jObject)
foreach (KeyValuePair<string, T?> item in _parent)
{
yield return item.Value;
}
}

bool ICollection<JsonNode?>.Remove(JsonNode? node) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
bool ICollection<T?>.Remove(T? node) => throw ThrowHelper.NotSupportedException_NodeCollectionIsReadOnly();
}
}
}
Loading

0 comments on commit 43b1f2c

Please sign in to comment.