diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicObject.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicObject.cs index 432285a9b97..707cf5160e9 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicObject.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicObject.cs @@ -4,6 +4,7 @@ using System.Reflection; using System.Text.Json.Nodes; using System.Text.Json.Settings; +using Json.More; #nullable enable @@ -52,7 +53,6 @@ public override bool TryGetMember(GetMemberBinder binder, out object? result) result = "{null}"; return true; } - result = GetValue(binder.Name); return true; } @@ -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); } diff --git a/test/OrchardCore.Tests/Data/JsonDynamicTests.cs b/test/OrchardCore.Tests/Data/JsonDynamicTests.cs index 5060531a1d8..fe0c8e6ec1e 100644 --- a/test/OrchardCore.Tests/Data/JsonDynamicTests.cs +++ b/test/OrchardCore.Tests/Data/JsonDynamicTests.cs @@ -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; @@ -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(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; } + } }