NeoJSON mapping of nil properties

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

NeoJSON mapping of nil properties

Esteban A. Maringolo
Hi all... and Sven ;-)

I'm mapping my objects with NeoJSONWriter, and I found it is not writing the properties whose values are null.

NeoJSONPropertyMapping>>#writeObject: anObject on: jsonMapWriter
| value |
value := getter value: anObject.
value 
ifNotNil: [ jsonMapWriter writeKey: propertyName value: value as: valueSchema ] 


It is a good default (saves space, and cpu cycles). But in my case the consumer of the JSON objects expect the objects to have all the attributes previously defined.

Is there a way I can configure the mapper or the mapping to write null when the value is nil?

Thank you!


Esteban A. Maringolo
Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON mapping of nil properties

Sven Van Caekenberghe-2
Hi Esteban,

Your wish is my command ;-)

In #bleedingEdge you can now do:

  String streamContents: [ :stream |
    (NeoJSONWriter on: stream)
      writeNil: true;
      mapAllInstVarsFor: Point;
      nextPut: Point new.

which will give you:

  {"x":null,"y":null}

instead of:

  {}

which remains the default.

===
Name: Neo-JSON-Core-SvenVanCaekenberghe.28
Author: SvenVanCaekenberghe
Time: 28 May 2014, 8:52:57.799163 pm
UUID: d6927f86-5b87-4ba2-99c1-cfe5b58ed22f
Ancestors: Neo-JSON-Core-SvenVanCaekenberghe.27

Added NeoJSONWriter #writeNil property, defaulting to false; when true, nil instance variables will be written as null by NeoJSONPropertyMapping, else they will be skipped
===
Name: Neo-JSON-Tests-SvenVanCaekenberghe.25
Author: SvenVanCaekenberghe
Time: 28 May 2014, 8:54:10.767617 pm
UUID: f1479d45-8d24-4bf6-b29a-aaf9f984c084
Ancestors: Neo-JSON-Tests-SvenVanCaekenberghe.24

Added NeoJSONWriterTests>>#testPointWriteNil

Added NeoJSONWriter #writeNil property, defaulting to false; when true, nil instance variables will be written as null by NeoJSONPropertyMapping, else they will be skipped
===

I hope this works for you.

Regards,

Sven

On 28 May 2014, at 18:06, Esteban A. Maringolo <[hidden email]> wrote:

> Hi all... and Sven ;-)
>
> I'm mapping my objects with NeoJSONWriter, and I found it is not writing the properties whose values are null.
>
> NeoJSONPropertyMapping>>#writeObject: anObject on: jsonMapWriter
> | value |
> value := getter value: anObject.
> value
> ifNotNil: [ jsonMapWriter writeKey: propertyName value: value as: valueSchema ]
>
>
> It is a good default (saves space, and cpu cycles). But in my case the consumer of the JSON objects expect the objects to have all the attributes previously defined.
>
> Is there a way I can configure the mapper or the mapping to write null when the value is nil?
>
> Thank you!
>
>
> Esteban A. Maringolo


Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON mapping of nil properties

Esteban A. Maringolo
2014-05-28 16:03 GMT-03:00 Sven Van Caekenberghe <[hidden email]>:

>
> Hi Esteban,
>
> Your wish is my command ;-)
>
> In #bleedingEdge you can now do:
>
>   String streamContents: [ :stream |
>     (NeoJSONWriter on: stream)
>       writeNil: true;
>       mapAllInstVarsFor: Point;
>       nextPut: Point new.
>
> which will give you:
>
>   {"x":null,"y":null}
>
> instead of:
>
>   {}
>
> which remains the default.

Thank you Sven!






Esteban A. Maringolo

Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON mapping of nil properties

stepharo
Sven I added a section to new empty chapter on NeoJSON :)
Do you have a doc about NeoJSON that I can use to fill up the chapter :)


On 29/5/14 14:39, Esteban A. Maringolo wrote:

> 2014-05-28 16:03 GMT-03:00 Sven Van Caekenberghe <[hidden email]>:
>> Hi Esteban,
>>
>> Your wish is my command ;-)
>>
>> In #bleedingEdge you can now do:
>>
>>    String streamContents: [ :stream |
>>      (NeoJSONWriter on: stream)
>>        writeNil: true;
>>        mapAllInstVarsFor: Point;
>>        nextPut: Point new.
>>
>> which will give you:
>>
>>    {"x":null,"y":null}
>>
>> instead of:
>>
>>    {}
>>
>> which remains the default.
> Thank you Sven!
>
>
>
>
>
>
> Esteban A. Maringolo
>
>


Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON mapping of nil properties

stepharo
found it and converting it to pillar

     https://github.com/svenvc/docs/blob/master/neo/neo-json-paper.md

Stef

On 2/6/14 22:19, stepharo wrote:

> Sven I added a section to new empty chapter on NeoJSON :)
> Do you have a doc about NeoJSON that I can use to fill up the chapter :)
>
>
> On 29/5/14 14:39, Esteban A. Maringolo wrote:
>> 2014-05-28 16:03 GMT-03:00 Sven Van Caekenberghe <[hidden email]>:
>>> Hi Esteban,
>>>
>>> Your wish is my command ;-)
>>>
>>> In #bleedingEdge you can now do:
>>>
>>>    String streamContents: [ :stream |
>>>      (NeoJSONWriter on: stream)
>>>        writeNil: true;
>>>        mapAllInstVarsFor: Point;
>>>        nextPut: Point new.
>>>
>>> which will give you:
>>>
>>>    {"x":null,"y":null}
>>>
>>> instead of:
>>>
>>>    {}
>>>
>>> which remains the default.
>> Thank you Sven!
>>
>>
>>
>>
>>
>>
>> Esteban A. Maringolo
>>
>>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: NeoJSON mapping of nil properties

stepharo