1. A JSON "object" corresponds to some kind of dictionary in Smalltalk. 2. {#a -> 1. #b -> 2} is NOT A DICTIONARY. It is an Array of Associations. 3. {#a -> 1. #b -> 2} asDictionary *is* a dictionary.
4. My Smalltalk allows #{#a -> 1. #b -> 2} -- how could I let it be less expressive than -- the language named for a snake? On Fri, 1 Mar 2019 at 02:57, Tim Mackinnon <tim@testit.works> wrote: > As I merrily chat to myself - I noticed that STONWriter almost does what I > need - it just complains in #writeAssociation: so I make a subclass > STONJSONWriter and override: > > writeAssociation: association > “don’t smack me…. > jsonMode > ifTrue: [ self error: 'wrong object class for JSON mode' > ]." > self > encodeKey: association key > value: association value > > I get exactly what I would expect - valid Json from simple Collections, > Associations and Array. > > So would it be handy to have such a writer in the image (or let STONWriter > be more configurable for this?) > > Tim > > p.s. my finally example would then be: > > ex := { 'track'-> 'pharo'. > 'language' -> 'smalltalk'. > 'exercises' -> > {'slug' -> 'hello'. > 'id' -> 55. > 'topics' -> #('a' 'b' 'c') } > }. > > String streamContents: [ :stream | > (STONJSONWriter on: (stream)) > jsonMode: true; > prettyPrint: true; > writeList: ex ]. > > > On 28 Feb 2019, at 13:45, Tim Mackinnon <tim@testit.works> wrote: > > > > Just to add more flavour to this - it seems quite wordy that we have to > do this (or equivalent) to write some config. > > > > ex := OrderedDictionary new > > at: 'track' put: 'pharo'; > > at: 'language' put: 'smalltalk'; > > at: 'exercises' put: ( > > OrderedDictionary new > > at: 'slug' put: 'hello'; > > at: 'id' put: 55; > > at: 'topics' put: #('a' 'b' 'c'); > > yourself ); > > yourself. > > > > String streamContents: [ :stream | > > (NeoJSONWriter on: (stream)) prettyPrint: true; > > mapInstVarsFor: Association; > > nextPut: ex ]. > > > > So I’m still wondering the NeoJSONObjectMapping can do something easy > for Association other than simply mapInstVars? > > > > > >> On 28 Feb 2019, at 13:36, Tim Mackinnon <tim@testit.works> wrote: > >> > >> Hi Sven - is there no convenience shortcut we can use in our code to > make this less wordy when we are specifying it? > >> > >> E.g. the following is very convenient to write - but doesn’t work (as > you have $‘ and not $“ ) > >> > >> ex := { 'track'-> 'pharo'. > >> 'language' -> 'smalltalk'. > >> 'exercises' -> > >> {'slug' -> 'hello'. > >> 'id' -> 55. > >> 'topics' -> #('a' 'b' 'c') } > >> }. > >> > >> String streamContents: [ :stream | > >> (STONWriter on: (stream)) prettyPrint: true; writeList: ex ]. > >> > >> I had thought maybe NeoJSON might help and put: > >> > >> String streamContents: [ :stream | > >> (NeoJSONWriter on: (stream)) prettyPrint: true; > >> "mapInstVarsFor: Association;" > >> nextPut: ex ]. > >> > >> But I get the error about missing an association mapping. If I > uncomment that bit - I get things like: { "value" : “pharo" }, > >> > >> So is there a way I can write a simple mapper for Association that will > write out the key in a string and the value in a string? > >> > >> I’m quite suprised we can’t easily write out fragments of Json in our > code in a light weight way? Or do I need to make a proper Config object and > then teach it how to map properly such that rather than fiddling with our { > x->y } dictionary sugar I do something like: > >> > >> Config new at: ‘id’ is: 123; at: ‘name’ is: ‘Tim’; at: ‘exercises’ is: > #(1 2 3). > >> > >> And I guess at:is: can do the Association asDictionary thing? > >> > >> But I thought Neo might give me something like that, as it must be > terribly common? > >> > >> Tim > >> > >>> On 28 Feb 2019, at 13:16, Sven Van Caekenberghe <s...@stfx.eu> wrote: > >>> > >>> STONJSON toString: { #id->1. #name->'tim' } asDictionary. > >>> > >>> JSON cannot deal with Associations by themselves. > >>> > >>>> On 28 Feb 2019, at 14:05, Tim Mackinnon <tim@testit.works> wrote: > >>>> > >>>> I am stumped about how to write out some simple json (for a config > file). I didn't think I need Neo, and STONJSON would be fine but it seems > like creating items like: > >>>> > >>>> { 'id'-> self id. 'name' -> self name } > >>>> > >>>> gives an error about the association. I think you have to do: { > ('id'-> self id) asDictionary. ('name' -> self name) asDictionary } > everywhere…. > >>>> > >>>> But when I switch over to NeoJsonWriter it also complains about > Assocations too. I just want a simple output like: > >>>> { "id" : 12, "name" : "tim” } > >>>> > >>>> I thought it was simple to do this? Am I missing something obvious. > >>>> > >>>> Tim > >>> > >>> > >> > >> > > > > > > >