[NeoJSON] creating custom maps

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

[NeoJSON] creating custom maps

sergio_101

Okay, I am having the worst time with this..

i have a class called RadioStation that for now, just had  few text instance variables.

all I really need to do is turn this into JSON..

i can do it with something like:

String streamContents: [ :stream |
   (NeoJSONWriter on: stream)
      prettyPrint: true;
      mapInstVarsFor: RadioStation;
      nextPut: (RadioStation SelectAll last) ]. 
      
but this seems like massive overkill.. plus, very soon, this is going to have collections of other objects..

also, since this returns a string.. when Teapot returns this, it escapes all of the “s and produces illegal json, as it’s just a string..

i read about mappers, but i can’t figure out how to create them..

doing something simple like:

| mapper server |
mapper := NeoJSONMapper new.

mapper mapAllInstVarsFor: RadioStation.
mapper mappings.

NeoJSONWriter toString: station.

which feels like it should work, gives me:

No mapping found for RadioStation in NeoJSONWriter

any help?

thanks so much!

----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.Village-Buzz.com
http://www.ThoseOptimizeGuys.com
http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101


signature.asc (852 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [NeoJSON] creating custom maps

Sven Van Caekenberghe-2
Hi Sergio,

You probably overlooked the option to add a mapping to the class side. Check the implementors of #neoJsonMapping:

In your case, all you have to do is add the following method.

RadioStation class>>#neoJsonMapping: mapper
  mapper for: self do: [ :mapping | mapping mapInstVars ]

I can't help you with Teapot though. Legal JSON is legal JSON, that is what NeoJSON produces.

Sven

> On 27 Jan 2017, at 22:46, sergio ruiz <[hidden email]> wrote:
>
>
> Okay, I am having the worst time with this..
>
> i have a class called RadioStation that for now, just had  few text instance variables.
>
> all I really need to do is turn this into JSON..
>
> i can do it with something like:
>
> String streamContents: [ :stream |
>    (NeoJSONWriter on: stream)
>       prettyPrint: true;
>       mapInstVarsFor: RadioStation;
>       nextPut: (RadioStation SelectAll last) ].
>      
> but this seems like massive overkill.. plus, very soon, this is going to have collections of other objects..
>
> also, since this returns a string.. when Teapot returns this, it escapes all of the “s and produces illegal json, as it’s just a string..
>
> i read about mappers, but i can’t figure out how to create them..
>
> doing something simple like:
>
> | mapper server |
> mapper := NeoJSONMapper new.
>
> mapper mapAllInstVarsFor: RadioStation.
> mapper mappings.
>
> NeoJSONWriter toString: station.
>
> which feels like it should work, gives me:
>
> No mapping found for RadioStation in NeoJSONWriter
>
> any help?
>
> thanks so much!
>
> ----
> peace,
> sergio
> photographer, journalist, visionary
>
> Public Key: http://bit.ly/29z9fG0
> #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
> http://www.Village-Buzz.com
> http://www.ThoseOptimizeGuys.com
> http://www.coffee-black.com
> http://www.painlessfrugality.com
> http://www.twitter.com/sergio_101
> http://www.facebook.com/sergio101


Reply | Threaded
Open this post in threaded view
|

Re: [NeoJSON] creating custom maps

sergio_101
This works perfectly!

one more question..

how would i go about changing the default behavior in this case for NeoJSONWriter’s writeNil instance variable? 

Thanks!




RadioStation class>>#neoJsonMapping: mapper
mapper for: self do: [ :mapping | mapping mapInstVars ]


----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.Village-Buzz.com
http://www.ThoseOptimizeGuys.com
http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

signature.asc (852 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [NeoJSON] creating custom maps

Sven Van Caekenberghe-2

> On 30 Jan 2017, at 22:32, sergio ruiz <[hidden email]> wrote:
>
> This works perfectly!
>
> one more question..
>
> how would i go about changing the default behavior in this case for NeoJSONWriter’s writeNil instance variable?

I don't understand the question. The #writeNil property of NeoJSONWriter controls if you want nil values to be written out as JSON null or just have them skipped. This is independent of the mapping.

The default is not to write out nil/null properties. If you want to change this, make a writer and set #writeNil: to false.

For an example, see #testPointWriteNil

> Thanks!
>
>
>
>>
>> RadioStation class>>#neoJsonMapping: mapper
>> mapper for: self do: [ :mapping | mapping mapInstVars ]
>>
>>
> ----
> peace,
> sergio
> photographer, journalist, visionary
>
> Public Key: http://bit.ly/29z9fG0
> #BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
> http://www.Village-Buzz.com
> http://www.ThoseOptimizeGuys.com
> http://www.coffee-black.com
> http://www.painlessfrugality.com
> http://www.twitter.com/sergio_101
> http://www.facebook.com/sergio101


Reply | Threaded
Open this post in threaded view
|

Re: [NeoJSON] creating custom maps

sergio_101
Looking at this now.. thanks!



On January 30, 2017 at 4:41:59 PM, Sven Van Caekenberghe ([hidden email]) wrote:

The default is not to write out nil/null properties. If you want to change this, make a writer and set #writeNil: to false.

For an example, see #testPointWriteNil 
----
peace,
sergio
photographer, journalist, visionary

Public Key: http://bit.ly/29z9fG0
#BitMessage BM-NBaswViL21xqgg9STRJjaJaUoyiNe2dV
http://www.Village-Buzz.com
http://www.ThoseOptimizeGuys.com
http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101

signature.asc (852 bytes) Download Attachment