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

json2 vs FF3 behaviour - jsonstring in array

Expand Messages
  • nostalgiker
    i noticed that this works with the native JSON object in FF3.5.5 JSON.parse([ true ]) but with the json2.js this throws an error. which one is the intended
    Message 1 of 13 , Dec 16, 2009
    View Source
    • 0 Attachment
      i noticed that this works with the native JSON object in FF3.5.5

      JSON.parse(["true"])

      but with the json2.js this throws an error.

      which one is the intended behaviour?

      greetings
      simon
    • Fredag_d13
      I guess that json2.js is supposed to work on Json-Text and not java arrays with json-text. You could do this instead: JSON.parse( [ true ] );
      Message 2 of 13 , Dec 17, 2009
      View Source
      • 0 Attachment
        I guess that json2.js is supposed to work on Json-Text and not java arrays with json-text.

        You could do this instead:

        JSON.parse('["true"]');


        --- In json@yahoogroups.com, "nostalgiker" <nostalgiker@...> wrote:
        >
        > i noticed that this works with the native JSON object in FF3.5.5
        >
        > JSON.parse(["true"])
        >
        > but with the json2.js this throws an error.
        >
        > which one is the intended behaviour?
        >
        > greetings
        > simon
        >
      • nostalgiker
        okay, i figured that out ;) bwt it s a plain javascript array, not java. but i was actually wondering why IE8 and FF3.5+ allow this with their native JSON
        Message 3 of 13 , Dec 17, 2009
        View Source
        • 0 Attachment
          okay, i figured that out ;) bwt it's a plain javascript array, not java.

          but i was actually wondering why IE8 and FF3.5+ allow this with their native JSON objects - passing an array with one element to parse()

          so does the jquery.json pluggin - which is unmaintained. that is why i switched to json2... where i noticed that it doesnt support this, but everybody else seems to.
        • Alexandre Morgaut
          JavaScript provides automatic type conversions on a lot of action So if you do var foo = [ bar ] + baz ; [ bar ] will be converted into bar and you ll have
          Message 4 of 13 , Dec 17, 2009
          View Source
          • 0 Attachment
            JavaScript provides automatic type conversions on a lot of action

            So if you do

            var foo = ["bar"] + " baz";

            ["bar"] will be converted into "bar" and you'll have foo === "bar baz"

            you can also see that in JavaScript ["foo"] == "foo"

            and String["foo"]) === "foo"

            Then when you send an array to a function expecting a string, and this function doesn't check the type of the parameter ( or force it to string), you'll have what you get


            Le 17 d�c. 2009 � 11:45, nostalgiker a �crit :



            okay, i figured that out ;) bwt it's a plain javascript array, not java.

            but i was actually wondering why IE8 and FF3.5+ allow this with their native JSON objects - passing an array with one element to parse()

            so does the jquery.json pluggin - which is unmaintained. that is why i switched to json2... where i noticed that it doesnt support this, but everybody else seems to.






            [Non-text portions of this message have been removed]
          • nostalgiker
            ... i realize this is mailinglist is about json2.js. but i was wondering why firefox allows passing an array and json2.js not. and which one is the correct
            Message 5 of 13 , Jan 4, 2010
            View Source
            • 0 Attachment
              > JavaScript provides automatic type conversions on a lot of action

              thx for the replies, but i'm not talking about a js function. it's the NATIVE firefox JSON object:

              > JSON.parse(["true"])

              i realize this is mailinglist is about json2.js. but i was wondering why firefox allows passing an array and json2.js not. and which one is the "correct" behaviour?
            • Fredag_d13
              According to this page: http://www.west-wind.com/weblog/posts/729630.aspx, the native implementation is based upon the interface of json2.js. In fact you
              Message 6 of 13 , Jan 4, 2010
              View Source
              • 0 Attachment
                According to this page: http://www.west-wind.com/weblog/posts/729630.aspx, the native implementation is based upon the interface of json2.js. In fact you should always include json2.js, because quote: "The JSON2 implementation includes checks to see if a JSON object exists already and if not leaves it in place so the native version is used." and you don't want to exclude browsers with no native json implementation.

                If the function that takes a javascript-array with json-text is very important to you, then you should write a wrapper function that calls JSON2 for every element in your array.

                I don't know if it is a bug or according to some specs. that native firefox json accepts a javascript-array, but you could try to see if IE8 does the same.


                --- In json@yahoogroups.com, "nostalgiker" <nostalgiker@...> wrote:
                >
                >
                > > JavaScript provides automatic type conversions on a lot of action
                >
                > thx for the replies, but i'm not talking about a js function. it's the NATIVE firefox JSON object:
                >
                > > JSON.parse(["true"])
                >
                > i realize this is mailinglist is about json2.js. but i was wondering why firefox allows passing an array and json2.js not. and which one is the "correct" behaviour?
                >
              • Luke
                Neither are correct. The ECMAScript 5 spec states the first step of JSON.parse is to call ToString( text ). That means JSON.parse([ true ]); should be
                Message 7 of 13 , Jan 5, 2010
                View Source
                • 0 Attachment
                  Neither are correct.

                  The ECMAScript 5 spec states the first step of JSON.parse is to call ToString( text ). That means JSON.parse(["true"]); should be equivalent to JSON.parse("true"); -- barring Array.proto.toString clobbering. FF3.5 and IE8 are in violation of the spec. json2.js also does not perform this step, resulting in a thrown error during the regex scouring because it assumes the input has the 'replace' method.

                  --- In json@yahoogroups.com, "nostalgiker" <nostalgiker@...> wrote:
                  >
                  >
                  > > JavaScript provides automatic type conversions on a lot of action
                  >
                  > thx for the replies, but i'm not talking about a js function. it's the NATIVE firefox JSON object:
                  >
                  > > JSON.parse(["true"])
                  >
                  > i realize this is mailinglist is about json2.js. but i was wondering why firefox allows passing an array and json2.js not. and which one is the "correct" behaviour?
                  >
                • nostalgiker
                  ... thank you, that was the answer is was looking for. should have checked the spec...
                  Message 8 of 13 , Jan 7, 2010
                  View Source
                  • 0 Attachment
                    > Neither are correct.
                    >
                    > The ECMAScript 5 spec states the first step of JSON.parse is
                    > to call ToString( text ).

                    thank you, that was the answer is was looking for. should have checked the spec...
                  • nostalgiker
                    is mr.crockford reading this list? imo this is then a bug in json2.js since JSON.parse([ true ]) fails in his implementation but it should behave like ecma5 s
                    Message 9 of 13 , Jan 7, 2010
                    View Source
                    • 0 Attachment
                      is mr.crockford reading this list? imo this is then a bug in json2.js since JSON.parse(['true']) fails in his implementation but it should behave like ecma5's native JSON.

                      greetings
                      simon
                    • Douglas Crockford
                      ... JSON.parse wants to be called with a string. If you call it with anything else, you get what you deserve. I don t recommend reliance on the edge cases.
                      Message 10 of 13 , Jan 7, 2010
                      View Source
                      • 0 Attachment
                        --- In json@yahoogroups.com, "nostalgiker" <nostalgiker@...> wrote:

                        > is mr.crockford reading this list? imo this is then a bug in json2.js since JSON.parse(['true']) fails in his implementation but it should behave like ecma5's native JSON.


                        JSON.parse wants to be called with a string. If you call it with anything else, you get what you deserve. I don't recommend reliance on the edge cases. That is where systems are the least portable.
                      • Fredag_d13
                        I believe that the EMCAScript standard should be changed. On page 212 in http://www.ecmascript.org/docs/tc39-2009-043.pdf ECMA specifies the JSON-grammar, but
                        Message 11 of 13 , Jan 7, 2010
                        View Source
                        • 0 Attachment
                          I believe that the EMCAScript standard should be changed.
                          On page 212 in http://www.ecmascript.org/docs/tc39-2009-043.pdf
                          ECMA specifies the JSON-grammar, but does not do so by the specification given by Crockford. http://tools.ietf.org/html/rfc4627
                          He specifies (page 2) that:

                          "A JSON text is a serialized object or array.
                          JSON-text = object / array"

                          ECMA says that JSONText is JSONValue, which is too liberal as it would also mean that JSONString, JSONNumber, etc. is JSONText.

                          Doing it the ECMA standard way would (as previously said) convert ["text"] it into 'text' or '"text"' - which isen't JSONText, but that what the ECMAstandard tells you to do.

                          The Native parser is actually a valid JSON parser. The spec says so:
                          "A JSON parser MUST accept all texts that conform to the JSON grammar. A JSON parser MAY accept non-JSON forms or extensions."

                          bummer.

                          But it would be interesting to see if JSON.stringify is outputting JSONText according to the spec given by D.Crockford or the self-invented JSON grammar by ECMA.

                          Anyway - It's always sane to feed a parser text, because a parser works on text (http://en.wikipedia.org/wiki/Parsing) - that's why json2.js should not be changed and ECMAScript 5 should be modified.

                          What you should do, is to encode your javascript-array into json before parsing it:

                          var a = ["text"];
                          var json_a = JSON.stringify(a); //gives you '["text"]'
                          var a_array = JSON.parse(json_a);

                          (I haven't tested this code - might not be bug free)

                          --- In json@yahoogroups.com, "nostalgiker" <nostalgiker@...> wrote:
                          >
                          > > Neither are correct.
                          > >
                          > > The ECMAScript 5 spec states the first step of JSON.parse is
                          > > to call ToString( text ).
                          >
                          > thank you, that was the answer is was looking for. should have checked the spec...
                          >
                        • Fredag_d13
                          Wait a minute - ignore my last advice. That would probably give you escaped characters instead of what you want. Just dont feed parsers anything but plain
                          Message 12 of 13 , Jan 7, 2010
                          View Source
                          • 0 Attachment
                            Wait a minute - ignore my last advice.
                            That would probably give you escaped characters instead of what you want. Just dont feed parsers anything but plain text.

                            --- In json@yahoogroups.com, "Fredag_d13" <jonas@...> wrote:
                            >
                            >
                            >
                            > I believe that the EMCAScript standard should be changed.
                            > On page 212 in http://www.ecmascript.org/docs/tc39-2009-043.pdf
                            > ECMA specifies the JSON-grammar, but does not do so by the specification given by Crockford. http://tools.ietf.org/html/rfc4627
                            > He specifies (page 2) that:
                            >
                            > "A JSON text is a serialized object or array.
                            > JSON-text = object / array"
                            >
                            > ECMA says that JSONText is JSONValue, which is too liberal as it would also mean that JSONString, JSONNumber, etc. is JSONText.
                            >
                            > Doing it the ECMA standard way would (as previously said) convert ["text"] it into 'text' or '"text"' - which isen't JSONText, but that what the ECMAstandard tells you to do.
                            >
                            > The Native parser is actually a valid JSON parser. The spec says so:
                            > "A JSON parser MUST accept all texts that conform to the JSON grammar. A JSON parser MAY accept non-JSON forms or extensions."
                            >
                            > bummer.
                            >
                            > But it would be interesting to see if JSON.stringify is outputting JSONText according to the spec given by D.Crockford or the self-invented JSON grammar by ECMA.
                            >
                            > Anyway - It's always sane to feed a parser text, because a parser works on text (http://en.wikipedia.org/wiki/Parsing) - that's why json2.js should not be changed and ECMAScript 5 should be modified.
                            >
                            > What you should do, is to encode your javascript-array into json before parsing it:
                            >
                            > var a = ["text"];
                            > var json_a = JSON.stringify(a); //gives you '["text"]'
                            > var a_array = JSON.parse(json_a);
                            >
                            > (I haven't tested this code - might not be bug free)
                            >
                            > --- In json@yahoogroups.com, "nostalgiker" <nostalgiker@> wrote:
                            > >
                            > > > Neither are correct.
                            > > >
                            > > > The ECMAScript 5 spec states the first step of JSON.parse is
                            > > > to call ToString( text ).
                            > >
                            > > thank you, that was the answer is was looking for. should have checked the spec...
                            > >
                            >
                          • Fredag_d13
                            Wait a minute - ignore my last advice. That would probably give you escaped characters instead of what you want. Just dont feed parsers anything but plain
                            Message 13 of 13 , Jan 7, 2010
                            View Source
                            • 0 Attachment
                              Wait a minute - ignore my last advice.
                              That would probably give you escaped characters instead of what you want. Just dont feed parsers anything but plain text.

                              --- In json@yahoogroups.com, "Fredag_d13" <jonas@...> wrote:
                              >
                              >
                              >
                              > I believe that the EMCAScript standard should be changed.
                              > On page 212 in http://www.ecmascript.org/docs/tc39-2009-043.pdf
                              > ECMA specifies the JSON-grammar, but does not do so by the specification given by Crockford. http://tools.ietf.org/html/rfc4627
                              > He specifies (page 2) that:
                              >
                              > "A JSON text is a serialized object or array.
                              > JSON-text = object / array"
                              >
                              > ECMA says that JSONText is JSONValue, which is too liberal as it would also mean that JSONString, JSONNumber, etc. is JSONText.
                              >
                              > Doing it the ECMA standard way would (as previously said) convert ["text"] it into 'text' or '"text"' - which isen't JSONText, but that what the ECMAstandard tells you to do.
                              >
                              > The Native parser is actually a valid JSON parser. The spec says so:
                              > "A JSON parser MUST accept all texts that conform to the JSON grammar. A JSON parser MAY accept non-JSON forms or extensions."
                              >
                              > bummer.
                              >
                              > But it would be interesting to see if JSON.stringify is outputting JSONText according to the spec given by D.Crockford or the self-invented JSON grammar by ECMA.
                              >
                              > Anyway - It's always sane to feed a parser text, because a parser works on text (http://en.wikipedia.org/wiki/Parsing) - that's why json2.js should not be changed and ECMAScript 5 should be modified.
                              >
                              > What you should do, is to encode your javascript-array into json before parsing it:
                              >
                              > var a = ["text"];
                              > var json_a = JSON.stringify(a); //gives you '["text"]'
                              > var a_array = JSON.parse(json_a);
                              >
                              > (I haven't tested this code - might not be bug free)
                              >
                              > --- In json@yahoogroups.com, "nostalgiker" <nostalgiker@> wrote:
                              > >
                              > > > Neither are correct.
                              > > >
                              > > > The ECMAScript 5 spec states the first step of JSON.parse is
                              > > > to call ToString( text ).
                              > >
                              > > thank you, that was the answer is was looking for. should have checked the spec...
                              > >
                              >
                            Your message has been successfully submitted and would be delivered to recipients shortly.