Skip to content

Commit

Permalink
Adds more features back that were lost due to the removal of Newtonso…
Browse files Browse the repository at this point in the history
…ft (#16292)

---------

Co-authored-by: Sébastien Ros <sebastienros@gmail.com>
Co-authored-by: Mike Alhayek <mike@crestapps.com>
Co-authored-by: Hisham Bin Ateya <hishamco_2007@yahoo.com>
  • Loading branch information
4 people authored Jun 26, 2024
1 parent 906f5a9 commit 51eb7b8
Show file tree
Hide file tree
Showing 6 changed files with 1,002 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using System.Linq;
using System.Reflection;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

#nullable enable

namespace System.Text.Json.Dynamic;

[DebuggerDisplay("JsonDynamicArray[{Count}]")]
public class JsonDynamicArray : DynamicObject, IEnumerable<object?>, IEnumerable<JsonNode?>
[JsonConverter(typeof(JsonDynamicJsonConverter<JsonDynamicArray>))]
public sealed class JsonDynamicArray : JsonDynamicBase, IEnumerable<object?>, IEnumerable<JsonNode?>
{
private readonly JsonArray _jsonArray;
private readonly Dictionary<int, object?> _dictionary = [];
Expand All @@ -22,6 +24,8 @@ public class JsonDynamicArray : DynamicObject, IEnumerable<object?>, IEnumerable

public int Count => _jsonArray.Count;

public override JsonNode Node => _jsonArray;

public object? this[int index]
{
get => GetValue(index);
Expand Down Expand Up @@ -140,7 +144,7 @@ public void SetValue(int index, object? value)
}
}

IEnumerator<JsonNode?> IEnumerable<JsonNode?>.GetEnumerator()
IEnumerator<JsonNode?> IEnumerable<JsonNode?>.GetEnumerator()
=> _jsonArray.AsEnumerable().GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Dynamic;
using System.Text.Json.Nodes;

#nullable enable

namespace System.Text.Json.Dynamic;

public abstract class JsonDynamicBase : DynamicObject
{
public abstract JsonNode? Node { get; }

public T? ToObject<T>() => Node.ToObject<T>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
using System.Dynamic;
using System.Reflection;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.Json.Settings;

#nullable enable

namespace System.Text.Json.Dynamic;

[DebuggerDisplay("JsonDynamicObject[{Count}]")]
public class JsonDynamicObject : DynamicObject
[JsonConverter(typeof(JsonDynamicJsonConverter<JsonDynamicObject>))]
public sealed class JsonDynamicObject : JsonDynamicBase
{
private readonly JsonObject _jsonObject;

private readonly Dictionary<string, object?> _dictionary = [];

public JsonDynamicObject()
Expand All @@ -28,17 +29,19 @@ public JsonDynamicObject(JsonObject jsonObject)

public int Count => _jsonObject.Count;

public void Merge(JsonNode? content, JsonMergeSettings? settings = null)
{
_jsonObject.Merge(content, settings);
}
public override JsonNode Node => _jsonObject;

public object? this[string key]
{
get => GetValue(key);
set => SetValue(key, value);
}

public void Merge(JsonNode? content, JsonMergeSettings? settings = null)
{
_jsonObject.Merge(content, settings);
}

public override bool TryGetMember(GetMemberBinder binder, out object? result)
{
if (binder.Name == "{No Member}")
Expand Down
Loading

0 comments on commit 51eb7b8

Please # to comment.