Click here to Skip to main content
15,867,453 members
Articles / Mobile Apps / iOS
Tip/Trick

RFJModel, an easy-to-use JSON loading library

Rate me:
Please Sign up or sign in to vote.
4.69/5 (3 votes)
8 Jan 2015CPOL1 min read 8.3K   1  
RFJModel is an easy-to-use JSON loading library. In comparing with other libraries, it's much easiler and less restricted to use.

RFJModel is an easy-to-use JSON loading library. In comparing with other libraries, it's much easiler and less restricted to use. RFJModel has the following characteristics.

1. Defining loading rules in the class declaration.

RFJModel uses a macro, called JProperty, to declare loading rules. JProperty declares loading properties, converted types and mapping keys in JSON. The following example is to declare a property called "value_NSString". When JSON is loading, RFJModel gets the value from field "map_value_NSString" in JSON, converts the field type to NSString, and sets the value to the property "value_NSString".

@interface ExampleJModel : RFJModel
JProperty(NSString *value_NSString, map_value_NSString);
@end

@implementation ExampleJModel
@end

{
  "map_value_NSString":"hello world",
}

NSDictionary *json = ...;
ExampleJModel *model = [[ExampleJModel alloc] initWithJsonDict:json];
NSLog(@"%@", model.value_NSString);

2. Supporting JProperty、@property mixing declaration without influencing each other.

In the following example, only "value_NSString" will be loaded, not "tag".

@interface ExampleJModel : RFJModel
JProperty(NSString *value_NSString, map_value_NSString);
@property (nonatomic, assign) int64_t tag;
@end

3. Reducing crashes from errors returned by the servers

  • All [NSNull null] objects will be converted appropriately, and would not be set to JProperty. (Lacking protections to [NSNull null] is a main cause of the crashes.)
  • When setting the values, the value of JSON will be converted basing on the type of JProperty. For example, the Number in JSON will be converted into NSString in JProperty.
  • Extra or missing fields in a JSON dictionary would not be considered as an error.

4. Supporting class inherits.

@interface ExampleJModel : RFJModel
JProperty(NSString *value_NSString, map_value_NSString);
@property (nonatomic, assign) int64_t tag;
@end

@interface ExampleJSubModel : ExampleJModel
JProperty(NSString *name, name);
@end

{
  "map_value_NSString":"hello world",
  "name":"Tom",
}

NSDictionary *json = ...;
ExampleJModel *model = [[ExampleJModel alloc] initWithJsonDict:json];
NSLog(@"%@", model.value_NSString);    // "hello world"

NSDictionary *json = ...;
ExampleJSubModel *model = [[ExampleJSubModel alloc] initWithJsonDict:json];
NSLog(@"%@", model.value_NSString);    // "hello world"
NSLog(@"%@", model.name);    // "Tom"

5. Supporting RFJModel subclasses in JProperty

@interface ExampleJModel : RFJModel
JProperty(NSString *value_NSString, map_value_NSString);
@property (nonatomic, assign) int64_t tag;
JProperty(ExampleJUserInfo *userInfo, UserInfo);
@end

@interface ExampleJUserInfo : RFJModel
JProperty(NSString *name, name);
@end

{
  "map_value_NSString":"hello world",
  "UserInfo":
    {
      "name":"Tom",
    },
}

6. Supporting arrays containing instances of RFJModel subclasses in JProperty

@protocol ExampleJUserInfo
@end

@interface ExampleJModel : RFJModel
JProperty(NSString *value_NSString, map_value_NSString);
@property (nonatomic, assign) int64_t tag;
JProperty(NSArray<ExampleJUserInfo> *userInfos, UserInfos);
@end

@interface ExampleJUserInfo : RFJModel
JProperty(NSString *name, name);
@end
{
    "map_value_NSString":"hello world",
    "UserInfos":[
        {
            "name":"Tom",
        },
        {
            "name":"Alice",
        },
    ],
}

 

7. Supporting NSMutableString, NSMutableArray and NSMutableDictionary in JProperty and Converting nested containers to mutable containers.

8. Supporting given types in JProperty only, and an execption will be thrown if any other type is in use

  • BOOL
  • Number(NSInteger, short, long long, double, etc)
  • NSString
  • NSMutableString
  • NSArray
  • NSMutableArray
  • NSDictionary
  • NSMutableDictionary
  • RFJModel's subclass
  • NSArray (RFJModel's subclass)
  • NSMutableArray (RFJModel's subclass)

 

Github: https://github.com/refusebt/RFJModel

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --