ID: 45563
User updated by: coyote4til7 at gmail dot com
Reported By: coyote4til7 at gmail dot com
Status: Bogus
Bug Type: JSON related
Operating System: Linux
PHP Version: 5.2.6
New Comment:
Jani, please read the definition of string in the spec. Strings do not
have to be quoted for JSON. That includes member (names).
Since JSON can be and is parsed via Javascript's eval (in fact that was
the method everyone used in the beginning and that is still used in
certain circumstances) and JavaScript's spec specifies that object names
are _not_ to be quoted, php's code can handle JSON that's coded properly
to make sure it doesn't blow up a strictly implemented JavaScript
interpreter.
So, let's see if I can summarize. PHP's JSON support isn't about
JavaScript and so it doesn't support a major real-world use case. So,
the JSON support in PHP is a fairly masturbatory exercise, no?
Previous Comments:
------------------------------------------------------------------------
[2008-07-22 14:04:35] [EMAIL PROTECTED]
Note: This is about JSON, it's not about Javascript.
------------------------------------------------------------------------
[2008-07-22 14:03:46] [EMAIL PROTECTED]
Actually you're wrong. From the RFC 4627:
object = begin-object [ member *( value-separator member ) ]
end-object
member = string name-separator value
string = quotation-mark *char quotation-mark
------------------------------------------------------------------------
[2008-07-19 13:00:23] coyote4til7 at gmail dot com
Description:
------------
When you encode a proper javascript object as json and then use the
json_decode function, it fails.
The keys in Javascript objects are _not_ supposed to be quoted. If
you're going to store a json-encoded object (say in a database) and then
reuse it later in javascript, quoting the keys so that php's json_decode
function works means that you're counting on the browser's javascript to
support something that is not supported by the javascript specification.
Reproduce code:
---------------
// In javascript, we have an object:
// var obj = { parent_id:-1,label:"label" };
//
// In javascript, the keys for objects are _not_ quoted by
// single or double quotes. So, if we're going to properly encode
// this to later reuse it in javascript, we get this sent to php:
$json = '[{parent_id:-1,label:"label"}]';
// Let's try to convert it:
var_dump(json_decode($json, true));
Expected result:
----------------
{ ["parent_id"]=> int(-1) ["label"]=> string(5) "label" }
Actual result:
--------------
NULL
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45563&edit=1