Search code examples
iosjsonjsonmodel

JSONModel Optional and Ignore are not respected


I have these properties in the header file.

@property (copy, nonatomic) NSString<Optional>* guidId;
@property (copy, nonatomic) NSDate* created_at;
@property (copy, nonatomic) NSNumber<Ignore>* longitude;
@property (copy, nonatomic) NSNumber<Ignore>* latitude;
@property (copy, nonatomic) NSNumber* altitude;
@property (copy, nonatomic) NSNumber* relative_altitude;
@property (copy, nonatomic) NSNumber* value;
@property (copy, nonatomic) NSString<Optional>* scale;
@property (copy, readonly, nonatomic, getter=getReadingTypeDescription) NSString* name;
@property (assign, nonatomic) WEEReadingType readingType;
@property (copy, nonatomic) NSString* name;
@property (strong, nonatomic) NSMutableArray* location;

Though it is strange Optional and Ignore are not respected.

In the implementation file I override these two methods.

// This one because when converting to JSON from Model I need specific properties only and Ignore is not for this purpose

    - (NSString *)toJSONString
        {
        return [super toJSONStringWithKeys:
            @[
            @"guidId",
            @"location",
            @"altitude",
            @"relative_altitude",
            @"scale",
            @"name",
            @"value",
            @"created_at"
            ]];
        }


// And this one for the enum property that I want to ignore, but I found out I get an error when using [JSONModel arrayOfModelsFromDictionaries:json error:&error] that Optional or Ignore is not respected.

    + (BOOL)propertyIsIgnored:(NSString *)propertyName
        {
        if ([propertyName isEqualToString:@"readingType"] ||
            [propertyName isEqualToString:@"guidId"] ||
            [propertyName isEqualToString:@"longitude"] ||
            [propertyName isEqualToString:@"latitude"])
            {
            return YES;
            }
        return NO;
        }

Example of JSON I need to convert to my model.

[
{
"created_at": "2015-03-20T19:26:28.000Z",
"location": [
40.70739851802653,
-74.005788154969
],
"altitude": 4.063048774263429,
"relative_altitude": 0.0,
"name": "pressure",
"value": 1014.718704223633,
"scale": "mb"
},
{
"created_at": "2015-03-20T19:26:28.000Z",
"location": [
40.70739851802653,
-74.005788154969
],
"altitude": 4.063048774263429,
"relative_altitude": 0.0,
"name": "pressure",
"value": 1014.718704223633,
"scale": null
}
]

The above with [JSONModel arrayOfModelsFromDictionaries:json error:&error] will throw the following error.

Error Domain=JSONModelErrorDomain Code=1 "Invalid JSON data: Value of required model key scale is null" UserInfo=0x170265cc0 {NSLocalizedDescription=Invalid JSON data: Value of required model key scale is null, kJSONModelKeyPath=[46].scale}

Any ideas what is wrong?


Solution

  • Just try using +(BOOL)propertyIsOptional:(NSString*)propertyName