iOS-Developer-OCiOS基础·OC高级篇iOS-OC中级

对象转有序JSON

2020-01-19  本文已影响0人  山已几孑

场景: 参数加签名!

参数都是对象,里面则是什么都有,对象,字典,数组,乱八七糟的,查了半天也没有直接把对象或者字典转成有序JSON的轮子,也没找到直接操作JSON字符串的有效工具(PS:安卓和我说,就一个方法的事儿),其实功能并不难,无非是中间加了一层排序,无所谓了,没有就自己写个呗。

通过class_copyPropertyList获取属性列表,排序,根据Value状态解析, 拼到JSON串里面,Value为nil的时候根据property_getAttributes判断类型,拼上不同的字符串,没了。

十分简陋,先凑合招吧,有时间改改再,或者直接从MJExtension上面改一个出来?😅

#import <objc/runtime.h>

@implementation KYSequenceJSON

/**
 获取JSON字符串

 @param obj <#obj description#>
 @return <#return value description#>
 */
+ (NSString *)sequenceJSON:(id)obj {
    return [self getStringInternal:obj];
}

/**
 解析attributes的string,返回类型

 @param attributes Ti,N,V_num
 @return Class
 */
+ (Class)attributeClass:(NSString *)attributes {
    
    NSString * typeString = [attributes componentsSeparatedByString:@","].firstObject;
    if ([typeString hasPrefix:@"T@\""] || [typeString hasSuffix:@"\""]) {
        NSString * type = [typeString substringWithRange:NSMakeRange(3, typeString.length - 4)];
        return NSClassFromString(type);
    }
    return nil;
}
/**
 获取
 
 @param obj 对象,到这里的都是object
 @return JSON string
 */
+ (NSString *)jsonStringWithObject:(NSObject *)obj {
    
    unsigned int propsCount;
    //获得属性列表
    objc_property_t *props = class_copyPropertyList([obj class], &propsCount);
    
    NSMutableArray<NSString *> * propreties = [NSMutableArray arrayWithCapacity:propsCount];
    
    //获得被忽略的属性列表,被改变的键值对
    NSArray * ignorePropreties = @[];
    // 因为不确定是否实现了ignorePropreties\replacedPropreties,所以先判断下一下是否实现额
    if ([obj respondsToSelector:@selector(ignorePropreties)]) {
        ignorePropreties = [obj valueForKey:@"ignorePropreties"];
    }
    NSDictionary * replacedPropreties = @{};
    if ([obj respondsToSelector:@selector(replacedPropreties)]) {
        replacedPropreties = [obj valueForKey:@"replacedPropreties"];
    }
    for(int i = 0;i < propsCount; i++){
        //获取属性名
        objc_property_t prop = props[i];
        
        NSString *propName = [NSString stringWithUTF8String:property_getName(prop)];//获得属性的名称
        
        // 当应被忽略时,忽略
        if ([ignorePropreties containsObject:propName]) {
            continue;
        }
        // 当被替换了名称时,把名称放到待排序数组里面
        if ([replacedPropreties.allKeys containsObject:propName]) {
            [propreties addObject:replacedPropreties[propName]];
        } else {
            [propreties addObject:propName];
        }
    }
    //排序
    NSArray * sortedPropreties = [propreties sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSString * str1 = (NSString *)obj1;
        NSString * str2 = (NSString *)obj2;
        return [str1 compare:str2 options:(NSCaseInsensitiveSearch)];
    }];
    // JSON String
    NSMutableString *mutableString = [NSMutableString string];
    // 拼接初始的{
    [mutableString appendString:@"{"];
    // 遍历排序后的属性数组,分别添加格式
    for(int i = 0;i < sortedPropreties.count; i++){
        NSString *propName = sortedPropreties[i];
        // 如果是已替换名称的,找到原始属性名
        NSString * propretyName = ![replacedPropreties.allValues containsObject:propName] ? propName : [replacedPropreties allKeysForObject:propName].firstObject;
        //kvc读值
        id value = [obj valueForKey:propretyName];
        
        if (value == nil) {
            // 当值为空时,无法根据值区分不同属性的类型,因此需要找到属性的原始类型,f
            objc_property_t prop = class_getProperty([obj class], [propretyName UTF8String]);
            NSString* attrs = @(property_getAttributes(prop));
            Class ccc = [self attributeClass:attrs];
            // 当值为空的时候,需要区分类型,给不同的值,如果忽略,那么按照下面基础类型的写
            if ([ccc isKindOfClass:[NSDictionary class]]) {
                value =  @"{}";
            }
            if ([ccc isKindOfClass:[NSArray class]]) {
                value =  @"()";
            }
            if ([ccc isKindOfClass: [NSObject class]]) {
                value =  @"{}";
            }
            //MAKR: 这里把字符串,url,数字,和Int,FFloat,double等基础类型为空,都给忽略掉了
            if (ccc == [NSString class]) { //NSString 和NSObject 是继承关系
                // 忽略s字符串为空的, 其实上面已经把String过滤一遍了
                //                value =  @"\"\"";
                continue;
            }
            if ([ccc isKindOfClass:[NSURL class]] ) {
                // URL 当做String处理
                //                value =  @"\"\"";
                continue;
            }
            if ([ccc isKindOfClass: [NSNumber class]]) {
                continue;
            }
            if (ccc == nil) { // 基础类型,忽略
                continue;
            }
        }else{
            value = [self getStringInternal:value];//自定义处理数组,字典,其他类
        }
        if (mutableString.length > 1) {
            [mutableString appendFormat:@","];
        }
        [mutableString appendFormat:@"\"%@\":%@",propName, value];
        
    }
    [mutableString appendString:@"}"];
    
    free(props);
    return mutableString;
}

+ (NSString *)getStringInternal:(id)obj{
    
    //字符串,直接返回
    if ([obj isKindOfClass: [NSString class]]) {
        return [NSString stringWithFormat:@"\"%@\"", obj];
    }
    if( [obj isKindOfClass: [NSNumber class]]) {
        NSLog(@"%@",obj);
        return obj;
    }
    if([obj isKindOfClass: [NSNull class]]) {
        return @"\"\"";
    }
    // NSUrl 返回字符串
    if ([obj isKindOfClass: [NSURL class]]) { //NSString 和NSObject 是继承关系
        return [NSString stringWithFormat:@"\"%@\"", [obj absoluteString]];
    }
    // 数组,需要逐条返回
    if([obj isKindOfClass:[NSArray class]]) {
        NSArray *objarr = obj;
        NSMutableString * arrString = [NSMutableString stringWithString:@"["];
        for(int i = 0;i < objarr.count; i++) {
            NSString * valueString = [self getStringInternal:[objarr objectAtIndex:i]];
            [arrString appendString:valueString];
            if (i < objarr.count - 1) {
                [arrString appendString:@","];
            }
        }
        [arrString appendString:@"]"];
        return arrString;
    }
    if([obj isKindOfClass:[NSDictionary class]]) {
        NSDictionary *objdic = obj;
        NSArray * sortedKeys = [objdic.allKeys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSString * str1 = (NSString *)obj1;
            NSString * str2 = (NSString *)obj2;
            return [str1 compare:str2 options:(NSCaseInsensitiveSearch)];
        }];
        
        NSMutableString * dictString = [NSMutableString stringWithString:@"{"];
        for (int i = 0; i < sortedKeys.count; i ++) {
            
            NSString * key = sortedKeys[i];
            
            NSString * valueStr = [self getStringInternal:[objdic objectForKey:key]];
            
            [dictString appendFormat:@"\"%@\":%@",key, valueStr];
            
            if (i < sortedKeys.count - 1) {
                [dictString appendString:@","];
            }
        }
        [dictString appendFormat:@"}"];
        return dictString;
    }
    
    return [self jsonStringWithObject:obj];
}


上一篇下一篇

猜你喜欢

热点阅读