NeoJSON Mapping name-changing field
The Flickr API returns a JSON object with two fields: 'status' and otherField, where the name of otherField depends on the API method. How do I handle mapping this second field with NeoJSONReader? Thanks ----- Cheers, Sean -- View this message in context: http://forum.world.st/NeoJSON-Mapping-name-changing-field-tp4775545.html Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.
Sean P. DeNigris wrote
How do I handle mapping this second field with NeoJSONReader? Thanks
I ended up with FlickApiResponse class>>neoJsonMapping: mapper mapper for: self customDo: [ :mapping | mapping decoder: [ :dict | self new status: (dict at: 'stat'); data: (dict at: dict keys first); yourself ] ] but maybe there's a more elegant way? ----- Cheers, Sean -- View this message in context: http://forum.world.st/NeoJSON-Mapping-name-changing-field-tp4775545p4775547.... Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.
Sean P. DeNigris wrote
I ended up with FlickApiResponse class>>neoJsonMapping: mapper...
It turns out the Flickr API returns slightly weird JSON responses - the status is on the same level as possible multiple data attributes, so I ended up with (even uglier): neoJsonMapping: mapper mapper for: self customDo: [ :mapping | mapping decoder: [ :dict | | dataDict | dataDict := Dictionary new. (dict keys reject: [ :k | k = 'stat']) do: [ :k | dataDict at: k put: (dict at: k) ]. self new status: (dict at: 'stat'); data: dataDict; yourself ] ] ----- Cheers, Sean -- View this message in context: http://forum.world.st/NeoJSON-Mapping-name-changing-field-tp4775545p4775552.... Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.
Sean, On 01 Sep 2014, at 03:29, Sean P. DeNigris <sean@clipperadams.com> wrote:
The Flickr API returns a JSON object with two fields: 'status' and otherField, where the name of otherField depends on the API method. How do I handle mapping this second field with NeoJSONReader? Thanks
If you want all responses to have the same class, then this does indeed mean that you have to handle a dynamic response, which is hard(er). But you say that the structure of the response depends on the API call, that means that you could create a (sub)class per response, no ? I have to admit that I most often just parse into general maps/lists (Dictionary/Array) and deal with those. Creating specific classes just to hold data is so Java style ;-) but it does add more implicit documentation. Sven
----- Cheers, Sean -- View this message in context: http://forum.world.st/NeoJSON-Mapping-name-changing-field-tp4775545.html Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.
participants (2)
-
Sean P. DeNigris -
Sven Van Caekenberghe