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

Implement more conversions from JsonDynamicValue #15816

Merged
merged 16 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Expand Up @@ -27,20 +27,8 @@ public class JsonDynamicObject : DynamicObject

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 +45,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 @@ -127,7 +108,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 @@ -138,8 +119,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 @@ -159,7 +139,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