RDHJSONObjectSerialisation 0.5.0

RDHJSONObjectSerialisation 0.5.0

测试已测试
语言语言 Obj-CObjective C
许可协议 MIT
发布最后发布2014年12月

Rich Hodgkins维护。



  • Rich Hodgkins

任何基于其声明的属性的NSObject的简单JSON序列化。

此readme是一个正在进行的工作,正在更新(2013年5月8日)。

示例

按以下方式定义自定义对象:具有自定义序列化和反序列化程序的customDateProp属性,输出生成的日期格式为yyyy-MM-dd

@interface TestObject : NSObject<RDHJSONObjectSerialisationProtocol>

@property (nonatomic, strong) NSString *strProp;
@property (nonatomic, strong) NSDecimalNumber *decNumProp;
@property (nonatomic, strong) NSNumber *numProp;

@property (nonatomic) short shortProp;
@property (nonatomic) int intProp;
@property (nonatomic) long longProp;

@property (nonatomic) BOOL boolPropYES;
@property (nonatomic) BOOL boolPropNO;

@property (nonatomic, strong) NSArray *arrayStringProp;
@property (nonatomic, strong) NSArray *arrayNumberProp;

@property (nonatomic, strong) TestObject *objectProp;

@property (nonatomic, strong) NSDate *dateProp;

@property (nonatomic, strong) NSData *dataProp;

@property (nonatomic, strong) NSDate *customDateProp;

@property (nonatomic, strong) NSString *nilStringProp;

@end

@interface AnotherTestObject : NSObject<RDHJSONObjectSerialisationProtocol>

@property (nonatomic, strong) NSString *anotherStrProp;
@property (nonatomic, strong) NSDecimalNumber *anotherDecNumProp;
@property (nonatomic, strong) NSNumber *anotherNumProp;

@end

序列化

用以下内容填充

TestObject->{
    arrayNumberProp =     (
        1,
        2,
        3,
        4
    );
    arrayStringProp =     (
        A1,
        A2,
        A3
    );
    boolPropNO = 0;
    boolPropYES = 1;
    customDateProp = "2018-02-21 02:21:36 +0000";
    dataProp = <42415345 20363420 454e434f 44454420 53545249 4e47>;
    dateProp = "2044-03-23 10:39:02 +0000";
    decNumProp = "23434234.2342342343232432";
    intProp = "-2343";
    longProp = "-234324324234324";
    nilStringProp = nil;
    numProp = "2324.003";
    objectProp = "AnotherTestObject->{
        anotherDecNumProp = "486768768.876488867867";
        anotherNumProp = 3547;
        anotherStrProp = "INNER_PROP";
    }";
    shortProp = "-133";
    strProp = "STR_PROP";
}

使用RDHJSONWritingOptionsNoOptions通过+[RDHJSONObjectSerialisation JSONForObject:options:error:]执行它生成以下JSON

{
    "arrayNumberProp": [
        1,
        2,
        3,
        4
    ],
    "arrayStringProp": [
        "A1",
        "A2",
        "A3"
    ],
    "boolPropNO": 0,
    "boolPropYES": 1,
    "customDateProp": "2018-02-21",
    "dataProp": "QkFTRSA2NCBFTkNPREVEIFNUUklORw==",
    "dateProp": "2044-03-23T10:39:02+0000",
    "decNumProp": "2.343423E+007",
    "intProp": -2343,
    "numProp": "2.324003E+003",
    "longProp": -234324324234324,
    "objectProp": {
        "anotherDecNumProp": "4.867688E+008",
        "anotherStrProp": "INNER_PROP",
        "anotherNumProp": "3.547000E+003"
    },
    "shortProp": -133,
    "strProp": "STR_PROP"
}

传递RDHJSONWritingOptionsConvertNilsToNSNulls将添加额外的字段

    "nilStringProp": null

传递RDHJSONWritingOptionsConvertDatesToUnixTimestamps将更改dateProp

    "dateProp": 2342342342