RFJModel, an IOS class library that can automatically load JSON dictionaries into OBJC objects

Time:2019-9-12

RFJModel is an IOS class library that can automatically load JSON dictionaries into OBJC objects. Compared with JSON Model, JSON Model has some very good features and is relatively simple to use.

RFJModel has the following characteristics

1. Determine the loading behavior at the time of declaration.

RFJModel uses JProperty macros to declare whether this property is used for JSON loading, type of loading, and KEY in JSON.

The following example declares a value_NSString attribute, which converts the map_value_NSString field in the dictionary to NSString and sets it to the attribute 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. RFJModel supports JProperty and @property hybrid declarations, and does not affect each other.

In the following example, only the value_NSString attribute is automatically loaded, and the tag attribute is not managed by RFJModel.

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

3. One of the purposes of RFJModel design is to minimize the problem of IOS client crash caused by misleading service-side interface definition or return. So the following features are introduced

  • All [NSNull null] objects are converted appropriately and are not assigned to JProperty properties. (due to lack of access to [NSNull]
    Null] Protection-induced crashes are the main cause of JSON parsing crashes.
  • When assigning, the JSON value is converted according to the attribute type declared by JProperty. For example, Number assignment in JSON can be automatically converted to NSString.
  • The redundant or missing fields in the JSON dictionary do not report errors.

4. RFJModel supports inheritance

@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. The types supported by JProperty include subclasses of RFJModel.

@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. The types supported by JProperty include arrays of subclasses of RFJModel.

@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. JProperty supports NSMutable String, NSMutable Array, and NSMutable Dictionary variable types. At the same time, the nested containers of NSMutable Array and NSMutable Dictionary will be converted to variable types as far as possible.

8. JProperty supports only the following type declarations. If the following types are declared, an exception will be thrown in the first 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>

RFJModel Code Address
https://github.com/refusebt/RFJModel

Recommended Today

Hadoop MapReduce Spark Configuration Item

Scope of application The configuration items covered in this article are mainly for Hadoop 2.x and Spark 2.x. MapReduce Official documents https://hadoop.apache.org/doc…Lower left corner: mapred-default.xml Examples of configuration items name value description mapreduce.job.reduce.slowstart.completedmaps 0.05 Resource requests for Reduce Task will not be made until the percentage of Map Task completed reaches that value. mapreduce.output.fileoutputformat.compress false […]