Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
CouchDB
Last updated at 12:36 am UTC on 13 May 2010
Get code here:

http://www.squeaksource.com/SCouchDB.html

http://www.squeaksource.com/JSON.html

JSON-hjh.32

Author: Hannes Hirzel

Ancestors: JSON-rh.31

In the project SCouchDB a copy of JSON is maintained by Igor Stasenko
and Radoslav Hodnicak.

This merges part of the changes back, in particular

SCouchDB project

JSON-Igor.Stasenko.28
JSON-Igor.Stasenko.29
JSON-rh.30
JSON-rh.31

Main changes

1. JsonObject is now a subclass of Dictionary instead of Object. So
there is no need to implement the Dictionary interface.
2. Fix for converting Unicode characters to \uNNNN format (missing
padding to 4 characters)

No further changes

The SCouchDB project contains more changes in the copy of the JSON package.

I did not go further in merging because in SCouchDB / JSON-rh.32
Radoslav Hodnicak introduces an instance variable 'converter'

which is initialized to

converter := UTF8TextConverter new

Igor Stasenko, Levente Uzonyi and Hannes Hirzel agreed that the UTF8
conversion does not belong into the JSON package

http://lists.squeakfoundation.org/pipermail/squeak-dev/2010-May/150497.html

Levente Uzonyi:

You only need to convert the characters to UTF-8, because you're
sending them over the network to a server, and Unicode characters have
to be converted to bytes someway. So the JSON printer shouldn't do any
conversion by default except for escaping. The only problem is that
escaping is not done as the spec requires it, but that's easy to fix.

http://www.json.org/

A string is a collection of zero or more Unicode characters, wrapped
in double quotes, using backslash escapes. A character is represented
as a single character string. A string is very much like a C or Java
string.
About escaping Unicode characters

Actually escaping Unicode characters to

\uNNNN

is not necessary for characters with codes >127 in case of an upload
to a CouchDB. But this version does it.

In case you want to patch this change method

Json class escapeForCharacter: c


So the updated test case for working with WebClient, JSON and the
couchDB is the following.

| json couchDBurl |
json := JsonObject new.
json title: 'The title of my note card'.
json body: 'The body test text of my note card with some Unicode
test characters ',
(8450 asCharacter asString, '.', Character cr).

"Note: JsonObject behaves like a JavaScript object insofar that you
can add properties to instances without the necessity that they have
been declared as instance variables. But you might just as well use
JsonObject like a Dictionary instead as it is a subclass of
Dictionary."

"create couchDB instance"
couchDBurl := 'http://localhost:5984/notes'.

WebClient httpPut: couchDBurl
content: ''
type: 'text/plain'.

"Store first document"
WebClient httpPut: couchDBurl, '/myNote1'
content: json asJsonString
type: 'text/plain'.

"You get the document back with"

WebClient httpGet: couchDBurl, '/myNote1' .

So far so good. This solution however still escapes code points > 127.