This could be very useful, in the past I used ExtJS which serialized its components in JSON format with a similar "xtype" attribute that was used to lookup the proper class to instantiate. But NeoJSON being stream based, and requiring a mapping to instantiate specific classes, the only, dirty, way I think about it is to instantiate everything as NeoJSONObject and then instantiate your desired object classes from the tree/graph of NeoJSONObjects. So basically you'd have to read the whole JSON stream and *then* instantiate your objects, this is more memory intensive than a stream based approach because you'll be instantiating intermediary objects, but it could work fine. Something like: ASNote fromJSON: (NeoJSONReader on: readStream) mapClass: NeoJSONObject; upToEnd Regards, Esteban A. Maringolo El mar., 8 ene. 2019 a las 20:35, Eric Gade (<eric.gade@gmail.com>) escribió:
Hey guys,
I'm toying with the idea of a Pharo implementation of ActivityStreams ( https://www.w3.org/TR/activitystreams-core/#introduction) which is based on a subset of JSON called JSON-LD (for "linked data").
While I do not believe this would entail creating a whole JSON-LD implementation, I do have a question about how to implement reading/writing/mapping to and from the core set of object types in the specification.
I am aware that giving any class #neoJsonMapping allows one to define the specifics of the mapping to and from objects of that class (I think this is super cool, by the way). The issue with ActivityStreams objects is that the "type" of object they represent is in the JSON itself as the property "type," for example:
``` { "@context": "https://www.w3.org/ns/activitystreams", "name": "A simple note", "type": "Note", "id": "http://www.test.example/notes/1", "content": "A simple note", "replies": { "type": "Collection", "totalItems": 1, "items": [ { "name": "A response to the note", "type": "Note", "content": "A response to the note", "inReplyTo": "http://www.test.example/notes/1" } ] } } ```
This refers to a top level ActivityStream object of type "Note" which we maybe want to parse into something like `ASNote`.
My core question is this: how can we get a NeoJSONReader to determine which type of object to create by checking for a "type" property as soon as possible in the process? Because this property can show up in any order, we won't know what type of object to create until it is encountered. What is the best strategy there?
Does the complexity suggest that we should simply parse incoming ActivityStream objects into the collection-and-dict default mapping and deal with creating the appropriate objects after that? In this case we could still use the custom mapping for the writing, which is not a problem since the "type" field will be associated with the class (ie, ASNote class
#type will respond with 'Note' and we can go from there).
Any thoughts or suggestions much appreciated! -- Eric