zoukankan      html  css  js  c++  java
  • iGson

    头文件

    #import <Foundation/Foundation.h>
    #import <objc/runtime.h>
    #import "NSString+Utils.h"
    
    @interface iGson : NSObject
    
    // 获取所有的property
    - (NSMutableArray *)getPropertyNames;
    
    // 获取成员属性及属性的类型等信息的列表
    - (NSMutableArray *)getPropertyAttrs;
    
    // 获取非ReadOnly的所有property
    - (NSMutableArray *)getNotReadOnlyPropertyNames;
    
    // 获取一个该类的实例需要多少字节的内存开销
    + (size_t)getInstanceMemoryCost;
    
    // 使用JSONKit做JSON解析
    // 用于JSON解析的辅助方法,方便对象赋值
    + (id)objFromDict:(NSDictionary *)dict;
    + (id)objFromJson:(NSString *)json;
    
    // 转换成json字符串
    - (NSString *)objToJson;
    - (NSDictionary *)ojbToDict;
    
    // 用于各个子类测试json解析
    - (void)testJsonParse;
    
    @end

    实现文件

    #import "iGson.h"
    #import "JSONKit.h"
    
    
    @interface NSString (Utils)
    - (BOOL)hasSubString:(NSString *)string;
    @end
    
    @implementation NSString (Utils)
    - (BOOL)hasSubString:(NSString *)string
    {
        NSRange range = [self rangeOfString:string];
        if(range.location == NSNotFound)
            return FALSE;
        return TRUE;
    }
    @end
    
    @interface iGson ()
    + (BOOL)isPropertyReadOnly:(objc_property_t)property;
    @end
    
    
    @implementation iGson
    
    + (BOOL)isPropertyReadOnly:(objc_property_t)property
    {
        NSString *propertyAttrString =  [NSString stringWithCString:property_getAttributes(property)
                                                           encoding:NSUTF8StringEncoding];
        
        
        if([propertyAttrString hasSubString: @",R,"]){
            return YES;
        }
        
        return FALSE;
    }
    
    
    - (NSMutableArray *)getPropertyAttrs
    {
        Class cls = [self class];
        NSMutableArray *propertyAttrsList = [[NSMutableArray alloc] init];
        
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];{
            
            unsigned int outCount = 0, i = 0;
            objc_property_t *properties = class_copyPropertyList(cls, &outCount);
            
            for( i = 0; i < outCount; i++){
                objc_property_t property = properties[i];
                //printf("%s
    " ,property_getAttributes(property));
                
                NSString *propertyAttrString =  [NSString stringWithCString:property_getAttributes(property)
                                                                   encoding:NSUTF8StringEncoding];
                //NSLog(@"propertyAttr: %@", propertyAttrString);
                
                // 字符串非空,则加入属性列表
                if(!(propertyAttrString == nil ||
                     propertyAttrString.length == 0 ||
                     [propertyAttrString isEqualToString:@""]))
                {
                    [propertyAttrsList addObject:propertyAttrString];
                }
            }
            
            [pool drain];
        }
        return [propertyAttrsList autorelease];
    }
    
    
    - (NSMutableArray *)getPropertyNames
    {
        Class cls = [self class];
        NSMutableArray *propertyList = [[NSMutableArray alloc] init];
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];{
            
            unsigned int outCount = 0, i = 0;
            objc_property_t *properties = class_copyPropertyList(cls, &outCount);
            
            for( i = 0; i < outCount; i++){
                
                objc_property_t property = properties[i];
                //printf("%s
    ", property_getName(property));
                
                
                
                NSString *propertyString =  [NSString stringWithCString:property_getName(property)
                                                               encoding:NSUTF8StringEncoding];
                //NSLog(@"property: %@", propertyString);
                // 字符串非空,则加入属性列表
                if(!(propertyString == nil ||
                     propertyString.length == 0 ||
                     [propertyString isEqualToString:@""]))
                {
                    [propertyList addObject:propertyString];
                }
            }
            
            [pool drain];
        }
        return [propertyList autorelease];
    }
    
    - (NSMutableArray *)getNotReadOnlyPropertyNames
    {
        Class cls = [self class];
        NSMutableArray *propertyList = [[NSMutableArray alloc] init];
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];{
            
            unsigned int outCount = 0, i = 0;
            objc_property_t *properties = class_copyPropertyList(cls, &outCount);
            
            for( i = 0; i < outCount; i++){
                
                objc_property_t property = properties[i];
                //printf("%s
    ", property_getName(property));
                
                if([Model isPropertyReadOnly:property]) continue;
                
                NSString *propertyString =  [NSString stringWithCString:property_getName(property)
                                                               encoding:NSUTF8StringEncoding];
                //NSLog(@"property: %@", propertyString);
                // 字符串非空,则加入属性列表
                if(!(propertyString == nil ||
                     propertyString.length == 0 ||
                     [propertyString isEqualToString:@""]))
                {
                    [propertyList addObject:propertyString];
                }
            }
            
            [pool drain];
        }
        return [propertyList autorelease];
        
    }
    
    + (size_t)getInstanceMemoryCost
    {
        return class_getInstanceSize([self class]);
    }
    
    
    + (id)objFromDict:(NSDictionary *)dict
    {
        id obj = [[[self class] alloc] init];
        
        NSMutableArray *properties = [obj getNotReadOnlyPropertyNames];
        for(NSString *property in properties){
            id objValue = [dict objectForKey:property];
            if([objValue isKindOfClass:[NSDictionary class]]){
                objValue = [[self class] objFromDict:objValue];
            }else if ([objValue isKindOfClass:[NSArray class]]){
                for(id itemObj in ((NSArray *)objValue)){
                    itemObj = [[self class] objFromDict:itemObj]; // 这句是否合理,需要测试下
                }
            }
            //判断非空再赋值
            if(objValue && (![objValue isKindOfClass:[NSNull class]])){
                [obj setValue:objValue forKey:property];
            }
            
        }
        
    #if __has_feature(objc_arc)
        return obj;
    #else
        return [obj autorelease];
    #endif
        
    }
    
    + (id)objFromJson:(NSString *)json
    {
        id dict = [json objectFromJSONString];
        return [[self class] objFromDict:dict];
    }
    
    - (NSDictionary *)objToDict
    {
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        NSMutableArray *properties = [self getNotReadOnlyPropertyNames];
        for(NSString *property in properties){
            id obj = [self valueForKey:property];
            [dict setValue:obj forKey:property];
        }
        
        return dict;
    }
    
    
    - (NSString *)objToJson
    {
        NSString *json = nil;
        json = [[self objToDict] JSONString];
        return json;
    }
    
    - (NSString *)description
    {
        NSMutableString *s = [NSMutableString stringWithCapacity:0];
        
        [s appendFormat:@"
    Class: %@{
    ", NSStringFromClass([self class])];
        NSArray *arr = [self getNotReadOnlyPropertyNames];
        for(id o in arr){
            [s appendFormat:@"%@: %@
    ", o, [self valueForKey:o]];
        }
        [s appendFormat:@"}
    "];
        return s;
    }
    
    
    - (void)setNilValueForKey:(NSString *)key
    {
        // 暂时不做处理
    }
    
    - (void)testJsonParse
    {
        // 子类重载实现
    }
    
    @end


  • 相关阅读:
    python注释方法以及编码问题
    python数据类型和变量
    JavaScript必须了解的知识点总结。
    javaScript语法总结
    美图WEB开放平台环境配置
    变点问题的统计推新及其在全融中的应用 谭常春
    Structural breaks in time series
    多种单位根检验法的比较研究 房林邹卫星
    1-出口数据的平稳性分析
    时间序列中的结构突变与单位根检验
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3503347.html
Copyright © 2011-2022 走看看