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

Unrecognized escape sequences -- what is proper parser behavior?

Expand Messages
  • mister_curry_personal
    JSON Newbie question: Given the following string: { parent : { child : Hello ! } } And asking a JSON parser to parse it, what is the expected behavior?
    Message 1 of 2 , Mar 20 10:36 AM
    • 0 Attachment
      JSON Newbie question:

      Given the following string:
      { "parent": { "child": "Hello\!" } }
      And asking a JSON parser to parse it, what is the expected behavior?
      Please note the \! in the "Hello\!" string value.

      What should a JSON parser do? Should it bail and return an error (or
      throw an exception)? Or should it pass the unrecognized escape along as
      part of the child: element's string value? (In other words: should the
      value of the child: element be passed along as "Hello\!" ?)

      json_c <http://oss.metaparadigm.com/json-c/> returns an error, since
      \! is not a recognized escape sequence (at least as far as I can see
      from my read of the JSON.org spec.)

      If I send the string with the \! escaped thusly:
      { "parent": { "child": "Hello\\!" } }
      json_c parses the string just fine.

      As to why I would want to send such a string - let's just say that ! is
      a special character that must be escaped using \ - for example, a unix
      shell script will digest this string, so we need to send \! along to the
      recipient after parsing. So we need to know how to prepare such a
      string before we hand it to a JSON parser.

      I'm trying to resolve this issue for a project in which we are
      considering using JSON and would like to know what is proper and
      acceptable JSON syntax and how parsers are expected to behave in such a
      scenario, so we can determine what sort of pre- and post-processing
      needs to take place.

      -Mike




      [Non-text portions of this message have been removed]
    • Douglas Crockford
      ... The ! is not strictly JSON, so a parser is free to do what it wants with it. Some reasonable options would be treat it as ! treat it as ! treat it as a
      Message 2 of 2 , Mar 20 2:43 PM
      • 0 Attachment
        > Given the following string:
        > { "parent": { "child": "Hello\!" } }
        > And asking a JSON parser to parse it, what is the expected behavior?

        The \! is not strictly JSON, so a parser is free to do what it wants
        with it. Some reasonable options would be

        treat it as !
        treat it as \\!
        treat it as a syntax error
      Your message has been successfully submitted and would be delivered to recipients shortly.