Re: How to escape single quotes in generated JSON?

Sven Van Caekenberghe <[email protected]>
Newsgroups gmane.comp.lang.smalltalk.squeak.seaside
Message-ID <[email protected]>
Hi Mariano,

I am not sure what your specific problem is, but strings in JSON are delimited with double quotes and only double quotes should be escaped, not single quotes.

https://tools.ietf.org/html/rfc7158#page-8

NeoJSONWriter toString: { 'exception' -> 'a  ''escape this'' aa' } asDictionary.

 "'{""exception"":""a  ''escape this'' aa""}'"

STON uses single quoted strings (like Smalltalk), hence escapes them.

STON toString: { 'exception' -> 'a  ''escape this'' aa' } asDictionary.
  
 "'{''exception'':''a  \''escape this\'' aa''}'"

Note the confusing escaping done by Smalltalk, better inspect the results !!

This won't solve your problem though, I know.

Sven

> On 23 Feb 2016, at 17:37, Mariano Martinez Peck <[email protected]> wrote:
> 
> Hi guys, 
> 
> I am trying to serialize a dictionary form Smalltalk with either NeoJSON or with Seaside's #asJson. Imagine something like this:
> 
> | infoDict stream | 
> stream := String new writeStream.
>   infoDict := Dictionary new.
>   infoDict at: 'exception' put: 'a  ''escape this'' aa'.
> 
> Note that the string of 'exception' may contain single quotes inside. 
> 
> Then, at some point I do:
> 
> self requestContext responseGenerator response nextPutAll: (infoDict asJson).
> 
> 
> Problem is the string arrives without any escaping so....when I try to do this at client side from that string:
> 
> jQuery.parseJSON(variableContainingThatString);
> 
> I get an error as single quotes inside the double quotes do not seem allowed. 
> I tried a lot of combinations of "JSON.stringify" or things like:
> 
> variableContainingThatString.replace(/[\\"'']/g, ''\\$&'').replace(/\u0000/g, ''\\0''); 
> 
> but none work...
> 
> So..I wonder...can I simply escape this from server side at the time I serialize to JSON? Any special way of doing this or I hack on the resulting string? 
> 
> Thanks in advance, 
> 
> 
> -- 
> Mariano
> http://marianopeck.wordpress.com
> _______________________________________________
> seaside mailing list
> [email protected]
> http://lists.squeakfoundation.org/cgi-bin/mailman/listinfo/seaside
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.