Re: JSON / XML support in E

Thomas Leonard <tal-v5nx5w6akNyLE8xUarVfuPLx9OUvmyODWmv/[email protected]> Thu, 08 Sep 2011 13:21:07 +0100
Newsgroups gmane.comp.lang.e.general
Message-ID <[email protected]>
On 2010-02-22 15:42, Thomas Leonard wrote:
> On Sat, 2010-01-16 at 17:07 -0800, Mark Miller wrote:
>> On Fri, Jan 15, 2010 at 4:47 AM, Kevin Reid<[email protected]>  wrote:
>>          On Jan 15, 2010, at 6:12, Thomas Leonard wrote:
>>          >  OK, I can create JSON documents easily enough using
>>          >
>>          >  def Term :=<type:org.quasiliteral.term.Term>
>>          >  def serialised := (testData :Term).asText()
>>          >
>>          >  This is nice and fast, and I assume it could produce XML in
>>          a
>>          >  similar way.
>>
>>
>>          Huh, I didn't know that worked except for leaf types. (I find
>>          that the
>>          relevant code is in
>>          org.erights.e.meta.org.quasiliteral.astro.AstroGuardSugar.)
>
>>
>>
>> Yes. The first test case at src/jsrc/org/quasiliteral/term/Term.updoc
>> is
>>
>>      ? [3=>4, "a"=>'x', [2,3]=>[4,5]]:Term
>>      # value: term`{3: 4,
>>      #              "a": 'x',
>>      #              [2, 3]:
>>      #                  [4, 5]}`
>>
>> which exercises some of the interesting cases.
>
> This fails though:
>
> ? def Term :=<type:org.quasiliteral.term.Term>
> ? def jsonSurgeon :=<elib:serial.deJSONKit>.makeSurgeon()
>
> ? def data := [["hello\nworld"]]
> ? def text := (data:Term).asText()
> ? def data2 := jsonSurgeon.unserialize(text)
>
> ? data == data2
> # value: true
>
> The pretty printing inserts a space into the string after the \n:
>
> ? println(text)
> [["hello
>   world"]]

On further investigation, it seems that newlines aren't allowed in JSON 
strings anyway:

http://stackoverflow.com/questions/42068/how-do-i-handle-newlines-in-json

This patch turns newlines into "\n" sequences and doesn't quote "'" 
(which JSON also doesn't allow):

http://gitorious.org/~tal-itinnov/repo-roscidus/it-innovation/commit/1b0a82891d305059c3732bc33b6702b41e944acf

I spotted these problems when trying to parse E's JSON output using 
Python. The changes also fix <elib:serial.deJSONKit>.


-- 
Dr Thomas Leonard
IT Innovation Centre
Gamma House, Enterprise Road,
Southampton SO16 7NS, UK


tel: +44 23 8059 8866

mailto:tal-v5nx5w6akNyLE8xUarVfuPLx9OUvmyODWmv/[email protected]
http://www.it-innovation.soton.ac.uk/