The JSON grammar explicitly says that it's not supported.

2. JSON Grammar
A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.
A JSON text is a serialized object or array.
        JSON-text = object / array
---
As you can see the base type should be object / array.
To further test this I checked the python, ruby and perl implementations and none of them support primitive types that aren't in an array or object. I then went and looked at the various JavaScript libraries and the browser implementations and they all rejected the primitive types and threw an error when trying to decode it.
Scott
On 13 Dec 2008, at 18:17, Ilia Alshanetsky wrote:

Lukas,

Reading the spec it clearly says in the pre-amble that JSON can represent both primitive and complex data types

From RFC:
"JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays)."

The RFC also says that when it comes to parsers anything that matches the JSON grammar is valid, it even goes further that accepting "extensions" on top of the grammar is fine as well

RFC:
"A JSON parser MUST accept all texts that conform to the JSON grammar. A JSON parser MAY accept non-JSON forms or extensions."






On 13-Dec-08, at 1:05 PM, Lukas Kahwe Smith wrote:


On 13.12.2008, at 18:59, Ilia Alshanetsky wrote:


On 12-Dec-08, at 10:50 PM, Scott MacVicar wrote:

Hi All,

Basic types were added to our JSON decoder to PHP 5.2.1, this allows one to use json_encode / json_decode on any of our scalar types. Omar correctly identified #38680 as not a bug but it appears that Ilia added support for this anyway violating the RFC [1]. Maybe there was a reason for this but I'm not sure why?

PHP is typeless language, IMO its conductive to this design to allow encoding of basic types via json_encode, it reduces the code when communicating to/form JavaScript to avoid type detection. While it does violate the RFC IMO the convenience of the feature is definitely worth it.


well seems to me like its safer to stick with RFC as the default. if at all this should be optional. JSON is a standard and this requires everybody to follow it. so using something else should be a conscious decision that is also visible during code review.

regards,
Lukas Kahwe Smith
m...@pooteeweet.org




Ilia Alshanetsky






--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to