Skip to content

Json::encode does not support the flag JSON_HEX_AMP #275

Closed
@iambrosi

Description

@iambrosi

Bug Description

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);

Expected Behavior

Given the following array:

$urls = ['http://foo.com?a=b&b=c', 'http://bar.com?a=b&b=c&d=a%20e'];

Calling Nette\Utils\Json::encode($urls, JSON_HEX_AMP); should output:

["http:\/\/foo.com?a=b\u0026b=c","http:\/\/bar.com?a=b\u0026b=c\u0026d=a%20e"]

Instead, it outputs:

[
    "http://foo.com?a=b&b=c",
    "http://bar.com?a=b&b=c&d=a%20e"
]

Possible Solution

Change the value of Nette\Utils\Json::PRETTY to something that is not already mapped to a built-in JSON flag.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions