Hi Joachim,

Have a look at this new example:

===
Name: Neo-JSON-Tests-SvenVanCaekenberghe.43
Author: SvenVanCaekenberghe
Time: 26 September 2017, 2:14:01.71055 pm
UUID: f40af758-7816-0d00-89cd-641e08d6220e
Ancestors: Neo-JSON-Tests-SvenVanCaekenberghe.42

Add #testVirtualTransferObject as another mapping example
===

NeoJSONMappingTests>>#testVirtualTransferObject
  | data customMapping extraJson json result |
  data := NeoJSONObject new data: { 1@1. 2@2 }.
  extraJson := '{"foo":1,"data":[{"x":1,"y":1},{"x":2,"y":2,"z":-1}]}'.
  "The idea here is that we are not interested in the top level object just in 
its data property, 
   which should be of a specific type, hence we create a virtual transfer 
object"
  customMapping := [ :mapper |
    mapper
      for: Point do: [ :mapping |
        mapping mapInstVars: #(x y) ];
      for: #ArrayOfPoints customDo: [ :mapping |
        mapping listOfElementSchema: Point ];
      for: #TransferObject do: [ :mapping |
        mapping subjectClass: NeoJSONObject.
        (mapping mapAccessor: #data) valueSchema: #ArrayOfPoints ];
      yourself ].
  "By using NeoJSONObject accessors (#data & #data:) are translated to generic 
#at:[put:] messages.
   A longer alternative is 
     (mapping mapProperty: #data getter: [ :obj | obj at: #data ] setter: [ 
:obj :x | obj at: #data put: x]) valueSchema: #ArrayOfPoints
   where the blocks give you the flexibility to use a plain Dictionary for 
example"
  result := (customMapping value: (NeoJSONReader on: extraJson readStream)) 
nextAs: #TransferObject.
  self assert: result equals: data.
  json := String streamContents: [ :out |
    (customMapping value: (NeoJSONWriter on: out)) nextPut: data as: 
#TransferObject ].
  result := (customMapping value: (NeoJSONReader on: json readStream)) nextAs: 
#TransferObject.
  self assert: result equals: data

Does this help ?

Sven

> On 26 Sep 2017, at 12:01, jtuc...@objektfabrik.de wrote:
> 
> I probably need to send a bit more info.
> 
> My latest attempt was this:
> 
>     mapper := NeoJSONReader new.
> 
>     mapper for: #RootObject customDo: [:mapping | mapping mapWithValueSchema: 
> #TransferMessage].
> 
>     mapper
> 
>         for: #TransferMessage
> 
>         do: [:mapping |
> 
>             (mapping mapInstVar: 'Data' to: 'Data') listOfElementSchema: 
> SomeExistingSmalltalkClass].
> 
> 
> which results in
> 
> Symbol does not understand allInstVarNames
> 
> Please note there is no class named #TransferMessage in my Image. This works 
> for things like #ArrayOfPoints in the examples on the NeoJSON git page and it 
> also works in some of my other JSON mappings in different contexts.
> 
> I guess this is because I cannot map to instvars if there is no class/object 
> to store values into...
> So do I really need to implement an otherwise useless Smalltalk class just to 
> have something to map the outermost {} object to?
> 
> I also tried variations of for:customDo: for the mapping of the 
> #TransferMessage and had no success so far.
> 
> Any ideas?
> 
> 
> Joachim
> 
> 
> Am 26.09.17 um 08:29 schrieb jtuc...@objektfabrik.de:
>> Hi there,
>> 
>> 
>> I am having a hard time configuring a NeoJSONReader for a JSON file like 
>> this:
>> 
>> {
>>     "Paging": { someStuff
>>        },
>>     "ErrorMessage": null,
>>     "ErrorCode": 0,
>>     "Data": [
>>         {object1},
>> 
>>         {object2}
>> 
>>         ]
>> 
>> }
>> 
>> 
>> Where I want to ignore everything but the list named "Data". (Of course I'll 
>> have to take a look at ErrorMessages later, but one step after the other...).
>> 
>> I've tried several combinations of mappers and always end up with NoeJSON 
>> parsing Exceptions. I semm unable to see the obvious.
>> 
>> In The end I just want to only use the contents of the Data list and map 
>> each entry in there to some smalltalk object. I was successful with other 
>> structures and lists, but this time I am unable to see the forest among all 
>> those trees.
>> 
>> Any hints?
>> 
>> 
>> Joachim
>> 
>> 
>> 
>> 
>> 
> 
> -- 
> -----------------------------------------------------------------------
> Objektfabrik Joachim Tuchel          mailto:jtuc...@objektfabrik.de
> Fliederweg 1                         http://www.objektfabrik.de
> D-71640 Ludwigsburg                  http://joachimtuchel.wordpress.com
> Telefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1
> 
> 


Reply via email to