Edit report at http://bugs.php.net/bug.php?id=49366&edit=1
ID: 49366 Updated by: ahar...@php.net Reported by: don at smugmug dot com -Summary: json_encode incorrectly escapes slashes (/) +Summary: Make slash escaping optional in json_encode() -Status: Open +Status: Closed Type: Feature/Change Request -Package: Feature/Change Request +Package: JSON related Operating System: CentOS 5.3 PHP Version: 5.3.0 -Assigned To: +Assigned To: aharvey Block user comment: N New Comment: This bug has been fixed in SVN. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better. I've implemented this on trunk via a json_encode() option called JSON_UNESCAPED_SLASHES. The default behaviour remains to escape slashes. Previous Comments: ------------------------------------------------------------------------ [2010-09-16 15:53:29] ahar...@php.net Automatic comment from SVN on behalf of aharvey Revision: http://svn.php.net/viewvc/?view=revision&revision=303421 Log: Implemented FR #49366 (Make slash escaping optional in json_encode()). ------------------------------------------------------------------------ [2010-08-08 14:43:22] jd2 at dilltree dot com Totally agree. This is needed for anything such as a REST service that wants to return JSON with valid urls. ------------------------------------------------------------------------ [2009-10-21 21:14:40] jemptymethod at acm dot org I concur with the suggested course of action making the escaping of slashes controllable with a flag as I'm experiencing the same issue as the OP albeit on Debian (rather than CentOS) ------------------------------------------------------------------------ [2009-08-26 08:50:11] j...@php.net It's not incorrect to escape slashes. It's also suggested on the RFC to do so. Just look at the chart on http://www.json.org/ for string.. But since it isn't wrong either NOT to escape them, I'll just reclassify this. We can add another option to that optional bitmask "options". ------------------------------------------------------------------------ [2009-08-25 23:32:14] don at smugmug dot com Description: ------------ When given a string with slashes in it, despite the JSON docs suggesting that's legal, json_encode escapes them anyway. http://www.json.org/ says "any-Unicode-character-except-"-or-\-or control-character" JSONLint at http://www.jsonlint.com/ also validates slashes that aren't escaped as valid. This adds response weight, and makes further processing much more complex. Reproduce code: --------------- $vars['url'] = "http://www.example.com/"; echo json_encode($vars); Expected result: ---------------- {"url":"http://www.example.com/"} Actual result: -------------- {"url":"http:\/\/www.example.com\/"} ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=49366&edit=1