NSManagedObject-HYPPropertyMapper 4.1.4

NSManagedObject-HYPPropertyMapper 4.1.4

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Oct 2016
SPMSupports SPM

Maintained by Christoffer Winterkvist, Elvis Nuñez.



Filling a NSManagedObject with JSON

Mapping your Core Data objects with your JSON providing backend has never been this easy.

JSON in CamelCase

{
  "firstName": "John",
  "lastName": "Hyperseed"
}
NSDictionary *values = [JSON valueForKey:@"user"];
[user hyp_fillWithDictionary:values];

Your Core Data entities should match your backend models. Your attributes should match their JSON counterparts. For example firstName maps to firstName, address to address.

There are two exceptions to this rule:

  • ids should match remoteID
  • Reserved attributes should be prefixed with the entityName (type becomes userType, description becomes userDescription and so on). In the JSON they don't need to change, you can keep type and description for example. A full list of reserved attributes can be found here

JSON in snake_case

{
  "first_name": "John",
  "last_name": "Hyperseed"
}
NSDictionary *values = [JSON valueForKey:@"user"];
[user hyp_fillWithDictionary:values];

Your Core Data entities should match your backend models but in camelCase. Your attributes should match their JSON counterparts. For example first_name maps to firstName, address to address.

There are two exceptions to this rule:

  • ids should match remoteID
  • Reserved attributes should be prefixed with the entityName (type becomes userType, description becomes userDescription and so on). In the JSON they don't need to change, you can keep type and description for example. A full list of reserved attributes can be found here

Custom

If you want to map your Core Data attribute with a JSON attribute that has different naming, you can do by adding hyper.remoteKey in the user info box with the value you want to map.

Remote mapping documentation

Attribute Types

For mapping for arrays and dictionaries just set attributes as Binary Data on the Core Data modeler

screen shot 2015-04-02 at 11 10 11 pm

Dates

We went for supporting ISO 8601 and unix timestamp out of the box because those are the most common formats when parsing dates, also we have a quite performant way to parse this strings which overcomes the performance issues of using NSDateFormatter.

NSDictionary *values = @{@"created_at" : @"2014-01-01T00:00:00+00:00",
                         @"updated_at" : @"2014-01-02",
                         @"published_at": @"1441843200"
                         @"number_of_attendes": @20};

[managedObject hyp_fillWithDictionary:values];

NSDate *createdAt = [managedObject valueForKey:@"createdAt"];
// ==> "2014-01-01 00:00:00 +00:00" 

NSDate *updatedAt = [managedObject valueForKey:@"updatedAt"];
// ==> "2014-01-02 00:00:00 +00:00" 

NSDate *publishedAt = [managedObject valueForKey:@"publishedAt"];
// ==> "2015-09-10 00:00:00 +00:00" 

If your date is not ISO 8601 compliant, you can use a transformer attribute to parse your date, too. First set your attribute to Transformable, and set the name of your transformer like, in this example is DateStringTransformer:

transformable-attribute

You can find an example of date transformer in DateStringTransformer.

Array

NSDictionary *values = @{@"hobbies" : @[@"football",
                                        @"soccer",
                                        @"code"]};

[managedObject hyp_fillWithDictionary:values];

NSArray *hobbies = [NSKeyedUnarchiver unarchiveObjectWithData:managedObject.hobbies];
// ==> "football", "soccer", "code" 

Dictionary

NSDictionary *values = @{@"expenses" : @{@"cake" : @12.50,
                                         @"juice" : @0.50}};

[managedObject hyp_fillWithDictionary:values];

NSDictionary *expenses = [NSKeyedUnarchiver unarchiveObjectWithData:managedObject.expenses];
// ==> "cake" : 12.50, "juice" : 0.50

JSON representation from a NSManagedObject

UserManagedObject *user;
[user setValue:@"John" forKey:@"firstName"];
[user setValue:@"Hyperseed" forKey:@"lastName"];

NSDictionary *userValues = [user hyp_dictionary];

That's it, that's all you have to do, the keys will be magically transformed into a snake_case convention.

{
  "first_name": "John",
  "last_name": "Hyperseed"
}

It supports relationships too, and we complain to the Rails rule accepts_nested_attributes_for, for example for a user that has many notes:

"first_name": "John",
"last_name": "Hyperseed",
"notes_attributes": [
  {
    "0": {
      "id": 0,
      "text": "This is the text for the note A"
    },
    "1": {
      "id": 1,
      "text": "This is the text for the note B"
    }
  }
]

If you don't want to get nested relationships you can also ignore relationships:

NSDictionary *dictionary = [user hyp_dictionaryUsingRelationshipType:HYPPropertyMapperRelationshipTypeNone];
"first_name": "John",
"last_name": "Hyperseed"

Or get them as an array:

NSDictionary *dictionary = [user hyp_dictionaryUsingRelationshipType:HYPPropertyMapperRelationshipTypeArray];
"first_name": "John",
"last_name": "Hyperseed",
"notes": [
  {
    "id": 0,
    "text": "This is the text for the note A"
  },
  {
    "id": 1,
    "text": "This is the text for the note B"
  }
]

Installation

NSManagedObject-HYPPropertyMapper is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'NSManagedObject-HYPPropertyMapper'

Contributing

Please check our playbook for guidelines on contributing.

Credits

Hyper made this. We're a digital communications agency with a passion for good code, and if you're using this library we probably want to hire you.

License

NSManagedObject-HYPPropertyMapper is available under the MIT license. See the LICENSE file for more info.