Git Product home page Git Product logo

nsmanagedobject-hyppropertymapper's Introduction

Filling a NSManagedObject with JSON

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

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

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 just supporting ISO8601 out of the box because that's the most common format 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",
                         @"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" 

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

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.