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

Ben Coman btc at openinworld.com
Sun Jun 17 04:56:46 EDT 2018


On 17 June 2018 at 02:47, Sven Van Caekenberghe <sven at stfx.eu> wrote:

> Hmm, Ben,
>
> Did you see NeoJSONCustomMapping's mapping method category ?


> There you can already find #listOfElementSchema:   #listOfType:

#listOfType:andElementSchema: as well as #mapWithValueSchema:
>

Yes. Indeed thats where I copied my selector naming from, although I
switched "with" for ''and"
NeoJSONPropertyMapping >> listOfType:withElementSchema:

However NeoJSONCustomMappings seems only accessible through "reader for:
... customDo: "
while instance variable interaction seems only possible using "reader for:
... do:"
So it seems NeoJSONCustomMapping lacks #mapAccessor* methods
while NeoJSONObjectMapping lacks #listOfType:* methods
and "never the twain shall meet".

I didn't look closely at #mapWithValueSchema:
but its covering tests seem to operate on Dictionaries rather than Arrays.



> I am guessing they do what you want, no ? Or am I missing something as
> well ?
>

Yes, but how to mix those list* methods with poking the result into an
instance variable
to be able to "assign a list of elements to an instance variable" as a one
liner?
I'd be glad to be shown another way to do this for the given data.

I see a few #testLists examples, but none that assign the result to an
instance variable.
NeoJSONExamplesTests has some "lists of objects" tests, but no "objects
with lists"
A useful example would be #testPolygonPoints...

NeoJSONExamplesTests >> testPolygonPoints
| polygon polygonJson result |
polygon := Polygon new vertices: { 1 at 2. 3 at 4. 5 at 6 }.
polygonJson :=
'{ "vertices": [    "I'm not clear on how to do the writer side of things"
{
"x" : 1,
"y" : 2
},
{
"x" : 3,
"y" : 4
},
{
"x" : 5,
"y" : 6
}]
}'.
result := (NeoJSONReader on: polygonJson readStream)
mapInstVarsFor: Point;
for: Polygon do: [ :mapping |
(mapping mapInstVar: #vertices) listOfType: Array withElementSchema: Point];
nextAs: Polygon.
self assert: result equals: polygon


Some equality support is needed for Polygon (PathShape)
(my best guess, discussion of this might be worth a separate thread)

PathShape >> = aPathShape
|otherVertices equal|
otherVertices := aPathShape vertices.
(self species = aPathShape species) & (vertices size = otherVertices size)
ifFalse: [ ^false ].
1 to: vertices size do: [ :i |
(vertices at: i) = (otherVertices at: i) ifFalse: [^false] ].
^true

PathShape >> hash
|hash|
hash := 0.
1 to: vertices size do: [ :i | hash bitXor: (vertices at: i) ].
^hash

cheers -ben


>
> Sven
>
> > On 16 Jun 2018, at 20:27, Ben Coman <btc at openinworld.com> wrote:
> >
> > hi Sven,
> >
> > With NeoJSON I have found several times in handling lists that
> > I need to indirect through  #for:customDo:  like this......
> >
> >       reader
> >               for: ExercismProblems
> >               do: [ :mapping |
> >                       self halt.
> >                       (mapper mapInstVar: #problems) valueSchema:
> #ArrayOfProblems ].
> >       reader
> >               for: #ArrayOfProblems
> >               customDo: [  :mapping |
> >                       mapper listOfElementSchema: ExercismProblem ].
> >
> >
> > when I'd really like to be simpler and more concise like this...
> >
> >       reader
> >               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)
> >
>

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


More information about the Pharo-users mailing list