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

RE: [json] JavaScript JSON parser without eval()

Expand Messages
  • Atif Aziz
    The Jayrock project has a json.js version that supports both. A safe eval as well as a pure JavaScript JSON parser available via the parse method. See
    Message 1 of 3 , Feb 7, 2008
    View Source
    • 0 Attachment
      The Jayrock project has a json.js version that supports both. A "safe eval" as well as a pure JavaScript JSON parser available via the "parse" method.
      See http://svn.berlios.de/wsvn/jayrock/trunk/www/json.js

      The parse method actually comes from one of the original json.js versions that used to be available from http://json.org before it switched to an implementation using regex-verification followed by a native eval. The parser is more accepting of JavaScript's object notation than what's strictly allowed as JSON text as per RFC 4627. For example, it supports slash-slash and /*...*/ style of comments. It is nonetheless a conforming parser. From the header comment...

      JSON.parse(text) takes a JSON text and produces a JavaScript value.
      It will throw SyntaxError if there is an error in parsing the JSON
      text. This method is fully JavaScript-based and therefore can be
      several magnitudes slower than JSON.eval. However, it is also more
      permissive than JSON.eval. For example, JSON.parse permits use of
      JavaScript-style comments in JSON text.

      JSON.eval(text) takes a JSON text and produces a JavaScript value.
      It will throw SyntaxError if there is an error in parsing the JSON
      text. JSON.eval is much faster (as it internally relies on the
      JavaScript eval method after making safety checks) and stricter in
      complaince to JSON text.

      HTH,
      Atif

      From: json@yahoogroups.com [mailto:json@yahoogroups.com] On Behalf Of Jesse Skinner
      Sent: Thursday, February 07, 2008 12:13 PM
      To: json@yahoogroups.com
      Subject: [json] JavaScript JSON parser without eval()


      Is anyone aware of a JSON parser written in JavaScript that doesn't use
      the eval() function at all, but instead just does the entire parsing
      using regular expressions and string functions?

      I'm curious because I have a situation where I want to parse JSON
      myself, but my code doesn't have access to eval() for security reasons
      (it's being run on a Facebook profile page).

      Thanks,

      Jesse



      [Non-text portions of this message have been removed]
    • Greg Patnude
      Yes. Doug Crockford s ORIGINAL json parser {circa 2005 or so} did NOT use the eval function -- it was a strict tokenizer / regex mechanism both for parsing a
      Message 2 of 3 , Feb 7, 2008
      View Source
      • 0 Attachment
        Yes.

        Doug Crockford's ORIGINAL json parser {circa 2005 or so} did NOT use
        the eval function -- it was a strict tokenizer / regex mechanism both
        for parsing a JSON string and for stringifying a JSON Object....

        I did NOT see the original on json.org -- perhaps Doug or someone
        else has an archived copy laying around --

        I have a modified copy I would be willing to share if you ask
        nicely...

        Greg



        --- In json@yahoogroups.com, Jesse Skinner <jesse@...> wrote:
        >
        > Is anyone aware of a JSON parser written in JavaScript that doesn't
        use
        > the eval() function at all, but instead just does the entire
        parsing
        > using regular expressions and string functions?
        >
        > I'm curious because I have a situation where I want to parse JSON
        > myself, but my code doesn't have access to eval() for security
        reasons
        > (it's being run on a Facebook profile page).
        >
        > Thanks,
        >
        > Jesse
        >
      Your message has been successfully submitted and would be delivered to recipients shortly.