Json case: Mapping objects to dictionaries
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
I've used the SCouchDB interface - which include mapping object to dictionaries (well, JSONObjects) and mapping back from them. http://www.squeaksource.com/SCouchDB.html Of course, it is tied to CouchDB, but shouldn't be to hard to pry the mapping part out (once you find it). On Thu, Dec 5, 2013 at 7:44 AM, Norbert Hartl <norbert@hartl.name> 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
Am 05.12.2013 um 17:03 schrieb Chris Cunningham <cunningham.cb@gmail.com>:
I've used the SCouchDB interface - which include mapping object to dictionaries (well, JSONObjects) and mapping back from them. http://www.squeaksource.com/SCouchDB.html Of course, it is tied to CouchDB, but shouldn't be to hard to pry the mapping part out (once you find it).
Thanks. Iâll take a look at the code. norbert
On Thu, Dec 5, 2013 at 7:44 AM, Norbert Hartl <norbert@hartl.name> 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
Norbert, I'm currently building the dictionaries myself. I have a specific domain class of mapper for each "message" (JSON). The mapper builder manually builds my domain objects from the dictionary returned by "JSJsonParser parse: aString", when writing creates a Dictionary and sends #asJson to it. E.g. I have a EventBuilder which can build an Event from a JSON string or viceversa. I used NeoJSON in the beggining and it's blazing fast (because it is stream based), but I couldn't get my head around some particular cases (which I don't remember now, sorry). I WILL give another try, but with time to fully understand it. In the Java side of my app, this is one of the simplest things to deal with, manually to read and write, by using JSONObject and JSONArray, there's not more of it than this. Regards, Esteban A. Maringolo 2013/12/5 Norbert Hartl <norbert@hartl.name>:
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
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 Paul 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-tp4727773p47... Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.
Paul, Am 05.12.2013 um 19:10 schrieb Paul DeBruicker <pdebruic@gmail.com>:
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.
My question does not rely on exact wording. As JsonObject is derived from Dictionary you can exchange the words Dictionary/JsonObject in my question. Iâm really on to finding a good way going from/to JsonObject/Dictionary. I can do it myself like you do. But if it comes to tasks I know I need more often I like to have something more generalized. Simple approaches have mostly just support for simple conversions but do not support structure changing conversion. Norbert
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
Paul
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-tp4727773p47... Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.
participants (4)
-
Chris Cunningham -
Esteban A. Maringolo -
Norbert Hartl -
Paul DeBruicker