I would use any of the following:

ex := {
  #track->#pharo.
  #language->#smalltalk.
  #exercises->{
    #slug->'hello'.
    #id->55.
    #topics->#('a' 'b' 'c') } asDictionary } asDictionary.

ex := {
  #track->#pharo.
  #language->#smalltalk.
  #exercises->({
    #slug->'hello'.
    #id->55.
    #topics->#('a' 'b' 'c') } as: NeoJSONObject) } as: NeoJSONObject.

ex := NeoJSONObject new
  track: #pharo;
  language: #smalltalk;
  exercises: (NeoJSONObject new
    slug: 'hello';
    id: 55;
    topics: #('a' 'b' 'c')).

> On 1 Mar 2019, at 05:01, Jimmie Houchin <jlhouc...@gmail.com> wrote:
> 
> I don't know if you really need OrderedDictionary or not. But I am a big fan 
> of and use all the time simple Arrays or OrderedCollection of pairs. It is 
> neat, clean and readable.
> 
> ex := Dictionary newFromPairs: {
>     'track'. 'pharo.
>     'language'. 'smalltalk'.
>     'exercises'. Dictionary newFromPairs: {
>         'slug'. 'hello'.
>         'id'. 55.
>         'topics'. #('a' 'b' 'c')}
> 
> In my app each individual level creates its own Dictionaries.
> 
> I don't if such would be applicable for you.
> 
> I have methods at each level which start like below. I don't know ahead of 
> time whether or not I am receiving a Dictionary or an Array. I use this 
> extensively for configuration that is readable and compact. I prefer this to 
> the above when possible. And prefer pairs to associations. To my eyes it is 
> cleaner.
> 
> config := {
>     'track'. 'pharo.
>     'language'. 'smalltalk'.
>     'exercises'. {
>         'slug'. 'hello'.
>         'id'. 55.
>         'topics'. #('a' 'b' 'c')}
> 
> myMethod: config
>     confd := config isDictionary
>         ifTrue: [ config ]
>         ifFalse: [ Dictionary newFromPairs: config ].
>         ...
> 
> I use this also for JSON as I am using a REST API which receives JSON objects 
> but it is also easy to pass in the necessary key:value pairs to the ZnClient.
> 
> I don't know if this is of any value. But it is something I do extensively 
> and pervasively in my app. I spent a lot of time thinking and iterating on 
> how to handle large, complex, and nested configurations and enable them to be 
> as clean, neat and concise as possible without limiting them in any 
> foreseeable manner.
> 
> Jimmie
> 
> 
> 
> On 2/28/19 7:45 AM, Tim Mackinnon 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