php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49323 json_decode escaped character weirdness
Submitted: 2009-08-21 19:31 UTC Modified: 2010-03-25 13:51 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: paul dot wieland at port80ware dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.3.0 OS: Linux / OSX
Private report: No CVE-ID:
 [2009-08-21 19:31 UTC] paul dot wieland at port80ware dot com
Description:
------------
json_encode is not removing the backslash from escaped forward slashes 
in my strings. It works fine on several machines running 5.2.x, but on 
one new machine running php 5.3.0 it returns the wrong result.

Reproduce code:
---------------
$my_string = 'Black/Gray';
var_dump($my_string);
var_dump(
	json_decode(
		json_encode($my_string)
	)
);


Expected result:
----------------
string(10) "Black/Gray"
string(10) "Black/Gray"

Actual result:
--------------
string(10) "Black/Gray"
string(11) "Black\/Gray"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-21 19:35 UTC] paul dot wieland at port80ware dot com
Very sorry, the virtualhost I was using was not configured to use 
version 5.3.0 - it is using 5.2.1 instead

So I am not sure if this is an old problem with 5.2.1 that has been 
solved, or it still exists with 5.3.0
 [2009-08-22 10:11 UTC] scottmac@php.net
Definitely fixed in 5.3

scott-mbp:~ scott$ php /tmp/test.php
string(10) "Black/Gray"
string(10) "Black/Gray"

 [2010-03-25 12:49 UTC] dmitry dot dulepov at gmail dot com
No, it is definitely NOT fixed.

php -v says:
=================
PHP 5.3.1 (cli) (built: Feb 15 2010 17:30:23) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
=================

Test file:
=================
<?= json_encode(array('test' => 'hello/world')) . chr(10);
=================

Result:
=================
{"test":"hello\/world"}
=================
 [2010-03-25 13:22 UTC] rasmus@php.net
dmitry, I don't understand your last comment.  Of course / is escaped in the 
json encoded string.  According to the spec it is supposed to be.  The important 
part is that when you json_decode() it it comes back without the \ which it 
does:

> php -v
PHP 5.3.3-dev (cli) (built: Feb 10 2010 10:53:36) 

> php -a
php > $enc = json_encode(array('test' => 'hello/world'));
php > echo $enc;
{"test":"hello\/world"}
php > var_dump(json_decode($enc));
object(stdClass)#1 (1) {
  ["test"]=>
  string(11) "hello/world"
}
 [2010-03-25 13:35 UTC] dmitry dot dulepov at gmail dot com
Ok, I see the point... However FireFox does not like that escaping. So any AJAX 
applications that use this generated JSON do not work in FireFox. It is enough to 
have \/ in JSON to cause JavaScript error in FireFox. Should I report the bug to 
FireFox than?
 [2010-03-25 13:51 UTC] rasmus@php.net
No, I wouldn't suggest you do that.  It works perfectly fine in Firefox.  Check 
your own code.  I wrote a quick check to make sure it works in FF.  Point your 
browser at:

http://progphp.com/test.html

Then do a view-source to see what I am doing.
 [2010-03-25 13:58 UTC] paul dot wieland at port80ware dot com
It definitely works just fine in FireFox. The entire project I was working on 
(which prompted this bug post) was tested with FF and everything worked fine.
 [2010-03-25 16:07 UTC] dmitry dot dulepov at gmail dot com
I appologize. The problem was elsewhere. Sorry for the trouble.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 04:01:55 2014 UTC