XLYMapping 1.0.0

XLYMapping 1.0.0

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最新版本2015年1月

kaizeiyimi维护。



  • 作者
  • kaizei

XLYMapping 系统。

XLYMapping 设计用于将 JSON 映射到本地对象。目标对象可以是继承自 NSObject 的对象,因为我使用 KVC 来设置值。

更多详细信息可以在下面的示例和测试代码中找到。

注意:映射过程如下

  1. 有机会修改 JSON。
  2. 动态映射。有机会给出另一种映射。
  3. 创建目标对象。如果失败则取消映射。
  4. 转换属性。关系映射或构建块可以替代默认转换。
  5. 验证转换后的值。如果失败则取消映射。

验证过程兼容某些指定的类型:数字、字符串、集合、有序集合和 URL。请阅读代码获取更多详细信息。

正常 NSObject 映射

'TestCat' 和 'TestDog' 类定义

    //definition of TestAnimal class
    @interface TestAnimal : NSObject

    @property (nonatomic, copy) NSString *name;
    @property (nonatomic, assign) NSInteger age;

    @end

    //definition of TestCat class
    @interface TestCat : TestAnimal

    @property (nonatomic, strong) UIColor *eyeColor;

    @end

    //definition of TestDog class
    @interface TestDog : TestAnimal

    @property (nonatomic, copy) NSURL *aboutLink;

    @end

Animals.json

//this is a example. our JSON is:
{"dogs":[
  {
    "name":"DogA",
    "age":5,
    "about link":"http://link.to.DogA"
  },
  {
    "name":"DogB",
    "age":6,
    "about link":"http://link.to.DogB"
  }],
  "cats":[
    {
      "name":"CatC",
      "age":3,
      "eye color":"70,70,70,1"
    },
    {
      "name":"CatD",
      "age":4,
      "eye color":null
    }]
}

设置正常对象映射和转换

    //setup mapping.
    XLYObjectMapping *dogMapping = [XLYObjectMapping mappingForClass:[TestDog class]];
    [dogMapping addAttributeMappingFromArray:@[@"name", @"age"]];
    [dogMapping addAttributeMappingFromDict:@{@"about link":@"aboutLink"}];

    XLYObjectMapping *catMapping = [XLYObjectMapping mappingForClass:[TestCat class]];
    [catMapping addAttributeMappingFromArray:@[@"name", @"age"]];
    [catMapping addMappingFromKeyPath:@"eye color" toKey:@"eyeColor" construction:^id(id JSONObject) {
    NSArray *colorComponents = [JSONObject componentsSeparatedByString:@","];
    return [UIColor colorWithRed:[colorComponents[0] floatValue] / 255.0f
                           green:[colorComponents[1] floatValue] / 255.0f
                           blue:[colorComponents[2] floatValue] / 255.0f
                           alpha:[colorComponents[3] floatValue]];
    }];

    XLYMapping *mapping = [XLYObjectMapping mappingForClass:[NSDictionary class]];
    [mapping addRelationShipMapping:dogMapping fromKeyPath:@"dogs" toKey:@"dogs"];
    [mapping addRelationShipMapping:catMapping fromKeyPath:@"cats" toKey:@"cats"];

    //perform transform
    NSDictionary *result = [mapping performSyncMappingWithJSONObject:JSONObject error:&error];
    //you will get a dictionary which has two keys of "dogs" and "cats". see test codes for more detail. 

NSManagedObject 映射

托管对象 'TestPerson' 和 'TestCar' 定义

    //definition of TestPerson class
    @interface TestPerson : NSManagedObject

    @property (nonatomic, assign) int32_t identity;
    @property (nonatomic, copy) NSString *name;
    @property (nonatomic, assign) int32_t age;
    @property (nonatomic, strong) NSSet *cars;

    @end

    //definition of TestCar class
    @interface TestCar : NSManagedObject

    @property (nonatomic, copy) NSString *vendor;
    @property (nonatomic, assign) int64_t identity;
    @property (nonatomic, strong) TestPerson *person;

    @end

Persons.json

[
  {
    "name":"kaizei",
    "age":26,
    "id":1,
    "cars":[
      {
        "vendor":"lamborghini",
        "id":1001
      },
      {
        "vendor":"porsche",
        "id":1002
      }
    ]
  },
  {
    "name":"yimi",
    "age":25,
    "id":2,
    "cars":[
      {
        "vendor":"cadillac",
        "id":1003
      },
      {
        "vendor":"mercedes-benz",
        "id":1004
      }
    ]
  }
]

设置托管对象映射和转换

    //create mappings.
    XLYManagedObjectMapping *carMapping = [XLYManagedObjectMapping mappingForClass:TestCar.class
                                                                        entityName:@"Car"
                                                                       primaryKeys:@[@"identity"]
                                                              managedObjectContext:self.context];
    [carMapping addAttributeMappingFromDict:@{@"id":@"identity"}];
    [carMapping addAttributeMappingFromArray:@[@"vendor"]];

    XLYManagedObjectMapping *personMapping = [XLYManagedObjectMapping mappingForClass:TestPerson.class
                                                                           entityName:@"Person"
                                                                          primaryKeys:@[@"identity"]
                                                                 managedObjectContext:self.context];
    [personMapping addAttributeMappingFromDict:@{@"id":@"identity"}];
    [personMapping addAttributeMappingFromArray:@[@"name", @"age"]];

    [personMapping addRelationShipMapping:carMapping fromKeyPath:@"cars" toKey:@"cars"];

    //transform json
    NSArray *result = [self.mapping performSyncMappingWithJSONObject:self.JSONObject error:&error];
    //you will get an array of two persons which are ManagedObject in 'self.context'.

动态映射

如果您需要,可以进行动态映射。

    //here we reuse the people mapping and child mapping.
    XLYObjectMapping *theMapping = [XLYObjectMapping mappingForClass:nil];

    theMapping.dynamicMappingBlock = ^XLYObjectMapping *(id JSONObject) {
        if (JSONObject[@"child_name"]) {
            return childMapping;
        }
        return peopleMapping;
    };

设置默认值

您也可以为缺失的值设置默认值。如果系统在某个映射中遇到 nil,则将使用默认值,否则忽略。您还可以使用 willMapBlock 检查 JSON 是否缺失某些内容,只需检查它,如果缺失某些内容,添加一些值。

设置默认值的方式很简单

    [childMapping setDefaultValueForAttributes:@{@"isMale" : @YES}];

注意:字典中的键是 toKey

为不同种类的映射添加关系映射

您还可以将一个管理的对象映射(ManagedObjectMapping)作为一个正常NSObject映射的关系映射。有关更多详细信息,请参阅 testManagedObjectMapping_embedded 测试方法。

请注意,并非所有不同种类的映射都可以添加为关系映射。这取决于具体实现。例如,您只能将 managedObjectMapping 添加为 managedObjectMapping 的关系。

对 Swift 的支持

如果您想在 Swift 中使用此映射系统,您必须

  1. 使得您的类从 NSObject 继承,并支持调用 'init()' 方法。
  2. 将每个属性设置为非可选的,并给每个属性提供一个默认值。

因为我们的系统使用 KVC,但 Swift 中的 KVC 支持不如 Objective-C 强。您不能对可选类型使用 'setValueForKey'。