-
Notifications
You must be signed in to change notification settings - Fork 202
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
JSON.stringify should use JsonSerializable interface if possible #494
Comments
It's possible to do something like toString() -> __toString() ? Did some debugging and found that callback_type == V8JS_PROP_GETTER was not been called. So I changed the ret_value when callback_type == V8JS_PROP_QUERY to v8::None and then V8JS_PROP_GETTER worker, but not with the value returned by jsonSerialize function. |
@chrisbckr Funnily enough, have been looking back on some of these enhancements too (particularly this and the forEach one, #451 ) to see if I can get somewhere and also to dust off my internals knowledge :) I’ll let you know if I find anything. |
@chrisbckr PHP's
However it seems that V8 isn't invoking the proxied method:
if we create a JS object and bind
I haven't (yet) dug deeper into V8 if there's some reason for this behaviour. It might also be worth a try to actively provide a |
from V8's source MaybeHandle<Object> JsonStringifier::ApplyToJsonFunction(Handle<Object> object,
Handle<Object> key) {
HandleScope scope(isolate_);
// Retrieve toJSON function. The LookupIterator automatically handles
// the ToObject() equivalent ("GetRoot") if {object} is a BigInt.
Handle<Object> fun;
LookupIterator it(isolate_, object, tojson_string_,
LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
ASSIGN_RETURN_ON_EXCEPTION(isolate_, fun, Object::GetProperty(&it), Object);
if (!fun->IsCallable()) return object; ... I don't know why they skip interceptors there. But they do :-/ Seems like we would have to actively export a |
Hmmm I had a look at this code today but didn't pay attention enough to this detail. I will see how to check for the interface and how to implement this. Thank you!!! |
There already are interface checks for |
I'll look at this! |
Something like this? |
Thank you @chrisbckr and @stesie !! Ya'll are rock stars! |
Awesome work on this extension!
It would great if calling
JSON.stringify()
on a PHP object would make use of that object'sjsonSerialize
method if the object implementsJsonSerializable
. That is, theJSON.stringify()
result would be easily controlled by the PHP author and have the same outcome asjson_encode()
.Given the above code the current result is:
But the desired result is:
The text was updated successfully, but these errors were encountered: