APJSONMapping 2.0.1

APJSONMapping 2.0.1

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Jan 2016

Maintained by Alexander Perechnev.



  • By
  • Alex Krzyżanowski

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