Skip to content

Commit

Permalink
Add README
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Jul 11, 2014
1 parent f4287cf commit a2b9d81
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Groot
With Groot you can convert JSON dictionaries and arrays to and from Core Data managed objects.

## Requirements
Groot supports OS X 10.8+ and iOS 6.0+.

## Installation
Add the following to your `Podfile` and run `$ pod install`.

``` ruby
pod 'Groot'
```

If you don't have CocoaPods installed or integrated into your project, you can learn how to do so [here](http://cocoapods.org).

## Usage
Consider the following Core Data model for a superhero database:

![Model](https://raw.githubusercontent.com/gonzalezreal/Groot/master/Images/sample-model.jpg)

Using Groot we could insert JSON data with a simple method call:

```objc
NSDictionary *batmanJSON = @{
@"id": @1699,
@"name": @"Batman",
@"real_name": @"Bruce Wayne",
@"powers": @[
@{
@"id": @4,
@"name": @"Agility"
},
@{
@"id": @9,
@"name": @"Insanely Rich"
}],
@"publisher": @{
@"id": @10,
@"name": @"DC Comics"
}
};

NSError *error = nil;
NSManagedObject *batman = [GRTJSONSerialization insertObjectForEntityName:@"Character"
fromJSONDictionary:batmanJSON
inManagedObjectContext:context
error:&error];
```
The serialization process can be customized by adding certain information to the user dictionary available for Core Data *entities*, *attributes* and *relationships*.
You can specify how an attribute or a relationship is mapped to JSON using the `JSONKeyPath` option. If this option is not present, then the attribute name will be used. If `JSONKeyPath` is associated with the string `null`, then the attribute or relationship will not participate in JSON serialization.
![Property User Info](https://raw.githubusercontent.com/gonzalezreal/Groot/master/Images/property-userInfo.jpg)
### Merge
Groot provides methods to merge (that is, create or update) managed objects from JSON representations. To use the merge methods, you need to specify an identity attribute on each *entity*, using the `identityAttribute` option.
![Entity User Info](https://raw.githubusercontent.com/gonzalezreal/Groot/master/Images/entity-userInfo.jpg)
```objc
NSDictionary *updateJSON = @{
@"id": @1699,
@"real_name": @"Guille Gonzalez"
}
// This will return the previously created managed object
NSManagedObject *batman = [GRTJSONSerialization mergeObjectForEntityName:@"Character"
fromJSONDictionary:batmanJSON
inManagedObjectContext:context
error:NULL];
```

### Convert a NSManagedObject into a JSON representation

You can convert managed objects into its JSON representation by using `JSONDictionaryFromManagedObject:` or `JSONArrayFromManagedObjects:`.

```objc
NSDictionary *JSONDictionary = [GRTJSONSerialization JSONDictionaryFromManagedObject:someManagedObject];
```
## Contact
[Guillermo Gonzalez](http://github.com/gonzalezreal)
[@gonzalezreal](https://twitter.com/gonzalezreal)
## License
Overcoat is available under the MIT license. See [LICENSE](https://github.com/gonzalezreal/Groot/blob/master/LICENSE).

0 comments on commit a2b9d81

Please sign in to comment.