Marshal
Marshal copied to clipboard
Add possibility to use indexes for array objects nested by keypath
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
.
I think there's precedent for specifying the index inside square brackets.
@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.