Skip to content

JSON.stringify should use JsonSerializable interface if possible #494

Closed
@colinmollenhour

Description

@colinmollenhour

Awesome work on this extension!

It would great if calling JSON.stringify() on a PHP object would make use of that object's jsonSerialize method if the object implements JsonSerializable. That is, the JSON.stringify() result would be easily controlled by the PHP author and have the same outcome as json_encode().

<?php
class Foo implements JsonSerializable {
    public $bar = 'bar';

    public function jsonSerialize()
    {
        return ['bar' => $this->bar];
    }
}
$v8 = new V8Js();
$v8->foo = new Foo;
$v8->executeString('
print( JSON.stringify(PHP.foo) );
');

Given the above code the current result is:

{"$bar":"bar"}

But the desired result is:

{"bar":"bar"}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions