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

json2.js

Expand Messages
  • Douglas Crockford
    The original version of json.js had a JSON object containing stringify and parse methods. When the opportunity to add JSON support to the next proposed
    Message 1 of 15 , Oct 29, 2007
    • 0 Attachment
      The original version of json.js had a JSON object containing stringify
      and parse methods. When the opportunity to add JSON support to the
      next proposed standard for ECMAScript, the methods changed to toString
      and parseJSON.

      In retrospect, I think the original form was better. I have posted
      json2.js which has JSON.stringify (which looks for the presence of a
      toJSON method in the objects it stringifies), and JSON.parse (which
      can still accept a filter function).

      http://www.JSON.org/json2.js
    • Kris Zyp
      I for one totally agree with the decision to revert to the original API. While I know that defensive enumeration is good practice, I really appreciate keeping
      Message 2 of 15 , Oct 30, 2007
      • 0 Attachment
        I for one totally agree with the decision to revert to the original API.
        While I know that defensive enumeration is good practice, I really
        appreciate keeping an unpolluted Object.prototype and Array.prototype. Thank
        you Douglas, I think that was a good decision!
        Kris


        [Non-text portions of this message have been removed]
      • Ric Johnson
        Thank you for implmenting this. this make the code so much cleaner. Now if we can just get the Prototype library to stop overloading Object.... ... stringify
        Message 3 of 15 , Oct 30, 2007
        • 0 Attachment
          Thank you for implmenting this. this make the code so much cleaner.

          Now if we can just get the Prototype library to stop overloading
          Object....

          --- In json@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
          >
          > The original version of json.js had a JSON object containing
          stringify
          > and parse methods. When the opportunity to add JSON support to the
          > next proposed standard for ECMAScript, the methods changed to
          toString
          > and parseJSON.
          >
          > In retrospect, I think the original form was better. I have posted
          > json2.js which has JSON.stringify (which looks for the presence of
          a
          > toJSON method in the objects it stringifies), and JSON.parse (which
          > can still accept a filter function).
          >
          > http://www.JSON.org/json2.js
          >
        • Gustavo Munoz
          Great job, Douglas. There is a typo in the initial multiline comment at line 57, it says JON.parse ... -- Blog: http://justavo.blogsome.com/ [Non-text portions
          Message 4 of 15 , Oct 31, 2007
          • 0 Attachment
            Great job, Douglas.

            There is a typo in the initial multiline comment at line 57, it says
            JON.parse

            On 10/30/07, Ric Johnson <RicJohnsonIII@...> wrote:
            >
            > Thank you for implmenting this. this make the code so much cleaner.
            >
            > Now if we can just get the Prototype library to stop overloading
            > Object....
            >
            > --- In json@yahoogroups.com <json%40yahoogroups.com>, "Douglas Crockford"
            > <douglas@...> wrote:
            > >
            > > The original version of json.js had a JSON object containing
            > stringify
            > > and parse methods. When the opportunity to add JSON support to the
            > > next proposed standard for ECMAScript, the methods changed to
            > toString
            > > and parseJSON.
            > >
            > > In retrospect, I think the original form was better. I have posted
            > > json2.js which has JSON.stringify (which looks for the presence of
            > a
            > > toJSON method in the objects it stringifies), and JSON.parse (which
            > > can still accept a filter function).
            > >
            > > http://www.JSON.org/json2.js
            > >
            >
            >
            >



            --
            Blog: http://justavo.blogsome.com/


            [Non-text portions of this message have been removed]
          • Atif Aziz
            Are there any plans to publish a set of unit tests in JavaScript that exercise json.js/json2.js? From: json@yahoogroups.com [mailto:json@yahoogroups.com] On
            Message 5 of 15 , Dec 24, 2007
            • 0 Attachment
              Are there any plans to publish a set of unit tests in JavaScript that exercise json.js/json2.js?

              From: json@yahoogroups.com [mailto:json@yahoogroups.com] On Behalf Of Douglas Crockford
              Sent: Monday, October 29, 2007 5:53 PM
              To: json@yahoogroups.com
              Subject: [json] json2.js


              The original version of json.js had a JSON object containing stringify
              and parse methods. When the opportunity to add JSON support to the
              next proposed standard for ECMAScript, the methods changed to toString
              and parseJSON.

              In retrospect, I think the original form was better. I have posted
              json2.js which has JSON.stringify (which looks for the presence of a
              toJSON method in the objects it stringifies), and JSON.parse (which
              can still accept a filter function).

              http://www.JSON.org/json2.js



              [Non-text portions of this message have been removed]
            • Douglas Crockford
              I have updated json2.js, adding features suggested by Mark Miller and Chris Zyp. JSON.parse can take a reviver function that is passed each key and value, with
              Message 6 of 15 , Mar 22, 2008
              • 0 Attachment
                I have updated json2.js, adding features suggested by Mark Miller and
                Chris Zyp.

                JSON.parse can take a reviver function that is passed each key and
                value, with this bound to the object containing the key.

                JSON.stringify can take a replacer function which similarly is passed
                each key and value, with this bound to the object containing the key.
                The replacer, like the toJSON method, gives more control over the
                serialization.

                http://www.JSON.org/json2.js
              • Douglas Crockford
                I have updated json2.js to include a prettyprinting option. If the third parameter is a string (such as t or ) or a number of spaces, then nested
                Message 7 of 15 , Mar 24, 2008
                • 0 Attachment
                  I have updated json2.js to include a prettyprinting option. If the
                  third parameter is a string (such as "\t" or " ") or a number of
                  spaces, then nested structures will be broken into lines with indentation.

                  See http://www.JSON.org/json2.js
                • Douglas Crockford
                  The json2 methods take optional functions that can make transformations on the JSON structures. For example, suppose you want to represent dates as strings in
                  Message 8 of 15 , Apr 27, 2008
                  • 0 Attachment
                    The json2 methods take optional functions that can make
                    transformations on the JSON structures.

                    For example, suppose you want to represent dates as strings in the form

                    "Date(09/09/2001)"

                    Pass a replacer function to the stringify method:

                    text = JSON.stringify([new Date()], function (key, value) {
                    return this[key] instanceof Date ?
                    'Date(' + this[key] + ')' : value;
                    });

                    Pass a reviver function to the parse method:

                    data = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
                    var d;
                    if (typeof value === 'string' &&
                    value.slice(0, 5) === 'Date(' &&
                    value.slice(-1) === ')') {
                    d = new Date(value.slice(5, -1));
                    if (d) {
                    return d;
                    }
                    }
                    return value;
                    });
                  • Douglas Crockford
                    I have modified one of the regular expressions in the JSON.parse method to more reliably signal a syntax error in the case of a malformed uHHHH string. No
                    Message 9 of 15 , May 25, 2008
                    • 0 Attachment
                      I have modified one of the regular expressions in the JSON.parse
                      method to more reliably signal a syntax error in the case of a
                      malformed \uHHHH string. No security exploit existed or was repaired
                      by this change.

                      http://json.org/json2.js
                    • Douglas Crockford
                      I added toJSON methods to Boolean, Number, and String. This makes simple type wrappers work correctly, so JSON.stringify(new String( string )) produce
                      Message 10 of 15 , Jul 9, 2008
                      • 0 Attachment
                        I added toJSON methods to Boolean, Number, and String. This makes
                        simple type wrappers work correctly, so

                        JSON.stringify(new String('string'))

                        produce "string". If you don't use the type wrappers (and it is best
                        that you don't) then you can delete those methods.
                      • Douglas Crockford
                        I modified json2.js slightly to coexist more easily with native JSON libraries. http://www.JSON.org/json2.js
                        Message 11 of 15 , Sep 1, 2008
                        • 0 Attachment
                          I modified json2.js slightly to coexist more easily with native JSON
                          libraries.

                          http://www.JSON.org/json2.js
                        • Lucas Smith
                          Doug, Can you elaborate on the changes? Do you maintain the source in a VCS, or is it the responsibility of consumers to store copies locally to see diffs?
                          Message 12 of 15 , Sep 1, 2008
                          • 0 Attachment
                            Doug,

                            Can you elaborate on the changes? Do you maintain the source in a
                            VCS, or is it the responsibility of consumers to store copies locally
                            to see diffs?

                            Luke

                            On Sep 1, 2008, at 3:03 PM, Douglas Crockford wrote:

                            > I modified json2.js slightly to coexist more easily with native JSON
                            > libraries.
                            >
                            > http://www.JSON.org/json2.js
                            >
                            >
                            >



                            [Non-text portions of this message have been removed]
                          • Douglas Crockford
                            ... A JSON object is created if one doesn t exist. If the object does not have a stringify method, one is added. If the object does not have a parse method,
                            Message 13 of 15 , Sep 2, 2008
                            • 0 Attachment
                              --- In json@yahoogroups.com, Lucas Smith <lsmith@...> wrote:
                              > Can you elaborate on the changes?

                              A JSON object is created if one doesn't exist.

                              If the object does not have a stringify method, one is added.

                              If the object does not have a parse method, one is added.
                            • Douglas Crockford
                              I changed the stringify method s array test from if (typeof value.length === number && !value.propertyIsEnumerable( length )) { to the more reliable Miller
                              Message 14 of 15 , Nov 19, 2008
                              • 0 Attachment
                                I changed the stringify method's array test from

                                if (typeof value.length === 'number' &&
                                !value.propertyIsEnumerable('length')) {

                                to the more reliable Miller device

                                if (Object.prototype.toString.apply(value) === '[object Array]') {

                                See http://blog.360.yahoo.com/douglascrockford?p=916
                              • Douglas Crockford
                                json2.js is now available on Github. https://github.com/douglascrockford/JSON-js
                                Message 15 of 15 , Nov 18, 2010
                                • 0 Attachment
                                  json2.js is now available on Github.

                                  https://github.com/douglascrockford/JSON-js
                                Your message has been successfully submitted and would be delivered to recipients shortly.