VASmalltalk – MSKJSONWrapperApp

The base of the CouchDB interface is my wrapper around an external C-library doing the JSON parsing stuff. The JSON parser/checker can be found in the MSKJSONWrapper App application (by the way: all source code is available at vastgoodies).

As I mentioned in my earlier posting about this wrapper – the c-library is pretty ANSI C and can be compiled quite easily. It’s a very good example to show the usage of a callback to Smalltalk.

The main use of this library is to parse JSON strings and get a Dictionary-like object (with the help of the external C library) or to create from a Dictionary a JSON string representation (without external C support).

In most of the time you will use the MSKJSONParser and on the class side you may find several small helper methods:

MSKJSONParser class>>newChecking

This method returns a new instance of the parser and you may use this instance to check the valid syntax of the JSON string. Please be aware, that you must release the external sources in the C library ! How this is done can be seen in

MSKJSONParser class>>newCheckingParserParsingOnce: aString.

You may use this method to check a valid JSON string representation and the result is returned to the caller (in error case an instance of AbtError with more information what is wrong with the string). After checking the string, the external resources are released.

All the other methods are used to build Dictionary-like Smalltalk objects from JSON string representations.

MSKJSONParser class >>newParser: aJSONConfig

The is the lowest level instance creation method. The configuration values for the newly parser are stored in an instance of MSKJSONConfig. The parser can be used several times – and be aware, that you have to release all external memory. How the instance of MSKJSONCOnfig can be configured can be seen in the next method:

MSKJSONParser class>>newDefaultBuildParser

This method is more or less the parser method newChecking (introduced above). A new parser is created and configured and you may use this parser several times. Also here: be aware to release all external resources.

MSKJSONParser class>>newDefaultBuildParserParsingOnce: aString

The next helper method, which does everything. Parsing and build an instance of the class MSKJSONObject – a subclass of dictionary. If you want to let the parser create an instance of a user defined class, that you should create a subclass of MSKJSONObject and then you call:

MSKJSONParser class>>newDefaultBuildParserParsingOnce: aString buildClass: aClass

The method is heavily used in the CouchDB interface.

For the other way (create a JSON string representation from a Dictionary like instance) the following small code sequence shows, how it can be done:

aStream := WriteStream on: String new.
aDictionary storeAsJSONOn: aStream.
jsonRepresentationString := aStream contents

Well, that shpuld give you enough informations to start with this wrapper. As I mentioned, the needed library is available from my site and can easily be compiled using a normal C compiler. The Watcom C project is included in the download.

This entry was posted in Smalltalk and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s