Skip to content

Commit

Permalink
Merge 3dc8689
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Van Caekenberghe committed Jun 20, 2023
2 parents e496011 + 3dc8689 commit 0033dbf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
37 changes: 26 additions & 11 deletions repository/Neo-JSON-Core/NeoJSONObject.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ NeoJSONObject class >> fromString: string [
"Parse string as JSON, so that maps become instances of me"

^ (NeoJSONReader on: string readStream)
mapClass: NeoJSONObject;
mapClass: self;
propertyNamesAsSymbols: true;
next
]
Expand All @@ -59,10 +59,25 @@ NeoJSONObject class >> fromString: string [
NeoJSONObject >> at: key [
"I return nil for missing keys.
My superclass would signal a KeyNotFound."

^ self at: key ifAbsent: [ nil ]
]

{ #category : #'nested dictionaries' }
NeoJSONObject >> at: firstKey at: secondKey [
"I return nil for missing keys.
My superclass would signal a KeyNotFound."

^ self atPath: { firstKey. secondKey }
]

{ #category : #'nested dictionaries' }
NeoJSONObject >> at: firstKey at: secondKey put: value [
"Store value under secondKey in nested object under firstKey, create new level when needed"

^ self atPath: { firstKey. secondKey } put: value
]

{ #category : #accessing }
NeoJSONObject >> at: key ifPresent: aPresentBlock ifAbsentPut: anAbsentBlock [
"Lookup the given key in the receiver. If it is present, answer the
Expand All @@ -71,7 +86,7 @@ NeoJSONObject >> at: key ifPresent: aPresentBlock ifAbsentPut: anAbsentBlock [

"Overwritten to patch a bug in the superclass implementation in Pharo 7 and 8.
This problem was fixed in Pharo 9 where this overwrite is no longer necessary but harmless."

^ self
at: key
ifPresent: aPresentBlock
Expand All @@ -81,19 +96,19 @@ NeoJSONObject >> at: key ifPresent: aPresentBlock ifAbsentPut: anAbsentBlock [
{ #category : #accessing }
NeoJSONObject >> atPath: keyCollection [
"Use each key in keyCollection recursively, stop when nil is encountered"

| value |
value := self.
keyCollection do: [ :each |
value := value at: each.
value ifNil: [ ^ nil ] ].
^ value
^ value
]

{ #category : #accessing }
NeoJSONObject >> atPath: keyCollection put: newValue [
"Use each key in keyCollection recursively, create new levels when needed"

| target |
keyCollection ifEmpty: [ ^ self ].
target := self.
Expand All @@ -108,7 +123,7 @@ NeoJSONObject >> atPath: keyCollection put: newValue [
NeoJSONObject >> doesNotUnderstand: message [
"Overwritten so that 'self foo' becomes 'self at: #foo'
and 'self foo: 1' becomes 'self at: #foo put: 1' except that self is returned"

| key |
key := message selector.
key isUnary
Expand All @@ -121,7 +136,7 @@ NeoJSONObject >> doesNotUnderstand: message [
{ #category : #accessing }
NeoJSONObject >> name [
"Overwritten to make this accessor available as key"

^ self at: #name
]

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

[ (NeoJSONWriter on: stream) nextPut: self ]
on: Error
do: [ :exception |
do: [ :exception |
stream
nextPutAll: ' Error printing JSON: ';
nextPutAll: exception printString ]
print: exception ]
]

{ #category : #evaluating }
NeoJSONObject >> value [
"Overwritten to make this accessor available as key"

^ self at: #value
]
17 changes: 16 additions & 1 deletion repository/Neo-JSON-Tests/NeoJSONObjectTests.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,24 @@ I am NeoJSONObjectTests, I hold unit tests for NeoJSONObject.
Class {
#name : #NeoJSONObjectTests,
#superclass : #TestCase,
#category : 'Neo-JSON-Tests'
#category : #'Neo-JSON-Tests'
}

{ #category : #testing }
NeoJSONObjectTests >> testAtAt [
| object |
object := NeoJSONObject new.
self assert: (object at: #foo) isNil.
self assert: (object at: #foo at: #bar) isNil.
object at: #foo at: #bar put: 123.
self assert: (object at: #foo) notNil.
self assert: (object at: #foo at: #bar) equals: 123.
self assert: object foo bar equals: 123.
object at: #foo at: #bar put: -1.
self assert: (object at: #foo at: #bar) equals: -1.
self assert: (object at: #foo at: #foo) isNil
]

{ #category : #testing }
NeoJSONObjectTests >> testAtPath [
| object |
Expand Down

0 comments on commit 0033dbf

Please sign in to comment.