Json::encode() will not escape ampersands (& to \u0026) when the JSON_HEX_AMP flag is used. Instead, it will pretty print the output. The cause is that Json::PRETTY is defined as 0b0010, which equals 2, the same value as the PHP constant JSON_HEX_AMP.
Escaping ampersands is helpful for embedding json data into HTML.
Steps To Reproduce
Compare the output of json_encode against the output of Nette\Utils\Json::encode() when using the flag JSON_HEX_AMP.
echo json_encode($urls, JSON_HEX_AMP);
// VS
echo Nette\Utils\Json::encode($urls, JSON_HEX_AMP);
The second parameter was not intended to be used for JSON_* constants, but for JSON::* constants. But I guess it could be modified to work with standard constants as well.
Bug Description
Json::encode()
will not escape ampersands (&
to\u0026
) when theJSON_HEX_AMP
flag is used. Instead, it will pretty print the output. The cause is thatJson::PRETTY
is defined as0b0010
, which equals2
, the same value as the PHP constantJSON_HEX_AMP
.Escaping ampersands is helpful for embedding json data into HTML.
Steps To Reproduce
Compare the output of
json_encode
against the output ofNette\Utils\Json::encode()
when using the flagJSON_HEX_AMP
.Expected Behavior
Given the following array:
Calling
Nette\Utils\Json::encode($urls, JSON_HEX_AMP);
should output:Instead, it outputs:
Possible Solution
Change the value of
Nette\Utils\Json::PRETTY
to something that is not already mapped to a built-in JSON flag.