VASmalltalk – JSON wrapper 2.6

Developing with data format JSON is pretty simple and also pretty difficult. In general most libraries (also in other languages) offer as a result of parsing and building an object with is pretty identical with instances of Dictionary.

Doing heavy developing with dictionaries are not very nice and it can produce lots of errors.

With version 2.6 (available at vastgoodies.com) it is now possible to serialize and deserialize object structures and json documents.

To give the library the information how to serialize an object structure into a JSON string the developer adds a method at the class side.

As an example you may have a look at such a method:

mskStorageJsonAttributes
	^super mskStorageJsonAttributes
		mskAddJSONStorageAtribute: 
                  ((MSKJSONStorageDescription newWithKey: 'string') 
                          attributeValueGetter: #aString ; 
                          attributeValueSetter: #aString:  );
		mskAddJSONStorageAtribute: 
                  ((MSKJSONStorageDescription newWithKey: 'double') 
                          attributeValueGetter: #aDouble ; 
                          attributeValueSetter: #aDouble:  );
		mskAddJSONStorageAtribute: 
                  ((MSKJSONStorageDescription newWithKey: 'integer') 
                          attributeValueGetter: #anInteger ; 
                          attributeValueSetter: #anInteger:  );
		mskAddJSONStorageAtribute: 
                  ((MSKJSONStorageDescription newWithKey: 'undefined') 
                           attributeValueGetter: #undefined ; 
                           attributeValueSetter: #undefined:  );
		mskAddJSONStorageAtribute: 
                  ((MSKJSONStorageDescription newWithKey: 'boolean') 
                           attributeValueGetter: #aBoolean ; 
                           attributeValueSetter: #aBoolean:  );
		mskAddJSONStorageAtribute: 
                  ((MSKJSONStorageDescription newArrayWithKey: 'intarray') 
                            resultClass: Integer ; 
                            attributeValueGetter: #integerArray ; 
                            attributeValueSetter: #integerArray:  );	
		mskAddJSONStorageAtribute: 
                   ((MSKJSONStorageDescription newArrayWithKey: 'testobjects') 
                            resultClass: self ; 
                            attributeValueGetter: #objectArray ; 
                            attributeValueSetter: #objectArray:  );	
		mskAddJSONStorageAtribute: 
                   ((MSKJSONStorageDescription newWithKey: 'object') 
                            resultClass: self ; 
                            attributeValueGetter: #singleObject ; 
                            attributeValueSetter: #singleObject:  );
		yourself

And to serialize an object into JSON we use the following code:

| aStream anObject |
aStream := WriteStream on: String new.
anObject := self new.
anObject
  aString: 'Hallo Marten !' ;
  aDouble: 3.1415213 ;
  anInteger: 42 ;
  aBoolean: true ;
  integerArray: (OrderedCollection new add: 1 ; add: 2 ;yourself);
  singleObject: (MSKJsonTestObject new);
  objectArray: (OrderedCollection new 
                            add: MSKJsonTestObject new ; 
                            add: MSKJsonTestObject new ;
                            yourself).
anObject mskStoreAsJSONOn: aStream.

now you have the JSON string

{
  "string" : "Hallo Marten !" , 
  "double" : 3.1415213 , 
  "integer" : 42 , 
  "undefined" : null , 
  "boolean" : true , 
  "intarray" : [ 1,2 ] , 
  "testobjects" : [ 
      {"string" : null , "double" : null , "integer" : null , "undefined" : null , "boolean" : null , "intarray" : null , "testobjects" : null , "object" : null},
      {"string" : null , "double" : null , "integer" : null , "undefined" : null , "boolean" : null , "intarray" : null , "testobjects" : null , "object" : null} ] , 
  "object" : {"string" : null , "double" : null , "integer" : null , "undefined" : null , "boolean" : null , "intarray" : null , "testobjects" : null , "object" : null}}

and to go back to the object structure we need code like:

| jsonString anInstance |
"returns an object structure as a json string"
jsonString := MSKJsonTestObject testOne.
"builds an object structure from the json string"
anInstance := MSKJSONParser 
                       newDefaultBuildParserParsingOnce: jsonString 
                       buildClass: MSKJsonTestObject.

and we have to a new instance.

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