Available thru Cocoapods
Latest 1.0
Homepage https://github.com/aperechnev/APJSONMapping
License MIT

Join the chat at https://gitter.im/alexkrzyzanowski/APJSONMapping
Build Status
CocoaPods
CocoaPods
codecov.io

An Objective-C class extension which allows you to easily map your objects to JSON string and parse your objects back from JSON.

Installation

The easiest way to get APJSONMapping is to install it via CocoaPods:

target 'MyApp' do
  pod 'APJSONMapping', '~> 1.0'
end

When the framework installed, just import it to add appropriate functionality to your existing classes:

@import APJSONMapping;

@interface MyCustomClass : NSObject
// ...
@end

Usage Example

To make your object able to be mapped to (and parsed from) JSON, you have to describe it’s mapping rules:

@import Foundation;
@import APJSONMapping;

//
// Here is interface
@interface MyCustomClass : NSObject

@property (nonatomic, strong) NSNumber *someNumber;
@property (nonatomic, strong) NSString *someString;

+ (Class)someArrayOfRelatingObjectsType;
@end

//
// And here is implementation
@implementation MyCustomClass

+ (NSMutableDictionary *)ap_objectMapping {
  NSMutableDictionary * mapping = [super ap_objectMapping];
  if (mapping) {
    NSDictionary * objectMapping = @{ @"someNumber": @"some_number",
                                      @"someString": @"some_string"};
    [mapping addEntriesFromDictionary:objectMapping];
  }
  return mapping;
}

@end

Since you’ve described the mapping, you can map your object to JSON and parse it back:

MyCustomClass *myObject = [[MyCustomClass alloc] init];
myObject.someNumber = @112;
myObject.someString = @"testString";

NSString *json = [myObject ap_mapToJSONString]; // { "some_number": 112, "some_string": "testString" }
MyCustomClass *anotherObject = [[MyCustomClass alloc] initWithJSONString_ap:json];

Code Coverage

codecov.io

Latest podspec

{
    "name": "APJSONMapping",
    "version": "1.0",
    "summary": "Objective-C class category which allows you to easily map your objects to dictionaries and vice versa.",
    "description": "Objective-C class extension which allows you to easily map your objects to dictionaries and parse your objects from dictionaries.",
    "homepage": "https://github.com/aperechnev/APJSONMapping",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Alexander Perechnev": "[email protected]"
    },
    "source": {
        "git": "https://github.com/aperechnev/APJSONMapping.git",
        "tag": "1.0"
    },
    "platforms": {
        "ios": "8.4"
    },
    "source_files": "APJSONMapping/APJSONMapping/*.{h,m}",
    "requires_arc": true
}

Pin It on Pinterest

Share This