iosjsonjsonmodelpocket

iOS - Convert IFNULL(...) JSON key into object attribute


I'm working with Pocket API and getting a user's articles. I use the JSONModel library that I found on GitHub to make Article objects using the attributes that I get from the JSON response. Each Article object has a wordCount attribute that used to be easily assigned from part of Pocket's API response:

"word_count" = 947;

But now, for some reason, Pocket changed the way it gives the word count, and now it gives the following:

"IFNULL(e.word_count, 0)" = 947;

This crashed my app because my JSONModel library was expecting each article to have a word_count attribute so it could make it into an Article object with a wordCount attribute. How do I fix this?

The following is my code for the Article.h object setup:

#import "JSONModel.h"

@class Article;

@interface Article : JSONModel

@property (strong, nonatomic) NSString<Optional> *newsSource;
@property (nonatomic, strong) NSString *givenUrl;
@property (strong, nonatomic) NSString *resolvedUrl;
@property (nonatomic, strong) NSString *resolvedTitle;
@property (nonatomic, strong) NSString *excerpt;
@property (strong, nonatomic) NSDictionary<Optional> *image;
@property (nonatomic, strong) NSNumber *wordCount;
@property (strong, nonatomic) NSString<Optional> *fullText;
@property (strong, nonatomic) NSString<Optional> *summary;
@property (strong, nonatomic) NSString *itemId;
@property (strong, nonatomic) NSNumber *favorite;
@property (strong, nonatomic) NSNumber *hasImage;
@property (strong, nonatomic) NSNumber *status;
@property (strong, nonatomic) NSDictionary<Optional> *authors;
@property (strong, nonatomic) NSNumber *sortId;

+(void)retrieveArticlesWithState:(NSString*)state andFavorite:(NSString *)favorite andOffset:(NSNumber*)offsetInteger successCallback:(void (^)(NSArray*))success errorCallback:(void (^)(NSString*))error;

This above code is letting JSONModel know what attributes to expect from the JSON response and turn into attributes for each Article object.

In my Article.m file, I implement the method with the following:

+(void)retrieveArticlesWithState:(NSString*)state andFavorite:(NSString *)favorite andOffset:(NSNumber*)offsetInteger successCallback:(void (^)(NSArray*))success errorCallback:(void (^)(NSString*))error
{
    [self letPocketRetrieveArticlesWithState:state andFavorite:favorite andOffset:offsetInteger successCallback:[Article modelListCallback:success] errorCallback:error];
}

This method helps me convert attributes like word_count to wordCount:

#pragma mark - helpers

//Built in option for JSONModel to turn things like sort_id into sortId
+(JSONKeyMapper*)keyMapper
{
    return [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase];
}

This code helps me consistently convert into multiple Article objects to return a list:

/*!
 * @brief Helper method to get the same block to consistently turn API response into article models.
 * @param callback A block to call once the article has been parsed from the API response.
 * @return A block to be passed along to API requests for parsing out API responses consistently.
 */
+(void(^)(NSDictionary*))modelListCallback:(void (^)(NSArray*))callback {
    return ^(NSDictionary* json){
        NSError* err = nil;
        NSMutableArray* articles = [[NSMutableArray alloc] init];
        for(NSDictionary* article in [[json objectForKey:@"list"] allValues]) {
            [articles addObject:[[Article alloc] initWithDictionary:article error:&err]];
        }
        callback(articles);
    };
}

This is part of the example JSON response that I get:

{
    complete = 1;
    error = "<null>";
    list =     {
        1027166096 =         {
            "IFNULL(e.word_count, 0)" = 2930;
            authors =             {
                37936109 =                 {
                    "author_id" = 37936109;
                    "item_id" = 1027166096;
                    name = "Telegraph Reporters";
                    url = "http://www.telegraph.co.uk/authors/telegraph-reporters/";
                };
            };
            excerpt = "In his third year at Oxford, as an undergraduate studying biology and physiology, Oliver Sacks was not yet 20 years old when he first decided to take LSD.";
            favorite = 0;
            "given_title" = "Oliver Sacks' most mind-bending experiment";
...
...
...

Solution

  • this looks like a bug on their side ... "IFNULL(e.word_count, 0)" is an SQL code that should not appear as a key in the JSON response. If you have a way to you should report this bug to them