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

comments on json.js

Expand Messages
  • Tom Metro
    Quoting from the latest reference implementation at: http://www.json.org/json.js ... This should document that it returns null upon finding something that
    Message 1 of 1 , Feb 22, 2006
    • 0 Attachment
      Quoting from the latest reference implementation at:
      http://www.json.org/json.js

      > /*
      > Stringify a JavaScript value, producing a JSON text.
      > */
      > stringify: function (v) {
      > var f = s[typeof v];
      > if (f) {
      > v = f(v);
      > if (typeof v == 'string') {
      > return v;
      > }
      > }
      > return null;
      > },


      This should document that it returns null upon finding something that
      can't be represented in JSON.

      Similarly, it should be noted that object properties set to functions or
      undefined will silently be dropped from the returned JSON structure.

      It may be worth having an option where undefined values are returned as
      'null' for cases where you want the existence of a property to propagate
      to the JSON structure.

      There may also be value in having a strict mode, where an exception is
      thrown instead of null being returned. Though this is probably only
      practical on platforms that implement hasOwnProperty(), which would be
      needed to sift out inherited properties.


      Why is this:

      > if (typeof v == 'string') {

      done? It would seem the methods of s all return something appropriate.
      I'd probably write that as (untested):

      stringify: function (v) {
      var f = s[typeof v];

      // unhandled or unknown object type
      if (!f) return null;

      return f(v);
      },


      And:
      > object: function (x) {
      > ...
      > if (x instanceof Array) {
      > a[0] = '[';
      > l = x.length;
      > for (i = 0; i < l; i += 1) {
      > v = x[i];
      > f = s[typeof v];
      > if (f) {
      > v = f(v);
      > if (typeof v == 'string') {
      > if (b) {
      > a[a.length] = ',';
      > }
      > a[a.length] = v;
      > b = true;
      > }
      > }
      > }

      Couldn't that, and the block that follows it, be written recursively,
      calling the higher level stringify() method? Such as (untested):

      object: function (x) {
      if (!x) return 'null';

      if (x instanceof Array) {
      var a = [];
      for (var i = 0; i < x.length; i++) {
      var v = stringify(x[i]);
      if (v) a.push(v);
      }
      return '[' + a.join(',') + ']';
      }

      And the block that follows...

      ... else if (x instanceof Object) {
      var a = [];
      for (var i in x) {
      var v = stringify(x[i]);
      if (v) a.push(s.string(i) + ': ' + v);
      }
      return '{' + a.join(',') + '}';
      } ...


      "return 'null'" or the equivalent appears in the code above and other
      places. At first it appears to be an error that null is in quotes, but
      then it becomes apparent that this is intentional because the calling
      code is expecting a string for legal values. It might clarify the code
      to include a comment, or define a constant, like JSON.null, and return that.


      Also:
      > object: function (x) {
      > ...
      > if (x instanceof Array) {
      > ...
      > } else if (x instanceof Object) {
      > ...
      > } else {
      > return;
      > }

      The return with no value produces an interpreter warning in Firefox:

      anonymous function does not always return a value
      json.js (line 104)
      return a.join('');

      anonymous function does not always return a value
      json.js (line 106)
      return 'null';

      It can be fixed by adding a null, but I think it is arguable that an
      exception should be thrown instead, as it suggests the code is failing
      to handle an object type that it doesn't know about, which given that
      everything descends from Object, should never happen.

      -Tom
    Your message has been successfully submitted and would be delivered to recipients shortly.