GitHub - buranmert/MBJSONMapper: Light-weight JSON<>Object mapper

2 min read Original article ↗

MBJSONMapper

Join the chat at https://gitter.im/MBJSONMapper/Lobby

CI Status Version License Platform

Why yet another JSON<>Object library???

I looked at other libraries but none of them seemed simple/minimal enough to me. The whole idea of this library is being as small as possible:

  1. 2 public headers
    1. 1 protocol
    2. 1 adapter
  2. Total line count of .m files: 186

Usage

Basic usage

JSON

{
    "middleName" = "Hatice"
    "name" = "John"
    "surname" = "Appleseed"
}

TestDataModel.h

#import <MBJSONMapper/MBJSONSerializable.h>

@interface TestDataModel : NSObject <MBJSONSerializable>

@property (nonatomic, copy, readonly) NSString *name;
@property (nonatomic, copy, readonly) NSString *middleName;
@property (nonatomic, copy, readonly) NSString *surname;

@end

TestDataModel.m

@implementation TestDataModel
// Nothing!
@end

Advanced usage

JSON

{
    "isItTrue" = 1
    "middleName" = "Hatice"
    "name" = "John"
    "surname" = "Appleseed"
    "nestedModel" =     {
        "middleName" = ""
        "name" = "Nested"
        "surname" = "John"
    }
    "nestedModels" =     (
                {
            "middleName" = "1"
            "name" = "John"
            "surname" = "Dupont"
        },
                {
            "middleName" = "2"
            "name" = "John"
            "surname" = "Dupont"
        },
                {
            "middleName" = "3"
            "name" = "John"
            "surname" = "Dupont"
        }
    )
}

TestDataModel.h

#import <MBJSONMapper/MBJSONSerializable.h>

@interface TestDataModel : NSObject <MBJSONSerializable>

@property (nonatomic, readonly) BOOL isItTrue;

@property (nonatomic, copy, readonly) NSString *name;
@property (nonatomic, copy, readonly) NSString *surname;
@property (nonatomic, copy, readonly) NSString *secondName;

@property (nonatomic, copy, readonly) NSString *nestedModelName;

@property (nonatomic, copy, readonly) TestDataModel *nestedModel;
@property (nonatomic, copy, readonly) NSArray<TestDataModel*> *nestedModels;

@end

TestDataModel.m

@implementation TestDataModel

- (NSDictionary<NSString*, NSString*> *)keyPropertyMappingDictionary {
    return @{@"nestedModel.name": NSStringFromSelector(@selector(nestedModelName)),
             @"middleName":  NSStringFromSelector(@selector(secondName))};
}

- (NSDictionary<NSString*, Class> *)keyClassMappingDictionary {
    return @{NSStringFromSelector(@selector(nestedModel)): [TestDataModel class],
             NSStringFromSelector(@selector(nestedModels)): [TestDataModel class]};
}

@end

That's all folks!

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

MBJSONMapper is available through CocoaPods. To install it, simply add the following line to your Podfile:

Author

Mert Buran, buranmert@gmail.com

License

MBJSONMapper is available under the MIT license. See the LICENSE file for more info.