[squeak-dev] Re: UTF8 in JSON (was: Re: [ANN] WebClient and WebServer 1.0 for Squeak)

Hannes Hirzel hannes.hirzel at gmail.com
Tue May 11 23:17:41 UTC 2010


On 5/11/10, Igor Stasenko <siguctua at gmail.com> wrote:
> On 12 May 2010 01:12, Hannes Hirzel <hannes.hirzel at gmail.com> wrote:
>> So String>> jsonWriteOn:aStream
>>
>> is now just
>>
>> jsonWriteOn: aStream
>>       aStream nextPut: $".
>>       aStream nextPutAll:  self.
>>       aStream nextPut: $".
>>
>>
> this is also wrong, because if your string contains a $" character
> (and other control characters), it must be properly escaped:
>
> '"' asJsonString  '"\""'
>
> String crlf asJsonString  '"\r\n"'
>

Yes, you are right. I just realised it as well. However there is no method
   asJsonString
in the http://www.squeaksource.com/JSON package by Tony Garnock-Jones
and others.


At least " and \ have to be escaped (cf. for example
http://awwx.ws/combinator/13)

So I went for the following

Instead of

Json class

  escapeForCharacter: c
	
	| index |
	^ (index := c asciiValue + 1) <= escapeArray size
		ifTrue: [ ^ escapeArray at: index ]
		ifFalse: [ ^ '\u', ((c asciiValue bitAnd: 16rFFFF) printStringBase: 16) ]


I do


escapeForCharacter: c
	
	| index |
	^ (index := c asciiValue + 1) <= escapeArray size
		ifTrue: [ ^ escapeArray at: index ]
		ifFalse: [ ^nil]


And I go back from

 String
   jsonWriteOn: aStream
 	aStream nextPut: $".
	aStream nextPutAll:  self.
	aStream nextPut: $".

to what it was before


 String
  jsonWriteOn: aStream

	| replacement |
	aStream nextPut: $".
	self do: [ :ch |
		(replacement := Json escapeForCharacter: ch)
			ifNil: [ aStream nextPut: ch ]
			ifNotNil: [ aStream nextPutAll: replacement ] ].
	aStream nextPut: $".


And in fact the test case has to be extended to include a backslash u
in the example string (myWideString).

myWideString := ('ä', 8220 asCharacter asString, Character cr, 'b\user').
d := Dictionary new. d at: 'title' put:   'aTitle'. d at: 'body' put:
myWideString.
r := WriteStream on: String new.
(JsonObject newFrom: d) jsonWriteOn: r.
WebClient httpPut: host, '/notes/test30' content: (UTF8TextConverter
new encodeString: r contents) type: 'text/plain'.



--Hannes



More information about the Squeak-dev mailing list