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

Returned string from overloaded toJSON is being qouted #194

Open
rmja opened this issue Oct 9, 2019 · 3 comments
Open

Returned string from overloaded toJSON is being qouted #194

rmja opened this issue Oct 9, 2019 · 3 comments

Comments

@rmja
Copy link
Contributor

rmja commented Oct 9, 2019

Following the suggestion from #193 the Value property on my custom JSObject's now inherit from JSObject. This causes the following test to not pass, hence my custom json serializer registered on the global context is not invoked:
image

I then tried to override the "toJSON" property as follows:
image

The problem is then that the returned string gets qouted because of the recursive call to stringifyImpl() instead of being returned directly:

image

Should this code instead be:

if (toJSONmemb._valueType == JSValueType.Function)
                {
                    var json = (toJSONmemb._oValue as Function).Call(obj, null);
                    if (json.Value is string jsonString)
                    {
                        return jsonString;
                    }
                    return stringifyImpl("", json, null, null, space, processed, null);
                }

?

@nilproject
Copy link
Owner

nilproject commented Oct 9, 2019

Unfortunately, this is correct behavior of JSON.stringify with toJSON (see this).
Yes, it's weird, but it's not the only weird place in JS.

I think about this trouble and concluded, that condition about JSValue and it's subclasses should be removed. But it's required to solve problem with performance penalty, which will arise after this.

@nilproject
Copy link
Owner

Also you can return instance of another type, which not inherits JSValue and passes through this condition.
Or do not inherit JSValue in you current implementation (see this example. Attributes not required, all works without them)

@rmja
Copy link
Contributor Author

rmja commented Oct 9, 2019

Whoot, year that is strange. Yes, I found that I could return a type not inheriting from JSObject in toJSON. It just thought that it was a bug.

Another "hack" could be to simply return the JSON string directly from toJSON. Then:

if (toJSONmemb._valueType == JSValueType.Function)
     return stringifyImpl("", json, null, null, space, processed, null);
else if (toJSONmemb._valueType == JSValueType.String)
     return (string)toJSONmemb.Value;

I know that it is a hack, but it would avoid introducing the performance regression that you are mentioning.

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

2 participants