I really don't understand.

JSON needs Dictionaries and Arrays (or other Sequenceable Collections). That 
has nothing to do with Pharo or how NeoJSON or STONJSON are implemented - it is 
what defines JSON.

Of course, compatible objects work as well (NeoJSONObject is an example). You 
could make your own.

Since there is no literal syntax in Pharo for a Dictionary, you have to write 
something like

>>>  { #id->1. #name->'tim' } asDictionary

This has been discussed for years, I won't start that again. The above is short 
enough for me, but a bit of a pain when nesting.

Part of the power of Pharo is a very simple syntax, we can express an awful lot 
with very few constructs.

Your mapping examples won't work: you try to make something that is not a map 
but a list of associations into a map, that is simply not possible, sorry.

> On 28 Feb 2019, at 14: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
>>> 
>>> 
>> 
>> 
> 
> 


Reply via email to