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 all commits
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
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