Skip to content

Commit

Permalink
Implement more conversions from JsonDynamicValue (#15816)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Mike Alhayek <[email protected]>
Co-authored-by: Sebastien Ros <[email protected]>
  • Loading branch information
3 people authored May 14, 2024
1 parent 0c00316 commit 59f2e1d
Show file tree
Hide file tree
Showing 6 changed files with 875 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,8 @@ public class JsonDynamicArray : DynamicObject, IEnumerable<JsonNode?>

public object? this[int index]
{
get
{
var value = GetValue(index);
if (value is JsonDynamicValue jsonDynamicValue)
{
return jsonDynamicValue.JsonValue;
}

return value;
}
set
{
SetValue(index, value);
}
get => GetValue(index);
set => SetValue(index, value);
}

public bool Remove(JsonNode? item)
Expand All @@ -57,14 +45,7 @@ public void RemoveAt(int index)

public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object? result)
{
var value = GetValue((int)indexes[0]);
if (value is JsonDynamicValue jsonDynamicValue)
{
result = jsonDynamicValue.Value;
return true;
}

result = value;
result = GetValue((int)indexes[0]);
return true;
}

Expand Down Expand Up @@ -116,7 +97,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object?[]? args,
return null;
}

public void SetValue(int index, object? value, object? nodeValue = null)
public void SetValue(int index, object? value)
{
if (value is null)
{
Expand All @@ -127,8 +108,7 @@ public void SetValue(int index, object? value, object? nodeValue = null)

if (value is not JsonNode)
{
var jsonNode = JNode.FromObject(value);
SetValue(index, jsonNode, value);
value = JNode.FromObject(value);
}

if (value is JsonObject jsonObject)
Expand All @@ -148,7 +128,7 @@ public void SetValue(int index, object? value, object? nodeValue = null)
if (value is JsonValue jsonValue)
{
_jsonArray[index] = jsonValue;
_dictionary[index] = new JsonDynamicValue(jsonValue, nodeValue);
_dictionary[index] = new JsonDynamicValue(jsonValue);
return;
}
}
Expand Down Expand Up @@ -183,14 +163,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object? result)
return false;
}

var value = GetValue(index);
if (value is JsonDynamicValue jsonDynamicValue)
{
result = jsonDynamicValue.Value;
return true;
}

result = value;
result = GetValue(index);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,27 @@ public class JsonDynamicObject : DynamicObject

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

public JsonDynamicObject() => _jsonObject = [];
public JsonDynamicObject()
{
_jsonObject = [];
}

public JsonDynamicObject(JsonObject jsonObject) => _jsonObject = jsonObject;
public JsonDynamicObject(JsonObject jsonObject)
{
_jsonObject = jsonObject;
}

public int Count => _jsonObject.Count;

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

public object? this[string key]
{
get
{
var value = GetValue(key);
if (value is JsonDynamicValue jsonDynamicValue)
{
return jsonDynamicValue.JsonValue;
}

return value;
}
set
{
SetValue(key, value);
}
get => GetValue(key);
set => SetValue(key, value);
}

public override bool TryGetMember(GetMemberBinder binder, out object? result)
Expand All @@ -57,14 +53,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object? result)
return true;
}

var value = GetValue(binder.Name);
if (value is JsonDynamicValue jsonDynamicValue)
{
result = jsonDynamicValue.Value;
return true;
}

result = value;
result = GetValue(binder.Name);
return true;
}

Expand Down Expand Up @@ -124,7 +113,7 @@ public bool Remove(string key)
return null;
}

public void SetValue(string key, object? value, object? nodeValue = null)
public void SetValue(string key, object? value)
{
if (value is null)
{
Expand All @@ -135,8 +124,7 @@ public void SetValue(string key, object? value, object? nodeValue = null)

if (value is not JsonNode)
{
var jsonNode = JNode.FromObject(value);
SetValue(key, jsonNode, value);
value = JNode.FromObject(value);
}

if (value is JsonObject jsonObject)
Expand All @@ -156,7 +144,7 @@ public void SetValue(string key, object? value, object? nodeValue = null)
if (value is JsonValue jsonValue)
{
_jsonObject[key] = jsonValue;
_dictionary[key] = new JsonDynamicValue(jsonValue, nodeValue);
_dictionary[key] = new JsonDynamicValue(jsonValue);
return;
}
}
Expand Down
Loading

0 comments on commit 59f2e1d

Please sign in to comment.