Latest 4.1.9
Homepage https://github.com/cflorion/NSManagedObject-HYPPropertyMapper
License MIT
Platforms ios 8.0, osx 10.9, watchos 2.0, tvos 9.0, requires ARC
Frameworks Foundation
Authors

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

Remote mapping documentation

  • If you want to map your Core Data identifier (key) 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.

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"
}

Excluding

If you don’t want to export attribute / relationship, you can prohibit exporting by adding hyper.nonExportable in the user info of the excluded attribute.

// TODO: Include photo of user key.

Relationships

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.

Latest podspec

{
    "name": "NSManagedObject-HYPPropertyMapperLBC",
    "version": "4.1.9",
    "summary": "Mapping your Core Data objects with your JSON providing backend has never been this easy",
    "description": "* Mapping your Core Data objects with your JSON providing backend has never been this easy",
    "homepage": "https://github.com/cflorion/NSManagedObject-HYPPropertyMapper",
    "license": {
        "type": "MIT",
        "file": "LICENSE.md"
    },
    "authors": {
        "SyncDB": "[email protected]"
    },
    "social_media_url": "https://twitter.com/Sync_DB",
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/cflorion/NSManagedObject-HYPPropertyMapper.git",
        "tag": "4.1.9"
    },
    "source_files": "Sources/**/*",
    "frameworks": "Foundation",
    "requires_arc": true
}

Pin It on Pinterest

Share This