Hi Chris,
I find the implementation rather unusual (e.g. the way #inject:into: is used there) and a bit too complicated (many assumptions about what returns what). #path:do: does not work correctly on its own as it relies on the argument block's return value. E.g.:
#((1)) path: #(1 1) do: [ :path :node | ] "=> DNU"
There's also some unexpected behavior:
#('foo') atPath: #(1 1) "=> $f"
I'm not sure these should be available for all collections considering all those assumptions in the method comments. Perhaps a custom class or the class side of Json would be a better place.
Best, Levente
On 2024. 02. 19. 0:01, commits@source.squeak.org wrote:
Chris Muller uploaded a new version of JSON to project The Trunk: http://source.squeak.org/trunk/JSON-cmm.61.mcz
==================== Summary ====================
Name: JSON-cmm.61 Author: cmm Time: 18 February 2024, 6:01:50.22946 pm UUID: 9f100ce1-3d3c-4ea2-b975-4821175b41ba Ancestors: JSON-ct.58
Path access and enumeration for JSON objects.
=============== Diff against JSON-ct.58 ===============
Item was added:
- ----- Method: Collection>>atPath: (in category '*json') -----
- atPath: anArray
- "Assume I'm a set of nested HashedCollections and/or SequenceableCollections. Answer the object at the path of indices and/or keys identified in anArray."
- ^ self
atPath: anArray
ifLost: [ : last | self error: 'path lost after' , last asString ]!
Item was added:
- ----- Method: Collection>>atPath:ifLost: (in category '*json') -----
- atPath: anArray ifLost: aBlock
- "Assume I'm a set of nested HashedCollections and/or SequenceableCollections. Answer the object at the path of indices and/or keys identified in anArray. If the full path specified by anArray isn't present, cull aBlock with the last element present along the path."
- | last |
- ^ (self
path: anArray
do: [ : elem : node | last := node ])
ifNil: [ aBlock cull: last ]
ifNotNil: [ last ]!
Item was added:
- ----- Method: Collection>>path:do: (in category '*json') -----
- path: anArray do: aBlock
- "Assume I'm a set of nested HashedCollections and/or SequenceableCollections. Value aBlock with each object along the path of indices and/or keys identified in anArray. If a path element isn't found, stop, and return nil, otherwise, return self."
- anArray
inject: self
into:
[ : dictOrArray : pathElem | dictOrArray
at: pathElem
ifPresent: [ : node | aBlock value: pathElem value: node ]
ifAbsent: [ ^ nil ] ].
- ^ self!