proposal-well-formed-stringify
proposal-well-formed-stringify copied to clipboard
PHP json_decode throws error with well-formed json
This might be a PHP json_decode issue but wanted to share our production bug here after upgrading to Node 12.
var a = 'π₯π¦π΄πͺπ¨π―π¦π³ π’π―π₯ πͺπππΆπ΄π΅π³π’π΅π°π³';
JSON.stringify(a.slice(0, 15));
// Node 10 output:
'"π₯π¦π΄πͺπ¨π―π¦οΏ½"';
// Node 12 output:
'"π₯π¦π΄πͺπ¨π―π¦\\ud835"'
This response is then sent to a PHP server as JSON and decoded. Which is where the error occurs. Node10's output used to work fine with PHP json_decode but it no longer works with Node12's output.
I simplified the NODE->PHP example see below.
<?php
$string = '{"string": "π₯π¦π΄πͺπ¨π―π¦\\ud835"}';
var_dump(json_decode($string, false, 512, JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE | JSON_INVALID_UTF8_SUBSTITUTE));
// Output:
Fatal error: Uncaught JsonException: Single unpaired UTF-16 surrogate in unicode escape in phptest.php:36
Stack trace:
#0 phptest.php(36): json_decode('{"string": "\xF0\x9D\x98...', false, 512, 7340032)
#1 {main}
thrown in phptest.php on line 36
//Node 10's output works fine.
object(stdClass)#1 (1) {
["string"]=>
string(31) "π₯π¦π΄πͺπ¨π―π¦οΏ½"
}
See #13.
As in that thread,
It seems weird to guard against lone surrogates in escaped form but not in their unescaped form.
Those two representations should be equivalent. You might consider filing a bug against PHP.