将Model对象转换成json文本或者json二进制文件

将Model对象转换成json文本或者json二进制文件

https://github.com/casatwy/AnyJson

注意:经过测试,不能够直接处理字典或者数组

主要源码的注释

AJTransformer.h 与 AJTransformer.m

//
//  AJTransformer.h
//  AnyJson
//
//  Created by casa on 14-9-19.
//  Copyright (c) 2014年 casa. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol AJSerializable;

@interface AJSerializer : NSObject

/**
 *  将对象转换成json二进制文件
 *
 *  @param object 对象
 *
 *  @return json二进制文件
 */
+ (NSData *)jsonDataWithObject:(id)object;

/**
 *  将对象转换成json字符串
 *
 *  @param object 对象
 *
 *  @return json字符串
 */
+ (NSString *)jsonStringWithObject:(id)object;

+ (id)objectWithJsonData:(NSData *)jsonData targetObjectClass:(Class)targetObjectClass;
+ (id)objectWithJsonString:(NSString *)jsonString targetObjectClass:(Class)targetObjectClass;

@end


//
//  AJTransformer.m
//  AnyJson
//
//  Created by casa on 14-9-19.
//  Copyright (c) 2014年 casa. All rights reserved.
//

#import "AJSerializer.h"
#import "AJObject2JsonSerializer.h"
#import "AJJson2ObjectSerializer.h"

@implementation AJSerializer

#pragma mark - public method
+ (NSData *)jsonDataWithObject:(id)object
{
    id basicObject = [AJObject2JsonSerializer serializeToBasicObject:object];
    return [NSJSONSerialization dataWithJSONObject:basicObject options:0 error:nil];
}

+ (NSString *)jsonStringWithObject:(id)object
{
    NSString *jsonString = [[NSString alloc] initWithData:[AJSerializer jsonDataWithObject:object] encoding:NSUTF8StringEncoding];
    return jsonString;
}

+ (id)objectWithJsonData:(NSData *)jsonData targetObjectClass:(Class)targetObjectClass
{
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
    return [AJJson2ObjectSerializer transformJsonObject:jsonObject toTargetObjectClass:targetObjectClass];
}

+ (id)objectWithJsonString:(NSString *)jsonString targetObjectClass:(Class)targetObjectClass
{
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    return [AJSerializer objectWithJsonData:jsonData targetObjectClass:targetObjectClass];
}

@end

Model类:
//
//  Playground.h
//  AnyJson
//
//  Created by casa on 14-9-20.
//  Copyright (c) 2014年 casa. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "OtherObject.h"

@interface Playground : NSObject

@property (nonatomic, assign) NSInteger testInteger;
@property (nonatomic, strong) OtherObject *otherObject;

@end


//
//  OtherObject.h
//  AnyJson
//
//  Created by casa on 14-9-21.
//  Copyright (c) 2014年 casa. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface OtherObject : NSObject

@property (nonatomic, assign) NSInteger  testInteger;
@property (nonatomic, assign) BOOL       isTest;
@property (nonatomic, strong) NSString  *name;
@property (nonatomic, assign) char       testChar;

@end

使用的源码:

ViewController.m

//
//  ViewController.m
//  AnyJson
//
//  Created by casa on 14-9-19.
//  Copyright (c) 2014年 casa. All rights reserved.
//

#import "ViewController.h"
#import "AnyJson.h"
#import "Playground.h"
#import "AJClassHelper.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    Playground *playground   = [[Playground alloc] init];
    OtherObject *otherObject = [[OtherObject alloc] init];
    otherObject.testInteger  = 13;
    otherObject.isTest       = YES;
    otherObject.name         = @"casa";
    otherObject.testChar     = 'c';

    playground.otherObject   = otherObject;
    NSString *jsonString     = [AJSerializer jsonStringWithObject:playground];
    
    NSLog(@"json string is %@", jsonString);
}

@end

打印的信息:

2015-01-04 20:52:18.399 AnyJson[3825:149779] json string is {"testInteger":0,"otherObject":{"isTest":true,"name":"casa","testInteger":13,"testChar":"c"}}

 

几个需要注意的地方:

-此处是属性中包含了属性-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值