utahiosmac / Marshal

Marshaling the typeless wild west of [String: Any]
MIT License
697 stars 62 forks source link

Add possibility to use indexes for array objects nested by keypath #134

Closed rafalwojcik closed 1 year ago

rafalwojcik commented 5 years ago

This PR adds a new feature of getting the object at a particular index by keyPath. Let's say that we have JSON like this:

{
   "id": 5393,
   "title": "",
   "content": "",
   "categories": [
       {
          "id": 321
          "name": "Main Category"
       }, {
          "id": 321
          "name": "Child Category"
       },
   ]
}

Right now is not possible to parse just first object from category array. After my change you can use index in keypath like this: category = try object.value(for: "categories.0") this will take a look inside array if object at key categories is array if not it will raise exception or follow keyPath as string value of 0.

JetForMe commented 5 years ago

I think there's precedent for specifying the index inside square brackets.

rafalwojcik commented 5 years ago

@JetForMe no need to specify square brackets. If a nested object is not array it will start looking for 0 as key. I think it is the right behavior.