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

JSON.stringify and inherited properties

Expand Messages
  • Douglas Crockford
    The JSON.stringify function builds a JSON text using the own properties of the objects it is given. If you want the text to also include the inherited
    Message 1 of 1 , Jan 30, 2009
    View Source
    • 0 Attachment
      The JSON.stringify function builds a JSON text using the own
      properties of the objects it is given. If you want the text to also
      include the inherited properties, then you can provide a replacer
      function parameter that causes all of the members to be included. For
      example,

      JSON.stringify(n, function replacer(key, value) {
      var i, new_value;

      switch (typeof value) {
      case 'function':
      case 'undefined':
      return;
      case 'object':
      if (value &&
      Object.prototype.toString.apply(value) !==
      '[object Array]') {
      new_value = {};
      for (i in value) {
      new_value[i] = value[i];
      }
      return new_value;
      }
      return value;
      default:
      return value;
      }
      } , 4)
    Your message has been successfully submitted and would be delivered to recipients shortly.