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

30Re: [json] backslash double-quote

Expand Messages
  • Mark Miller
    Aug 1, 2005
    • 0 Attachment
      jonschull5 wrote:
      > Hi,
      >
      > Shouldn't it be possible to jsonize a string like this?
      >
      > {"doubleQuoteSurroundedBySpaces":" \" "}}'


      Assuming your closing ")'" is an error, E's embedded term-tree language
      (<http://www.erights.org/data/terml/index.html>) accepts this just fine:

      ? term`{"doubleQuoteSurroundedBySpaces":" \" "}`
      # value: term`{"doubleQuoteSurroundedBySpaces": " \" "}`

      We altered E's term-tree language to be a superset of JSON. Shown above is a
      command-line interaction with the E interpreter. "? " is E's prompt. ("> " is
      E's continuation prompt, and is not shown here.) The text to the right of the
      "? " is an E expression entered by the user. The expression shown above is a
      quasi-literal expression, consisting of the name of the quasi-parser to the
      left of the back-tick, and the quasi-literal string to be parsed within the
      back-ticks. In this case we're just using it as a parser. To better see what's
      going on, below we invoke E's term-tree parser explicitly on your JSON string:

      ? def str := `{"doubleQuoteSurroundedBySpaces":" \" "}`
      # value: "{\"doubleQuoteSurroundedBySpaces\":\" \\\" \"}"

      ? def termParser := <import:org.quasiliteral.term.TermParser>
      # value: <makeTermParser>

      ? def tt := termParser(str)
      # value: term`{"doubleQuoteSurroundedBySpaces": " \" "}`

      We can use a quasi-literal pattern to pattern match this tree and extract its
      components. The text between the "@{" and "}" is a pattern in the E
      programming language. Whatever the term-tree quasi-literal pattern extracts at
      that position is matched against this E pattern, which in this case just
      coerces and binds variables k and v.

      Since both E and the term-tree language use ":", but for two quite different
      purposes, our convention is to put a space to the left of an E ":" but a space
      to the right of a term-tree colon.

      ? def term`{@{k :String}: @{v :String}}` := tt
      # value: term`{"doubleQuoteSurroundedBySpaces": " \" "}`

      ? k
      # value: "doubleQuoteSurroundedBySpaces"

      ? v
      # value: " \" "

      For more, see <http://www.erights.org/elang/quasi/terms/like-ellipses.html>
      and the section "Quasi-JSON back from the dead" at
      <http://www.eros-os.org/pipermail/e-lang/2005-May/010646.html>.

      --
      Text by me above is hereby placed in the public domain

      Cheers,
      --MarkM
    • Show all 13 messages in this topic