Loading ...
Sorry, an error occurred while loading the content.

JSON grammar question

Expand Messages
  • rogue_pedro
    Hi, 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.
    Message 1 of 6 , Jul 12, 2006
    • 0 Attachment
      Hi,

      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 problems.

      I was so sure that this must be a bug that, after double-checking
      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 behaviour.

      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 various
      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,

      Peace,
      ~Simon
    • Michal Migurski
      ... Good question, I also had no idea this was the case. My decoder, JSON-PHP, has no problem with decoding atoms, since it s based on the apparently more
      Message 2 of 6 , Jul 12, 2006
      • 0 Attachment
        > 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 problems.
        >
        > I was so sure that this must be a bug that, after double-checking
        > 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 behaviour.
        >
        > 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?

        Good question, I also had no idea this was the case.

        My decoder, JSON-PHP, has no problem with decoding atoms, since it's
        based on the apparently more liberal spec at http://json.org

        -mike.

        ----------------------------------------------------------------
        michal migurski- contact info and pgp key:
        sf/ca http://mike.teczno.com/contact.html
      • Fang Yidong
        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
        Message 3 of 6 , Jul 12, 2006
        • 0 Attachment
          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
          statment:

          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
          associative array.
          * An ordered list of values. In most languages,
          this is realized as an array, vector, list, or
          sequence.


          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@...>:

          > Hi,
          >
          > 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
          > problems.
          >
          > I was so sure that this must be a bug that, after
          > double-checking
          > 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
          > behaviour.
          >
          > 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
          > various
          > 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,
          >
          > Peace,
          > ~Simon
          >
          >
          >
          >



          --
          JSON: Action in AJAX!

          JSON - http://www.json.org
          JSON.simple - http://www.json.org/java/simple.txt







          ___________________________________________________________
          Mp3疯狂搜-新歌热歌高速下
          http://music.yahoo.com.cn/?source=mail_mailbox_footer
        • Todd
          “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
          Message 4 of 6 , Jul 12, 2006
          • 0 Attachment
            “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”



            string
            number
            true
            false
            null



            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: json@yahoogroups.com [mailto:json@yahoogroups.com] On Behalf Of Fang Yidong
            Sent: Wednesday, July 12, 2006 11:44 AM
            To: json@yahoogroups.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
            statment:

            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
            associative array.
            * An ordered list of values. In most languages,
            this is realized as an array, vector, list, or
            sequence.

            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> yahoo.com>:

            > Hi,
            >
            > 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
            > problems.
            >
            > I was so sure that this must be a bug that, after
            > double-checking
            > 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
            > behaviour.
            >
            > 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
            > various
            > 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,
            >
            > Peace,
            > ~Simon
            >
            >
            >
            >

            --
            JSON: Action in AJAX!

            JSON - http://www.json. <http://www.json.org> org
            JSON.simple - http://www.json. <http://www.json.org/java/simple.txt> org/java/simple.txt





            __________________________________________________________
            Mp3疯狂搜-新歌热歌高速下
            http://music. <http://music.yahoo.com.cn/?source=mail_mailbox_footer> yahoo.com.cn/?source=mail_mailbox_footer





            [Non-text portions of this message have been removed]
          • Atif Aziz
            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
            Message 5 of 6 , Jul 12, 2006
            • 0 Attachment
              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: json@yahoogroups.com [mailto:json@yahoogroups.com] On Behalf Of
              Todd
              Sent: Wednesday, July 12, 2006 10:33 PM
              To: json@yahoogroups.com
              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"

              string
              number
              true
              false
              null

              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: json@yahoogroups.com <mailto:json%40yahoogroups.com>
              [mailto:json@yahoogroups.com <mailto:json%40yahoogroups.com> ] On Behalf
              Of Fang Yidong
              Sent: Wednesday, July 12, 2006 11:44 AM
              To: json@yahoogroups.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
              statment:

              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
              associative array.
              * An ordered list of values. In most languages,
              this is realized as an array, vector, list, or
              sequence.

              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>
              yahoo.com>:

              > Hi,
              >
              > 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
              > problems.
              >
              > I was so sure that this must be a bug that, after
              > double-checking
              > 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
              > behaviour.
              >
              > 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
              > various
              > 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,
              >
              > Peace,
              > ~Simon
              >
              >
              >
              >





              [Non-text portions of this message have been removed]
            • rogue_pedro
              ... No problem.. I hate to be the bearer of bad news.. :) Incidentally, though, if you didn t go with ... I just didn t see it at first. www.json.org had
              Message 6 of 6 , Jul 13, 2006
              • 0 Attachment
                --- In json@yahoogroups.com, "Atif Aziz" <atif.aziz@...> wrote:
                > thanks for bringing this to light (even if I am the last one standing to
                > learn about it today).

                No problem.. I hate to be the bearer of bad news.. :)

                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.


                I just didn't see it at first. www.json.org had all the information I
                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
                public use.

                Peace,
                ~Simon
              Your message has been successfully submitted and would be delivered to recipients shortly.