[Pharo-dev] Json case: Mapping objects to dictionaries

Paul DeBruicker pdebruic at gmail.com
Thu Dec 5 13:10:24 EST 2013

FWIW - The ElasitcSearch stuff only maps to Dictionaries because of an
oversight on my part.  It should be fine if those objects materialized as
JsonObjects.  What are you doing to get Dictionaries?  Its been a while
since I looked at the code.  


To map domain objects I usually do something custom because I usually don't
want all inst vars being sent as JSON.  An example of what I normally do is:

#jsonWriteOn: aStream
	| y |
	y := JsonObject new.	
	self varNamesToSerializeAsJson.
		do: [ :each | 
			(self perform: each asSymbol) isNil
				ifFalse: [ y at: each put: (self perform: each asSymbol) ] ].
	y jsonWriteOn: aStream

and then when materializing:

fromJson: aJsonResult
	self varsToMaterializeFromJson
		do: [ :eachSymbol | 
			| jsonObj class obj |
			jsonObj := aJsonResult at: eachSymbol asString ifAbsent: [ nil ].
			jsonObj notNil
				ifTrue: [ 
					class := MyDomain classForJsonVariableNamed: eachSymbol.
					obj := class isNil
						ifTrue: [ jsonObj trimBoth ]
						ifFalse: [ class fromJson: jsonObj ].
					self perform: (eachSymbol , ':') asSymbol with: obj ] ]

#classForJsonVariableNamed: just looks up in a dictionary that contains inst
var names as keys and classes as values.  

That all being said I'd be interested to learn a better way to do it. 

Hope this helps


Norbert Hartl wrote
> What would be the best software to map my objects to dictionaries? There
> are a lot of packages in pharo that deal with JSON somehow. But it seems
> none does mapping from objects to dictionaries and is standalone. We’ve
> got
> - NeoJSON package . Maps objects to strings directly for performance
> reason
> - JSON package. Maps string to JsonObject (subclassed from dictionary)
> - Voyage magritte based mapper: Closest to what I find useful but buried
> inside of Voyage. And I’m not sure it would be easy to factor out this
> module as a standalone mapper.
> - Seaside javascript… : I forgot what it really does. I often shy away
> looking at it because I don’t want to suck in all the dependencies. 
> I’m asking because for me this is an important asset. If we talk about
> JSON there are three things: Objects, JSON structure (being dictionaries
> and other collections in pharo) and JSON strings. Writing to another
> server or to a file producing JSON strings seems to be enough. But using
> it for another system like MongoDB or ElasticSearch this does not work.
> Here the common interface seems to be dictionaries. With ElasticSearch
> mapping dictionaries to JSON and MongoDB mapping dictionaries to BSON. So
> a good descriptive way to map objects to dictionaries would be a good
> thing to have.
> Any thoughts?
> Norbert

View this message in context: http://forum.world.st/Json-case-Mapping-objects-to-dictionaries-tp4727773p4727802.html
Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.

More information about the Pharo-dev mailing list