links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
JSON serialization
Last updated at 8:57 pm UTC on 22 October 2017

Herbert König
Sat, Oct 21, 2017 at 9:06 PM
Reply-To: The general-purpose Squeak developers list
To: squeak-dev@lists.squeakfoundation.org


I used JSON with the addition of carrying the class (JSON being Javascript born has no concept of classes) because it requires only very little work after loading the JSON package. (And because I've been doing some web application where I needed JSON anyway.)

Each class needs on the class side:

 constructFromJson: j
    ^ self new constructFromJson: j

and on the instance side:

 jsonWriteOn: aStream
    Json renderInstanceVariables:
            (self class allInstVarNames collect: [:ea| ea asSymbol])
        of: self
        on: aStream


 constructFromJson: j
    j keysAndValuesDo: [:key :value|
        self instVarNamed: key put: value]

Simple, stupid and good enough for several of my applications.

This should work across dialects if the destination image has the required classes in the same shape. The @Classname designator is a Smalltalk specific extension to JSON. I think it is not good enough to transfer object trees with cyclic dependencies. Other languages with a different ideas of how a class is constructed it might need more work.

JSON excerpt:

 [@IndividualGenom{"halfNum":77,"fitness":2.364011719201257,"fitnesses":null,"geneValues":[-0.7875619210225705,-1.1191086825115324,2.2987547782235476,-= 2.1996847933965746,3.3254889274350594,-1.7481778033261461 ......