php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #67599 case mismatch for json_encode between doc and reality
Submitted: 2014-07-09 20:44 UTC Modified: 2014-07-10 09:30 UTC
From: om+bugs dot php dot net at miakinen dot net Assigned: sobak (profile)
Status: Not a bug Package: Documentation problem
PHP Version: 5.4.30 OS: Ubuntu 12.04 (not important)
Private report: No CVE-ID: None
 [2014-07-09 20:44 UTC] om+bugs dot php dot net at miakinen dot net
Description:
------------
---
From manual page: http://www.php.net/function.json-encode
---

<cit.>
Example #2 A json_encode() example showing some options in use 
...
echo "Tags: ",    json_encode($a, JSON_HEX_TAG), "\n";
...
The above example will output:
...
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
</cit.>

Actually, it won't output \u003C and \u003E (upper case) but
\u003c and \u003e (lower case).

Note that the same bug exists in French, and probably also in
other languages.


Test script:
---------------
The test script is the one given in the documentation.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-10 07:23 UTC] sobak@php.net
-Status: Open +Status: Analyzed -Assigned To: +Assigned To: sobak
 [2014-07-10 07:23 UTC] sobak@php.net
On 5.5.6 and 5.4.26 which I have currently available, output is the same as in the manual. Could you provide exact code which gave you result with those characters lowercased?
 [2014-07-10 08:28 UTC] om+bugs dot php dot net at miakinen dot net
I'm sorry, it is in fact a bug in the program and not in the documentation.

You are right that < and > are encoded in upper case, but they are the only ones that behave so !

See that program :

<?php
$json_string = '{
        "ctrl1" : "\\u000B\\u000E\\u000F",
        "ctrl2" : "\\u001B\\u001C\\u001D\\u001E\\u001F",
        "other" : "<> élève grâce à 北京"
}';
$json_value = json_decode($json_string);
$json_string2= json_encode($json_value, JSON_HEX_TAG | JSON_PRETTY_PRINT);
echo var_dump($json_string2);
?>

The display is :
string(160) "{
    "ctrl1": "\u000b\u000e\u000f",
    "ctrl2": "\u001b\u001c\u001d\u001e\u001f",
    "other": "\u003C\u003E \u00e9l\u00e8ve gr\u00e2ce \u00e0 \u5317\u4eac"
}"

As you can see, the control characters are \u000b to \u001f, the French accented letters are \u00e0 to \u00e9, and the second chinese character is \u4eac, all in lower case. Only < and > are \u003C and \u003E in upper case.

Note: In JavaScript, the JSON.stringify function also encodes the control characters in lower case; I don't know how to obtain hexadecimal code for characters above \u001f.
 [2014-07-10 09:16 UTC] sobak@php.net
-Status: Analyzed +Status: Not a bug
 [2014-07-10 09:16 UTC] sobak@php.net
It probably needs futher clarification. I'll try to expand this example to show behavior you've described.
 [2014-07-10 09:30 UTC] om+bugs dot php dot net at miakinen dot net
Ok, and thank you!
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Oct 27 01:01:23 2020 UTC