Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hyzx86 committed Jun 11, 2024
1 parent 3d3f172 commit 25400cd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using System.Text.Json.Nodes;
using System.Text.Json.Settings;
using Json.More;

#nullable enable

Expand Down Expand Up @@ -52,7 +53,6 @@ public override bool TryGetMember(GetMemberBinder binder, out object? result)
result = "{null}";
return true;
}

result = GetValue(binder.Name);
return true;
}
Expand Down Expand Up @@ -107,6 +107,21 @@ public bool Remove(string key)

if (jsonNode is JsonValue jsonValue)
{
var valueKind = jsonValue.GetValueKind();
switch (valueKind)
{
case JsonValueKind.String:
return _dictionary[key] = jsonValue.GetString();
case JsonValueKind.Number:
return _dictionary[key] = jsonValue.GetNumber();
case JsonValueKind.True:
return _dictionary[key] = true;
case JsonValueKind.False:
return _dictionary[key] = false;
case JsonValueKind.Undefined:
case JsonValueKind.Null:
return _dictionary[key] = null;
}
return _dictionary[key] = new JsonDynamicValue(jsonValue);
}

Expand Down
26 changes: 26 additions & 0 deletions test/OrchardCore.Tests/Data/JsonDynamicTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Dynamic;
using System.Text.Json;
using System.Text.Json.Dynamic;
using System.Text.Json.Nodes;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement;

namespace OrchardCore.Tests.Data;

Expand Down Expand Up @@ -337,4 +341,26 @@ public void JsonDynamicValueMustConvertToUri()

Assert.Equal(expectedValue, (Uri)myDynamic);
}

[Fact]
public void ExpandoObjectSerializeTest()
{
dynamic expandoValue = new ExpandoObject();

var contentItem = new ContentItem();
contentItem.Alter<TestPart>(part =>
{
part.TextFeildProp = new TextField { Text = "test" };
});
expandoValue.stringValue = contentItem.Content.TestPart.TextFeildProp.Text;

var jsonStr = JConvert.SerializeObject((ExpandoObject)expandoValue);
// Actual:"{\"stringValue\":{\"JsonValue\":\"test\"}}"
Assert.Equal("{\"stringValue\":\"strValue\"}", jsonStr);
}

public class TestPart : ContentPart
{
public TextField TextFeildProp { get; set; }
}
}

0 comments on commit 25400cd

Please sign in to comment.