Skip to content

A way to handle APIs which return collections? #639

@stevendesu

Description

@stevendesu

I'm hitting an API whose return value is a JSON array containing multiple objects with the same properties, like so:

[
    { "title": "A", "description": "Item A" },
    { "title": "B", "description": "item B" },
    ...
]

Is there any way to read in such a structure using JSONModel? Given a single item I can say [Item initWithString:singleItem error:&error], but I can't exactly say [NSArray<Item> initWithString:items error:&error]...

If there is currently no way to handle this, could one be added? For instance, a class could contain only a single property which is an NSArray, then some pre-defined method could declare how to handle root collections:

@interface ItemList : JSONModel

@property NSArray<Item> items;

@end

@implementation ItemList

- (void) handleJSONRootCollection:(NSArray *)collection
{
    _items = collection;
}

@end

Alternatively, along with initWithString an initWithStringCollection (or similar) could be added which returns an NSArray of the parent type:

NSArray<Item> *items = [Item initWithStringCollection:string];

I'm not sure how easy this would be to implement (or how common of a problem it is), but it's something I ran into

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions