RE: [json] JSON grammar question
- I'm afraid that Simon is right. The Internet Draft clearly states, "A
JSON text is a serialized object or array." The JSON-text production is
defined as follows:
JSON-text = object / array
I also checked Douglas's JSONWriter in Java
<http://www.json.org/javadoc/org/json/JSONWriter.html> and lo and
behold, the documentation says, "The first method called must be array
or object." I must admit that I totally missed this detail. I had the
JsonTextWriter in C# done before the Internet Draft was published and
before the Java version became available. At the time, json.org is what
I had to do work with and I started with the value production as the
root. I guess I'll have to go and update my implementation now. Simon,
thanks for bringing this to light (even if I am the last one standing to
learn about it today). Incidentally, though, if you didn't go with
XML-RPC and you like JSON so much then why didn't you opt for JSON-RPC?
Single arguments there would have been wrapped naturally in an array
today and you wouldn't be running into this problem, not to invalidate
your question because it is a good one.
From: email@example.com [mailto:firstname.lastname@example.org] On Behalf Of
Sent: Wednesday, July 12, 2006 10:33 PM
Subject: RE: [json] JSON grammar question
"JSON is built on two structures:"
I think if you look a little further into the spec on JSON.org you will
see that in addition to these two basic strutures JSON also allows for
simple value strings that can take on any of the following "data-types"
So, allowing a simple value to exist at the root of your JSON string is
not a violation of the spec at all. In fact not allowing for them is a
violation of the spec.
From: email@example.com <mailto:json%40yahoogroups.com>
[mailto:firstname.lastname@example.org <mailto:json%40yahoogroups.com> ] On Behalf
Of Fang Yidong
Sent: Wednesday, July 12, 2006 11:44 AM
To: email@example.com <mailto:json%40yahoogroups.com>
Subject: Re: [json] JSON grammar question
Agreed. The value seems more logically like a start in
the grammar,although there's a 'hidden' start
(start->object|array) according to the nonnormal
JSON is built on two structures:
* A collection of name/value pairs. In various
languages, this is realized as an object, record,
struct, dictionary, hash table, keyed list, or
* An ordered list of values. In most languages,
this is realized as an array, vector, list, or
In my JSON.simple implementation,I choose to accept
any value as root element,but I often remind myself
that I may have violated the JSON spec.
--- rogue_pedro <rogue_pedro@ <mailto:rogue_pedro%40yahoo.com>
> Hi,[Non-text portions of this message have been removed]
> I really love JSON and I praise the day Douglas came
> up with it. It's
> a truly useful spec, and anything that keeps XML at
> arm's length is
> good news to me. ;-)
> However, I just got bitten by a curious facet of the
> JSON grammar.
> I'm writing a server that was originally going to
> use XML-RPC to
> provide clients with a set of methods. During the
> course of
> development, we (the other developers and I) decided
> that we wanted a
> lighter-weight RPC mechanism, and we settled on a
> simple system that
> basically sends JSON encoded data back and forth
> between the clients
> and server over sockets.
> This worked well until the developer of the PHP
> client code noticed
> that the JSON decoder returned null for single args,
> i.e. anything
> other than a object or array. We figured this was
> an error in the
> encoder, since the PHP encoder, and the python
> encoder/decoder we're
> using server-side, all accepted single args without
> I was so sure that this must be a bug that, after
> json.org, I wrote to the php-json list and wondered
> aloud about it.
> Omar Kilani immediately informed me (Thanks Omar!
> :-) that this was
> correct behaviour, but that a *previous* version of
> php-json's decoder
> handled single args if I really wanted that
> In my opinion, the spec outlined on json.org is
> ambiguous about valid
> "start" elements for the grammar. Of course, after
> writing to the
> php-json list and learning that single values
> (outside of an object or
> array) are not valid JSON, I read further and
> discovered that the
> Internet Draft does specifically state that <object
> or array> is the
> valid "start" element for JSON.
> My question is, why?
> Are there good, concrete reasons for this?
> More to the point, could the spec be changed so that
> value is the
> "start" element?
> I know it's kind of silly to pass single ints,
> floats, strings, etc...
> through JSON, but that's not the point. The point,
> for me, is that I
> want to pass *any* data through JSON, so that I can
> [de]serialize the
> input args and return values of my server's methods
> in JSON, without
> having to think about it.
> As it is now, if I want to conform to the standard
> (which I do) then
> *only* lists and dicts (in python terms) are valid
> args to pass to the
> encoder or expect from the decoder.
> This means that I either have to special-case single
> args (i.e. not
> encode/decode them in JSON and signal their
> occurrence somehow), or
> wrap (and unwrap) *all* input/output data in arrays,
> either of which
> incurs unnecessary overhead on *both* the client-
> and server-sides.
> If value were made the "start" element of the JSON
> grammar it would,
> in my opinion, result in a more elegant grammar, and
> it would allow
> the above work to be done transparently in the
> encoder/decoders of the various languages.
> So, are there compelling reasons why the "start"
> element for JSON
> grammar is <object or array> rather than value? And
> if not, could the
> draft be revised to have value as the start element?
> Thanks in advance,
- --- In firstname.lastname@example.org, "Atif Aziz" <atif.aziz@...> wrote:
> thanks for bringing this to light (even if I am the last one standing toNo problem.. I hate to be the bearer of bad news.. :)
> learn about it today).
Incidentally, though, if you didn't go with
> XML-RPC and you like JSON so much then why didn't you opt for JSON-RPC?I just didn't see it at first. www.json.org had all the information I
> Single arguments there would have been wrapped naturally in an array
> today and you wouldn't be running into this problem, not to invalidate
> your question because it is a good one.
needed to get going and our simple RPC implementation was up and
running within a half an hour.
We're considering using JSON-RPC if and when we open our servers up to