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

Fix JsonDynamic related object serialisation problem in Jint. #16298

Merged
merged 32 commits into from
Jun 30, 2024

Conversation

hyzx86
Copy link
Contributor

@hyzx86 hyzx86 commented Jun 12, 2024

fixes: #16290

fixes: #16292 (comment)

this PR shuld merge after #16292

@hyzx86
Copy link
Contributor Author

hyzx86 commented Jun 12, 2024

Hi @lahma , can you help review this PR? Thanks!

@lahma
Copy link
Contributor

lahma commented Jun 12, 2024

Anything related to SetWrapObjectHandler related to STJ sounds like Jint isn't doing good enough job, so might be worth checking if it could.

@gvkries
Copy link
Contributor

gvkries commented Jun 13, 2024

Anything related to SetWrapObjectHandler related to STJ sounds like Jint isn't doing good enough job, so might be worth checking if it could.

I think our problem here is more specific and it is not really a problem of Jint. We have an additional layer to support dynamics with STJ and when doing serialization inside Jint, this layer must be ignored.

I experimented with another approach by replacing the "toJSON" method, but don't know enough about Jint:

var engine = new Engine(options =>
{
    options.Interop.MemberAccessor = (eng, target, member) =>
    {
        if (target is JsonDynamicValue || target is JsonDynamicObject || target is JsonDynamicArray)
        {
            if (member == "toJSON")
            {
                return new ClrFunction(eng, "toJSON", (thisObject, _) =>
                {
                    var wrappedInstance = ((IObjectWrapper)thisObject).Target;

                    return JConvert.SerializeObject(wrappedInstance, JOptions.Default);
                });
            }
        }

        return null;
    };
});

@hyzx86
Copy link
Contributor Author

hyzx86 commented Jun 13, 2024

Anything related to SetWrapObjectHandler related to STJ sounds like Jint isn't doing good enough job, so might be worth checking if it could.

I think Jint is good enough, it's just that STJ is too rudimentary. So we need to put a lot of effort into adapting it.

I experimented with another approach by replacing the "toJSON" method, but don't know enough about Jint:

This would also solve the problem, my solution would be to have the JsonDynamicXX type converted to an STJ object, as Jint already handles STJ objects by default

@gvkries
Copy link
Contributor

gvkries commented Jun 13, 2024

This would also solve the problem, my solution would be to have the JsonDynamicXX type converted to an STJ object, as Jint already handles STJ objects by default

This sounds far more reasonable 👍

@hishamco
Copy link
Member

If this rely on #16292, please don't merge until the first PR has been merged

@lahma
Copy link
Contributor

lahma commented Jun 20, 2024

Suggestion, make every new introduced class sealed by default. Maybe even internal if possible.

@hyzx86
Copy link
Contributor Author

hyzx86 commented Jun 21, 2024

Suggestion, make every new introduced class sealed by default. Maybe even internal if possible.

Thanks , this problem has been solved in another PR, I just added the Jint processing to PR #16292 (comment)

Copy link
Contributor

This pull request has merge conflicts. Please resolve those before requesting a review.

hyzx86 and others added 3 commits June 27, 2024 16:26
Co-authored-by: Georg von Kries <georg.von.kries@creativbox.net>
Co-authored-by: Georg von Kries <georg.von.kries@creativbox.net>
@hyzx86 hyzx86 requested a review from gvkries June 27, 2024 13:58
hyzx86 and others added 2 commits June 30, 2024 15:59
Co-authored-by: Hisham Bin Ateya <hishamco_2007@yahoo.com>
@hyzx86
Copy link
Contributor Author

hyzx86 commented Jun 30, 2024

@hishamco This PR is ready to merge 😊

@hishamco hishamco merged commit b95eb7c into OrchardCMS:main Jun 30, 2024
5 checks passed
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

Successfully merging this pull request may close these issues.

Getting value from JsonDynamicValue in Jint
5 participants