Latest 0.2
Homepage https://github.com/jconst/JCModel
License MIT
Platforms ios 6.0, requires ARC
Dependencies NSObjectProperties
Authors

JCModel is a small, simple framework that automatically converts JSON data to real NSObjects in your data model. It aims to require as little code as possible, by allowing you to specify mappings using PList files, with a simple, easy to understand dot syntax for nested attributes.

Usage

If you’re using AFNetworking (which you should be!) and your class’s properties have the same name as the JSON attributes, using JCModel is dirt simple. Just make your model object (let’s say it’s it’s called Article) a subclass of JCModel, and set the response serializer on your AFNetworking operation to that Article’s JCResponseSerializer, like so:

[operation setResponseSerializer:[Article responseSerializer]];

If multiple objects are returned in an array, use

[Article arrayResponseSerializer];

and if the article(s) aren’t the root object, use something like

[Article arrayResponseSerializerWithRootKeyPath:@"response.content.articles"];

depending on how the JSON looks.

Mapping

I’ve tried to make the process of specifying an object mapping with JCModel as simple as possible. If your object’s properties have the same names as their respective JSON attributes, you’re already done! JCPropertyMapper will automatically map all properties that match.

Otherwise, override the method "mappingPlistName" on your JCModel subclass, and return the name of a Plist in your main bundle. That plist can take two possible forms:

  • If the names all match, as above, but you only want to map a subset of the mappable properties, the plist can be an array of the names of properties you do want mapped.
  • If the names don’t match, the plist will be a dictionary. The keys of this dictionary will be the JSON attribute names, and the dictionary values should be the respective object properties you wish to map to.

Here’s an example from one of my apps:
Example mapping

Some of the syntax might be a little confusing, but I’m in the process of fleshing out the documentation, so check out the source or feel free to contact me if you have any questions.

Latest podspec

{
    "name": "JCModel",
    "version": "0.2",
    "license": "MIT",
    "summary": "A small framework to help with writing model layers in iOS.",
    "homepage": "https://github.com/jconst/JCModel",
    "authors": {
        "Joseph Constantakis": "[email protected]"
    },
    "platforms": {
        "ios": "6.0"
    },
    "source": {
        "git": "https://github.com/jconst/JCModel.git",
        "tag": "0.2"
    },
    "requires_arc": true,
    "dependencies": {
        "NSObjectProperties": [
            "~> 0.0.3"
        ]
    },
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Classes/Core/*.{h,m}"
        },
        {
            "name": "AFNetworking",
            "source_files": "Classes/AFNetworking/*.{h,m}",
            "dependencies": {
                "AFNetworking": [
                    "~> 2.0"
                ],
                "JCModel/Core": []
            }
        }
    ]
}

Pin It on Pinterest

Share This