php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53946 add json_encode option for not escaping unnecessary character
Submitted: 2011-02-07 10:22 UTC Modified: 2012-01-06 16:30 UTC
Votes:55
Avg. Score:4.6 ± 0.8
Reproduced:46 of 47 (97.9%)
Same Version:32 (69.6%)
Same OS:24 (52.2%)
From: christian dot pernot at pingroom dot net Assigned: scottmac (profile)
Status: Closed Package: JSON related
PHP Version: 5.3.5 OS:
Private report: No CVE-ID: None
 [2011-02-07 10:22 UTC] christian dot pernot at pingroom dot net
Description:
------------
It would be great to have a option for json_encode which make possible to keep non-ascii character "as such" in strings (and not \uxxxx), which is the case for example with mozilla's JSON.stringify.

json.org states string can have "any-Unicode-character-except-"-or-\-or-control-character"

Test script:
---------------
$str = "test accentué";
echo json_encode($str);

gives 

"test accentu\u00e9"

Expected result:
----------------
"test accentué"

Actual result:
--------------
"test accentu\u00e9"

Patches

json_unescaped_unicode (last revision 2011-08-29 14:04 UTC by gwynne@php.net)
json_encode_doc.patch (last revision 2011-08-29 09:01 UTC by irker@php.net)
bug53946.phpt (last revision 2011-07-25 06:18 UTC by irker@php.net)
json_unescaped_unicode.patch (last revision 2011-07-19 16:02 UTC by irker@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-10 20:42 UTC] scottmac@php.net
-Assigned To: +Assigned To: scottmac
 [2011-02-10 20:42 UTC] scottmac@php.net
Seems reasonable to add.
 [2011-03-25 17:59 UTC] trolzen at gmail dot com
Maybe it's worth mentioning in manual that json_encode converts national characters into strings like \uxxxx and that there is no way to change this behaviour yet?
 [2011-07-19 12:01 UTC] irker@php.net
The following patch has been added/updated:

Patch Name: json_unescaped_unicode
Revision:   1311091293
URL:        https://bugs.php.net/patch-display.php?bug=53946&patch=json_unescaped_unicode&revision=1311091293
 [2011-07-19 12:02 UTC] irker@php.net
The following patch has been added/updated:

Patch Name: bug53946.phpt
Revision:   1311091344
URL:        https://bugs.php.net/patch-display.php?bug=53946&patch=bug53946.phpt&revision=1311091344
 [2011-07-19 12:02 UTC] irker@php.net
The following patch has been added/updated:

Patch Name: json_unescaped_unicode.patch
Revision:   1311091375
URL:        https://bugs.php.net/patch-display.php?bug=53946&patch=json_unescaped_unicode.patch&revision=1311091375
 [2011-07-19 12:14 UTC] irker@php.net
The given option is especially necessary, when json is used in ajax responses. The 
size of a line decreases approximately twice.

I have added a patch which the minimum changes in a code adds support given 
parameter.

This parameter doesn't demand  UTF8 to UTF16 encoding. But encoding validate utf8 
line, therefore I hadn't touch it for BC.
 [2011-07-19 12:18 UTC] irker@php.net
Fragment from rfc4627 ( http://www.ietf.org/rfc/rfc4627.txt )
"All Unicode characters may be placed within the
   quotation marks EXCEPT for the characters that MUST BE escaped:
   quotation mark, reverse solidus, and the control characters (U+0000
   through U+001F).

   Any character MAY BE escaped."
 [2011-07-25 02:18 UTC] irker@php.net
The following patch has been added/updated:

Patch Name: bug53946.phpt
Revision:   1311574689
URL:        https://bugs.php.net/patch-display.php?bug=53946&patch=bug53946.phpt&revision=1311574689
 [2011-08-29 08:59 UTC] irker@php.net
The following patch has been added/updated:

Patch Name: json_encode_doc.patch
Revision:   1314608383
URL:        https://bugs.php.net/patch-display.php?bug=53946&patch=json_encode_doc.patch&revision=1314608383
 [2011-08-29 09:01 UTC] irker@php.net
The following patch has been added/updated:

Patch Name: json_encode_doc.patch
Revision:   1314608487
URL:        https://bugs.php.net/patch-display.php?bug=53946&patch=json_encode_doc.patch&revision=1314608487
 [2011-08-29 12:57 UTC] gwynne@php.net
The following patch has been added/updated:

Patch Name: json_unescaped_unicode
Revision:   1314622649
URL:        https://bugs.php.net/patch-display.php?bug=53946&patch=json_unescaped_unicode&revision=1314622649
 [2011-08-29 14:04 UTC] gwynne@php.net
The following patch has been added/updated:

Patch Name: json_unescaped_unicode
Revision:   1314626654
URL:        https://bugs.php.net/patch-display.php?bug=53946&patch=json_unescaped_unicode&revision=1314626654
 [2011-08-29 14:56 UTC] gwynne@php.net
Automatic comment from SVN on behalf of gwynne
Revision: http://svn.php.net/viewvc/?view=revision&revision=315707
Log: Add unescaped Unicode encoding to json_encode(). Closes bug #53946. Patch by Irker and Gwynne.
 [2011-08-29 14:57 UTC] gwynne@php.net
Automatic comment from SVN on behalf of gwynne
Revision: http://svn.php.net/viewvc/?view=revision&revision=315708
Log: Add test for #53946 to 5.4 (missed it when committing revision 315707)
 [2011-08-29 15:08 UTC] gwynne@php.net
Automatic comment from SVN on behalf of gwynne
Revision: http://svn.php.net/viewvc/?view=revision&revision=315710
Log: Added NEWS note for #53946
 [2011-08-29 16:21 UTC] gwynne@php.net
Automatic comment from SVN on behalf of gwynne
Revision: http://svn.php.net/viewvc/?view=revision&revision=315718
Log: Add documentation for the JSON_UNESCAPED_UNICODE flag (bug #53946)
 [2012-01-06 16:30 UTC] irker@php.net
-Status: Assigned +Status: Closed
 [2012-07-27 05:59 UTC] fetr at gmail dot com
very nice
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 09:01:30 2024 UTC