[NeoJSON] creating custom maps
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
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 <sergio.rrd@gmail.com> 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
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
On 30 Jan 2017, at 22:32, sergio ruiz <sergio.rrd@gmail.com> 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
Looking at this now.. thanks! On January 30, 2017 at 4:41:59 PM, Sven Van Caekenberghe (sven@stfx.eu) 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
participants (2)
-
sergio ruiz -
Sven Van Caekenberghe