2

My app creates some ObJC objects from JSON. Things were working fine, until I added a new property to my ObjC model class, which doesn't have a counterpart in JSON.

I've configured the mapping as follows:

+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{
             @"firstName"                   : @"firstname",
             @"middleName"                  : @"middlename",
             @"lastName"                    : @"lastname",
             // etc.
             @"phoneNumber"                 : NSNull.null // no JSON data for this one
             };
}

However I get an assertion failure in Mantle, in MTLJSONAdapter initWithModelClass: "phoneNumber must either map to a JSON key path or a JSON array of key paths, got: null."

This is how I create the model object:

MyData *myData = [MTLJSONAdapter modelOfClass:[MyData class] 
                           fromJSONDictionary:json error:&error];

How can I have the phoneNumber property in the data class without it mapping to a JSON value?

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76

2 Answers2

4

Just don't specify its mapping to + JSONKeyPathsByPropertyKey.

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
almas
  • 7,090
  • 7
  • 33
  • 49
  • Thanks. I didn't even try that because I was sure Mantle does default property mappings. – TotoroTotoro Jul 09 '15 at 16:18
  • 2
    Mantle 1 implicitly maps fields with the same name, Mantle 2 does not – see the [changelog](https://github.com/Mantle/Mantle/blob/master/CHANGELOG.md#explicit-json-key-paths). – David Snabel-Caunt Jul 10 '15 at 10:01
  • @DavidCaunt that makes sense, thanks. I remember Mantle doing the default property mapping, but I didn't know they've changed that logic. – TotoroTotoro Jul 10 '15 at 16:23
0

The accepted answer didn't end up working for me (I wasn't parsing from a JSON object) and I found subclassing the encodingBehaviorsByPropertyKey to work. The comments in the header file read as follows:

/// Determines how the +propertyKeys of the class are encoded into an archive.
/// The values of this dictionary should be boxed MTLModelEncodingBehavior
/// values.
///
/// Any keys not present in the dictionary will be excluded from the archive.
///
/// Subclasses overriding this method should combine their values with those of
/// `super`.
///
/// Returns a dictionary mapping the receiver's +propertyKeys to default encoding
/// behaviors. If a property is an object with `weak` semantics, the default
/// behavior is MTLModelEncodingBehaviorConditional; otherwise, the default is
/// MTLModelEncodingBehaviorUnconditional.
+ (NSDictionary *)encodingBehaviorsByPropertyKey;

Overriding that method from within my MTLModel subclass like so ended up working for me:

+ (NSDictionary *)encodingBehaviorsByPropertyKey {
    NSMutableDictionary *encodingBehaviorsDictionary = [[super encodingBehaviorsByPropertyKey] mutableCopy];
    [encodingBehaviorsDictionary removeObjectForKey:@"propertyToBeOmitted"];
    return encodingBehaviorsDictionary;
}

FYI: I'm using Mantle version 2.1.

Community
  • 1
  • 1
Stunner
  • 12,025
  • 12
  • 86
  • 145