CYJSONValidator 0.1

CYJSONValidator 0.1

TestsTested
LangLanguage Obj-CObjective C
License Custom
ReleasedLast Release May 2015

Maintained by Li Fuqiang.



  • By
  • Jason li

A simple and safe wrapper for RPJSONValidator on iOS JSON data parsing. In practice, it's very verbose on handling JSON data. We wanna both safty and flexibility.This Project can make this. What's more, I make it very easy to use.

Example

NSDictionary *json = @{
                           @"tag1": @"star",
                           @"tag2": @"jason",
                           @"totalNum": @(1616),
                           @"start_index": @"60",
                           @"return_number": @(30),
                           @"data" : @[],
                           @"tags" : [NSNull null],
                           @"config" : @{
                                   @"max_num" : @(30000),
                                   @"tag" : [NSNull null]
                                   }
                           };

For example, wo wanna get the value of "max_num" in "config" of json;

Normal way

NSDictionary *configDic1 = [json objectForKey:@"config"];
    if (configDic1 != nil && [configDic1 isKindOfClass:[NSDictionary class]]) {
        id number = [configDic1 objectForKey:@"max_num"];
        if ([number isKindOfClass:[NSNumber class]] || [number isKindOfClass:[NSString class]]) {
            NSInteger maxNum = [number integerValue];
            NSLog(@"maxNum 1: %@", @(maxNum));
        }
    }

CYJSONValidator way

In Cbjective-c, id doesn't support dot notation. This is very very upset for me. So I changed return value to actual type to support chainable syntax gracefully.

    NSInteger maxNum3 = [[json cy_dictionaryKey](@"config") cy_integerKey](@"max_num");
    NSLog(@"maxNum 3: %@", @(maxNum3));

Or just this!

    NSInteger maxNum = [[json cy_dictionaryKey:@"config"] cy_integerKey:@"max_num"];
    NSLog(@"maxNum: %@", @(maxNum));

error handle

    // Handle NSNull
    NSArray *tags = [json cy_arrayKey:@"tags"];
    NSLog(@"%@", tags);
    // Handle wrong type
    NSString *string = [[json cy_dictionaryKey:@"data"] cy_stringKey:@"1"];
    NSLog(@"%@", string);