> On 26 Apr 2017, at 01:01, PBKResearch <pe...@pbkresearch.co.uk> wrote:
> 
> Browsing in Pharo, I found that Dictionary has a subclass KeyedTree, which is 
> in effect a set of nested dictionaries. Retrieval is by quoting a path, which 
> is simply an array of keys. So in your case it would read:
> dict1 atPath: #(’key1’ ‘key2’ ‘key3’). 
> This might be more efficient than repeated use of the DNU mechanism. It is 
> not clear (to me, at least) how to construct the nested dictionaries as a 
> KeyedTree in the first place, but this might be an alternative approach.

I saw this too and I thought it was interesting, hence I added it to 
NeoJSONObject. 

Name: Neo-JSON-Core-SvenVanCaekenberghe.40
Author: SvenVanCaekenberghe
Time: 26 April 2017, 4:35:49.433269 pm
UUID: c336587e-740a-0d00-b51a-dacd0088e41c
Ancestors: Neo-JSON-Core-SvenVanCaekenberghe.39

Name: Neo-JSON-Tests-SvenVanCaekenberghe.38
Author: SvenVanCaekenberghe
Time: 26 April 2017, 4:36:04.583272 pm
UUID: 73623f7f-740a-0d00-b51b-f4000088e41c
Ancestors: Neo-JSON-Tests-SvenVanCaekenberghe.37

From the class comment:

Additionally, I support path access for nested instances of me, using #atPath: 
and #atPath:put:

The first is special because it returns nil as soon as a key is missing. The 
second is special because it creates extra levels (instances of me) as needed 
to follow the path of keys.

        NeoJSONObject new atPath: #(one two three) put: 42; yourself.
        NeoJSONObject new atPath: #(one two three) put: 42; atPath: #(one two 
three).

For the examples given above, the actual JSON looks like this:

        {"one":{"two":{"three":42}}}

Sven

> Hope this helps
>  
> Peter Kenny
>  
> From: Pharo-users [mailto:pharo-users-boun...@lists.pharo.org] On Behalf Of 
> Markus Böhm
> Sent: 24 April 2017 18:42
> To: pharo-users@lists.pharo.org
> Subject: [Pharo-users] Hot to retrieve values from Nested Dictionaries
>  
> May I ask:
> What's the idiomatic way to retrieve values from nested dictionaries?
>  
> (((dict1 at: 'key1') at: 'key2') at: 'key3')
>  
> Are all the brackets really necessary?
>  
> BR Mike


Reply via email to