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

System.Text.Json Error: The node must be of type 'JsonObject' #1798

Closed
hyzx86 opened this issue Mar 5, 2024 · 1 comment
Closed

System.Text.Json Error: The node must be of type 'JsonObject' #1798

hyzx86 opened this issue Mar 5, 2024 · 1 comment

Comments

@hyzx86
Copy link
Contributor

hyzx86 commented Mar 5, 2024

An error will be reported in the following scenarios

    [Fact]
    public void AccessingJsonObjectShouldWork()
    {
        var o = new JsonObject
        {
            ["name"] = "test-name"
        };
        _engine.SetValue("o", o);
        Assert.True(_engine.Evaluate("return o.name == 'test-name'").AsBoolean());
    }
 System.InvalidOperationException : The node must be of type 'JsonObject'.

test case pr : #1797

relate : OrchardCMS/OrchardCore#15449

@hyzx86
Copy link
Contributor Author

hyzx86 commented Mar 5, 2024

use the following configuration to resolve

var engine = new Engine(options =>
{
// make JsonArray behave like JS array
options.Interop.WrapObjectHandler = static (e, target, type) =>
{
var wrapped = new ObjectWrapper(e, target);
if (target is JsonArray)
{
wrapped.Prototype = e.Intrinsics.Array.PrototypeObject;
}
return wrapped;
};
// we cannot access this[string] with anything else than JsonObject, otherwise itw will throw
options.Interop.TypeResolver = new TypeResolver
{
MemberFilter = static info =>
{
if (info.ReflectedType != typeof(JsonObject) && info.Name == "Item" && info is PropertyInfo p)
{
var parameters = p.GetIndexParameters();
return parameters.Length != 1 || parameters[0].ParameterType != typeof(string);
}
return true;
}
};
});

@hyzx86 hyzx86 closed this as completed Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant