tristanhimmelman/AlamofireObjectMapper

keypath: to allow array index targeting "AnArray.0.ObjectOfArrayItem"

Closed this issue · 3 comments

Greets,

Can we make keypath respect array indexes to target a specific array before parsing. I'm trying to access "AnArray.0.ObjectOfArrayItem", i.e.

.responseArray(queue: nil, keyPath:"Galleries.0.Images") {

To parse the first Gallery from an array's Images tag as the starting point

{
"Galleries":[
   {
      "GalleryField":"First gallery has Images of interest",
      "Images":[
         {
         "URL":"http://example.com",
         "Caption":"First Image"
         },
         {
         "URL":"http://example.com",
         "Caption":"Second Image"
         }
         ]
   },
   {
      "GalleryField":"Subsequent galleries are not of interest",
      "Images":[
         {
         "URL":"http://example.com",
         "Caption":"inb4404"
         },
         {
         "URL":"http://example.com",
         "Caption":"MOAR IMG!!!"
         }
         ]
   }
   ]
}

This prevents having to create redundant classes for top-level items

I have been considering adding this as well. I've marked this as an enhancement to keep track of it until I have time to work on it.

Feel free to make a Pull request if you would like to add the functionality

4np commented

Bump, I think this is pretty useful as well... I am using ObjectMapper with AlamoFire through AlamofireObjectMapper and some of the API results include an array containing a single object (which is not that great API design, but that's what I need to work with.)

Example:

// get the first element in the array
let keyPath = "result.0"
Alamofire.request(...).responseObject(queue: dispatchQueue, keyPath: keyPath) { ... }

Example JSON response:

{  
   "result":[  
      {  
         "someObject": {  ...  }
      }
   ]
}

As a workaround I am now using responseArray and getting the first object which works but responseObject would be more appropriate in this case...

+1, this would be useful.