[Pharo-users] NeoJSON extension - easier list handling with NeoJSONPropertyMapping

Ben Coman btc at openinworld.com
Sat Jun 16 14:27:04 EDT 2018

hi Sven,

With NeoJSON I have found several times in handling lists that
I need to indirect through  #for:customDo:  like this......

for: ExercismProblems
do: [ :mapping |
self halt.
(mapper mapInstVar: #problems) valueSchema: #ArrayOfProblems ].
for: #ArrayOfProblems
customDo: [  :mapping |
mapper listOfElementSchema: ExercismProblem ].

when I'd really like to be simpler and more concise like this...

for: ExercismProblems
do: [ :mapping |
(mapping mapInstVar: #problems) listOfType: Array withElementSchema:
ExercismProblem ].

so I'm seeking your feedback on the following hack which facilitates that.
(and if I missed something that already makes it simpler)

   Object subclass: #NeoJSONPropertyMapping
instanceVariableNames: 'propertyName valueSchema getter setter
collectionClass' "Added collectionClass"

NeoJSONPropertyMapping >> listOfType: aCollectionClass withElementSchema:
collectionClass := aCollectionClass.
valueSchema := elementSchema

   NeoJSONPropertyMapping >> readObject: anObject from: jsonReader
| value |
value := collectionClass
ifNil: [jsonReader nextAs: valueSchema]  "nil by default retains the
original behaviour"
ifNotNil: [jsonReader nextList: collectionClass of: valueSchema ].
setter value: anObject value: value

   NeoJSONReader >> nextList: nextListClass of: schema   "copied from
"Secondary interface to parse JSON.
Return a list of objects, each element according to schema."
^ nextListClass streamContents: [ :stream |
self parseListDo: [
stream nextPut: (self nextAs: schema) ] ]

Sample data is shown at:
with the following code to access it.

Object subclass: #ExercismAPI
instanceVariableNames: 'client rawResponse jsonResponse success message
classVariableNames: 'ApiKey'
package: 'Exercism'

ExercismAPI  class >> configureApiKey: key
ApiKey := key.

ExercismAPI >> track: trackIdString
self path: 'v2/exercises/' , trackIdString.

ExercismAPI >> fetchTrack: trackIdString
|znclient response|
ApiKey ifNil: [ self error: 'ApiKey not configured. See class-side
ExcercismAPI' ].
znclient := ZnClient new
enforceHttpSuccess: true;
accept: ZnMimeType applicationJson;
host: 'x.exercism.io' .
znclient path: 'v2/exercises/' , trackIdString.
znclient queryAt: 'key' put: ApiKey.
^ [ response := znclient get ]
on: ZnHttpUnsuccessful
do: [ :exception | Transcript crShow: exception ]

fetchJsonTrack: trackIdString
| reader |
reader := NeoJSONReader on: (self fetchTrack: trackIdString) readStream.
for: ExercismProblems
do: [ :mapper |
(mapper mapInstVar: #problems) listOfType: Array andElementSchema:
ExercismProblem ].
^ reader nextAs: ExercismProblems

In Playground...

ExercismAPI configureApiKey: 'ec60c50cb50d4ffaa97903d5ebb3d5ff'.

(ExercismAPI new fetchJsonTrack: 'go') inspect

I'll reset the apikey shortly.  You can get a new one from

cheers -ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-users_lists.pharo.org/attachments/20180617/e6e21e9e/attachment.html>

More information about the Pharo-users mailing list