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

toJSON() and the replacer, also pretty printing

Expand Messages
  • Tyler Close
    There s a small inconsistency between the current documentation for JSON.stringify() and the actual implementation (v. 2008-07-09) . The documentation for the
    Message 1 of 2 , Jul 14, 2008
    • 0 Attachment
      There's a small inconsistency between the current documentation for
      JSON.stringify() and the actual implementation (v. 2008-07-09) . The
      documentation for the replacer argument says:

      """
      an optional parameter that determines how object
      values are stringified for objects without a toJSON
      method. It can be a function or an array.
      """

      AFAICT, the actual implementation always calls a toJSON method if it
      exists, and then passes the return value from that call to the
      replacer. The value returned from the replacer is the one serialized.
      So the replacer determines how all objects are serialized, not just
      those without a toJSON() method.

      I'd prefer an implementation that did *not* call toJSON() if a
      replacer function is specified. If the replacer wants to call toJSON()
      itself, it can, but the call isn't required. This way, the caller of
      JSON.stringify() may be able to avoid passing the flow of control to
      an object in the serialized object tree. The "may" depends on the use
      of a Javascript subset, such as ADsafe.

      I'd also prefer that no newline character be inserted between elements
      in an array. When using the Firebug "Net" pane, the current
      implementation requires scrolling for even short payloads. I like
      having the newline between object members though. For some reason, my
      arrays tend to be longer lists of literals, whereas object members are
      more likely to be objects.

      Thanks,
      --Tyler

      <http://www.json.org/js.html>
    • Douglas Crockford
      ... I corrected the comment. ... Pretty is in the eye of the beholder. There are uses that need newlines in arrays. I don t want to complicate the calling
      Message 2 of 2 , Jul 16, 2008
      • 0 Attachment
        --- In json@yahoogroups.com, "Tyler Close" <tyler.close@...> wrote:
        >
        > There's a small inconsistency between the current documentation for
        > JSON.stringify() and the actual implementation (v. 2008-07-09).

        I corrected the comment.

        > I'd also prefer that no newline character be inserted between elements
        > in an array. When using the Firebug "Net" pane, the current
        > implementation requires scrolling for even short payloads. I like
        > having the newline between object members though. For some reason, my
        > arrays tend to be longer lists of literals, whereas object members are
        > more likely to be objects.

        Pretty is in the eye of the beholder. There are uses that need
        newlines in arrays. I don't want to complicate the calling sequence or
        complicate the code. What do you suggest?
      Your message has been successfully submitted and would be delivered to recipients shortly.