On 21 Feb 2014, at 09:18, Norbert Hartl <norbert@hartl.name> wrote:
Am 21.02.2014 um 07:50 schrieb itlists@schrievkrom.de:
I have a class with an instance attribute "x" and this one contains an integer value.
No when exporting this to json I want to write a string instead of this number to the json string ...
neoJsonMapping: mapper mapper for: self do: [ :mapping | (mapping mapInstVar: #x to: 'x') valueSchema: #AsString. ].
mapper for: #AsString customDo: [ :mapping | mapping encoder: [ :anInteger | x asString ]. mapping decoder: [ :aString | ... ] ].
But whatever I do ... only numbers are written out ... and yes of course I could add additional accessors doing the conversion.
Try
mapping mapProperty: #num getter: [ :object | (object perform: #num) asString ] setter: [ :object :value | object perform: #num asMutator with: value asNumber ] ]
Norbert
That is right, Norbert, thanks. Here is a snippet you can execute in the console: String streamContents: [ :out | (NeoJSONWriter on: out) for: Point do: [ :mapping | mapping mapProperty: #x getter: [ :object | object x asString ] setter: [ :object :value | object setX: value asNumber setY: object y ]. mapping mapProperty: #y getter: [ :object | object y asString ] setter: [ :object :value | object setX: object x setY: value asNumber ] ]; nextPut: 1@2 ]. (NeoJSONReader on: '{"x":"1","y":"2"}' readStream) for: Point do: [ :mapping | mapping mapProperty: #x getter: [ :object | object x asString ] setter: [ :object :value | object setX: value asNumber setY: object y ]. mapping mapProperty: #y getter: [ :object | object y asString ] setter: [ :object :value | object setX: object x setY: value asNumber ] ]; nextAs: Point. It is a bit forced since Point has no #x: or #y: mutators by design, but you get the idea. Sven