[squeak-dev] WebClient, Json and CouchDB
Hannes Hirzel
hannes.hirzel at gmail.com
Wed May 12 14:56:38 UTC 2010
Hello
This is a note how I upload Json documents to a couchDB using
Webclient http://www.squeaksource.com/WebClient
and a modified version of http://www.squeaksource.com/JSON JSON-jrd.28
The test case is
|d r|
d := Dictionary new. d at: 'title' put: 'The title of this card'.
d at: 'body' put: (8820 asCharacter asString, 'aäbc', Character cr).
r := WriteStream on: String new.
(JsonObject newFrom: d) jsonWriteOn: r.
"r contents"
WebClient httpPut: 'http://192.168.0.121:5984/test/myDoc6' content:
r contents type: ''.
WebClient gives back as contents
'{"ok":true,"id":"myDoc6","rev":"1-cef58e13534fc0fcf7f38262bc086d12"}
'
To make this work I had to patch the method
Json escapeForCharacter: aCharacter
This was necessary because for example
Json escapeForCharacter: 228 asCharacter
gave back
'\uE4'
instead of
'\u00E4'
I changed the method Json escapeForCharacter to
escapeForCharacter: c
| index nnnn |
^ (index := c asciiValue + 1) <= escapeArray size
ifTrue: [ ^ escapeArray at: index ]
ifFalse: [nnnn := ((c asciiValue bitAnd: 16rFFFF) printStringBase: 16) .
[nnnn size < 4] whileTrue: [nnnn := '0', nnnn].
^ '\u', nnnn]
My question:
Is there a nicer way of doing
nnnn := ((c asciiValue bitAnd: 16rFFFF) printStringBase: 16) .
[nnnn size < 4] whileTrue: [nnnn := '0', nnnn].
^ '\u', nnnn
Basically I have to patch zeros in front of a ByteString which is too short.
Regards
Hannes
P.S. This note does not address the issue that it would be nice to NOT
escape characters which have a code >127 at all but rather keep them
as Unicode characters as the Json spec allows for this.
http://www.json.org/
Any Unicode character except " and \ is allowed.
This is about fixing an error which comes up when posting Json objects
to CouchDB.
More information about the Squeak-dev
mailing list
|