CocoaPods trunk is moving to be read-only. Read more on the blog, there are 12 months to go.

MBJSONMapper 0.2.7

MBJSONMapper 0.2.7

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

Maintained by Mert Buran.



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:

pod "MBJSONMapper"

Author

Mert Buran, [email protected]

License

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