Skip to content

Commit 0033dbf

Browse files
author
Sven Van Caekenberghe
committed
Merge 3dc8689
2 parents e496011 + 3dc8689 commit 0033dbf

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

repository/Neo-JSON-Core/NeoJSONObject.class.st

+26-11
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ NeoJSONObject class >> fromString: string [
5050
"Parse string as JSON, so that maps become instances of me"
5151

5252
^ (NeoJSONReader on: string readStream)
53-
mapClass: NeoJSONObject;
53+
mapClass: self;
5454
propertyNamesAsSymbols: true;
5555
next
5656
]
@@ -59,10 +59,25 @@ NeoJSONObject class >> fromString: string [
5959
NeoJSONObject >> at: key [
6060
"I return nil for missing keys.
6161
My superclass would signal a KeyNotFound."
62-
62+
6363
^ self at: key ifAbsent: [ nil ]
6464
]
6565

66+
{ #category : #'nested dictionaries' }
67+
NeoJSONObject >> at: firstKey at: secondKey [
68+
"I return nil for missing keys.
69+
My superclass would signal a KeyNotFound."
70+
71+
^ self atPath: { firstKey. secondKey }
72+
]
73+
74+
{ #category : #'nested dictionaries' }
75+
NeoJSONObject >> at: firstKey at: secondKey put: value [
76+
"Store value under secondKey in nested object under firstKey, create new level when needed"
77+
78+
^ self atPath: { firstKey. secondKey } put: value
79+
]
80+
6681
{ #category : #accessing }
6782
NeoJSONObject >> at: key ifPresent: aPresentBlock ifAbsentPut: anAbsentBlock [
6883
"Lookup the given key in the receiver. If it is present, answer the
@@ -71,7 +86,7 @@ NeoJSONObject >> at: key ifPresent: aPresentBlock ifAbsentPut: anAbsentBlock [
7186

7287
"Overwritten to patch a bug in the superclass implementation in Pharo 7 and 8.
7388
This problem was fixed in Pharo 9 where this overwrite is no longer necessary but harmless."
74-
89+
7590
^ self
7691
at: key
7792
ifPresent: aPresentBlock
@@ -81,19 +96,19 @@ NeoJSONObject >> at: key ifPresent: aPresentBlock ifAbsentPut: anAbsentBlock [
8196
{ #category : #accessing }
8297
NeoJSONObject >> atPath: keyCollection [
8398
"Use each key in keyCollection recursively, stop when nil is encountered"
84-
99+
85100
| value |
86101
value := self.
87102
keyCollection do: [ :each |
88103
value := value at: each.
89104
value ifNil: [ ^ nil ] ].
90-
^ value
105+
^ value
91106
]
92107

93108
{ #category : #accessing }
94109
NeoJSONObject >> atPath: keyCollection put: newValue [
95110
"Use each key in keyCollection recursively, create new levels when needed"
96-
111+
97112
| target |
98113
keyCollection ifEmpty: [ ^ self ].
99114
target := self.
@@ -108,7 +123,7 @@ NeoJSONObject >> atPath: keyCollection put: newValue [
108123
NeoJSONObject >> doesNotUnderstand: message [
109124
"Overwritten so that 'self foo' becomes 'self at: #foo'
110125
and 'self foo: 1' becomes 'self at: #foo put: 1' except that self is returned"
111-
126+
112127
| key |
113128
key := message selector.
114129
key isUnary
@@ -121,7 +136,7 @@ NeoJSONObject >> doesNotUnderstand: message [
121136
{ #category : #accessing }
122137
NeoJSONObject >> name [
123138
"Overwritten to make this accessor available as key"
124-
139+
125140
^ self at: #name
126141
]
127142

@@ -131,15 +146,15 @@ NeoJSONObject >> printOn: stream [
131146

132147
[ (NeoJSONWriter on: stream) nextPut: self ]
133148
on: Error
134-
do: [ :exception |
149+
do: [ :exception |
135150
stream
136151
nextPutAll: ' Error printing JSON: ';
137-
nextPutAll: exception printString ]
152+
print: exception ]
138153
]
139154

140155
{ #category : #evaluating }
141156
NeoJSONObject >> value [
142157
"Overwritten to make this accessor available as key"
143-
158+
144159
^ self at: #value
145160
]

repository/Neo-JSON-Tests/NeoJSONObjectTests.class.st

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,24 @@ I am NeoJSONObjectTests, I hold unit tests for NeoJSONObject.
55
Class {
66
#name : #NeoJSONObjectTests,
77
#superclass : #TestCase,
8-
#category : 'Neo-JSON-Tests'
8+
#category : #'Neo-JSON-Tests'
99
}
1010

11+
{ #category : #testing }
12+
NeoJSONObjectTests >> testAtAt [
13+
| object |
14+
object := NeoJSONObject new.
15+
self assert: (object at: #foo) isNil.
16+
self assert: (object at: #foo at: #bar) isNil.
17+
object at: #foo at: #bar put: 123.
18+
self assert: (object at: #foo) notNil.
19+
self assert: (object at: #foo at: #bar) equals: 123.
20+
self assert: object foo bar equals: 123.
21+
object at: #foo at: #bar put: -1.
22+
self assert: (object at: #foo at: #bar) equals: -1.
23+
self assert: (object at: #foo at: #foo) isNil
24+
]
25+
1126
{ #category : #testing }
1227
NeoJSONObjectTests >> testAtPath [
1328
| object |

0 commit comments

Comments
 (0)