HPManagedObjects 1.2.4

HPManagedObjects 1.2.4

测试已测试
Lang语言 Obj-CObjective C
许可证 GPL-3.0
发布最新发布2019年11月

DimasSup 维护。



  • DimasSup

HPManagedObjects

CI Status Version License Platform

支持 Swift 3.0/Obj-C

内部结构

简单的 Obj-c iOS JSON 库。帮助您使您的类支持序列化和反序列化。

已知的 Swift 限制

  • Swift 枚举目前不可用。

示例

要运行示例项目,请先克隆仓库,然后从 Example 目录中运行 pod install。将 HPManagedObjects 导入到您的文件中

@interface BaseOtherJsonClassObject : BaseManagedObjectModel
@property(nonatomic,strong,nullable)NSString* baseIdentifier;
@property(nonatomic,assign)int objectType;//0 - OtherJsonClassObject, 1 - SecondOtherJsonClassObject, 2 - ThirdOtherJsonClassObject
@end
@implementation BaseOtherJsonClassObject
+(Mapping *)mapping
{
	Mapping* mapping = [super mapping];
	
	MappingDescriptor* descriptor = [MappingDescriptor descriptorBy:@"baseIdentifier" jsonName:@"base_id"];
	[mapping.mapings addObject:descriptor];
	
	descriptor = [MappingDescriptor descriptorBy:@"objectType" jsonName:@"type"];
	[mapping.mapings addObject:descriptor];
	
	
	return mapping;
}
@end


@interface OtherJsonClassObject : BaseOtherJsonClassObject
@property(nonatomic,strong,nullable)NSString* anySomeProperty;
@end
@implementation OtherJsonClassObject
+(Mapping *)mapping
{
	Mapping* mapping = [super mapping];
	
	[mapping.mapings addObject:[MappingDescriptor descriptorBy:@"anySomeProperty"]];
	
	return mapping;
}
@end


@interface SecondOtherJsonClassObject : BaseOtherJsonClassObject
@property(nonatomic,assign)int secondProperty;
@end
@implementation SecondOtherJsonClassObject
+(Mapping *)mapping
{
	Mapping* mapping = [super mapping];
	[mapping.mapings addObject:[MappingDescriptor descriptorBy:@"secondProperty"]];
	return mapping;
}
@end


@interface ThirdOtherJsonClassObject : BaseOtherJsonClassObject
@property(nonatomic,strong)id anyValue;
@end
@implementation ThirdOtherJsonClassObject
+(Mapping *)mapping
{
	Mapping* mapping = [super mapping];
	[mapping.mapings addObject:[MappingDescriptor descriptorBy:@"anyValue"]];
	
	return mapping;
}
@end
@interface SimpleJSONObject: BaseManagedObjectModel{
}
@property(nonatomic,strong,nullable)NSString* textValue;
@property(nonatomic,assign)long objectId;
@property(nonatomic,strong,nullable)NSArray<NSString*>* stringValues;
@property(nonatomic,strong,nullable)NSDictionary<NSString*,id>* anyValues;
@property(nonatomic,strong,nullable)OtherJsonClassObject* otherObject;
@property(nonatomic,strong,nullable)NSArray<BaseOtherJsonClassObject*>* otherObjectsArray;
@property(nonatomic,strong,nullable)NSArray<BaseOtherJsonClassObject*>* otherObjectsWitBlockSelectorArray;

@property(nonatomic,strong,nullable)NSDate* creationDate;
@property(nonatomic,assign)CGPoint point;
@end

@implementation SimpleJSONObject
+(Mapping *)mapping
{
	Mapping* mapping = [super mapping];
	
	[mapping.mapings addObject:[MappingDescriptor descriptorBy:@"textValue" jsonName:@"text"]];
	
	//JSON name same as text field name
	[mapping.mapings addObject:[MappingDescriptor descriptorBy:@"objectId"]];
	
	//No need specified array of items, it recognizes array and dictionary
	[mapping.mapings addObject:[MappingDescriptor descriptorBy:@"stringValues" jsonName:@"vals"]];
	
	//No need specified array of items, it recognizes array and dictionary
	[mapping.mapings addObject:[MappingDescriptor descriptorBy:@"anyValues" jsonName:@"vals_map"]];
	
	//Serialize/deserialize date field in UTC zone
	[mapping.mapings addObject:[MappingDescriptor descriptorBy:@"creationDate" jsonName:@"create_date" columnName:nil format:@"<your_date_fromat>"]];
	
	//Serialize OtherJsonClassObject to dictionary, deserialize dictionary to OtherJsonClassObject
	[mapping.mapings addObject:[MappingDescriptor descriptorBy:@"otherObject" jsonName:@"subclass" className:@"OtherJsonClassObject"]];
	
	//User custom serializer/deserializer for property
	[mapping.mapings addObject:[MappingDescriptor descriptorBy:@"point" jsonName:@"point" className:nil columnName:nil asString:FALSE convert:^id(NSString* value) {
		//Convert JSON value to needed class
		NSArray* arr =  [value componentsSeparatedByString:@":"];
		CGPoint point = CGPointZero;
		if(arr.count == 2)
		{
			point.x = [[arr firstObject] floatValue];
			point.y = [[arr lastObject] floatValue];
		}
		return [NSValue valueWithCGPoint:CGPointZero];
	} convertBack:^id(NSValue* value) {
		CGPoint point = [value CGPointValue];
		return [NSString stringWithFormat:@"%f:%f",point.x,point.y];
	}]];
	
	
	NSArray<TypeSelector*>* typeSelectors = @[
											  //if object in array containt field "anyValue" object will be deserialize to 'OtherJsonClassObject'
											  [TypeSelector selectorBy:@"anyValue" className:@"ThirdOtherJsonClassObject"],
											  //If object in array contain field 'type' and it is equal to '1' then object will be deserialize to 'SecondOtherJsonClassObject'
											  [TypeSelector selectorBy:@"type" value:@(1) className:@"SecondOtherJsonClassObject"],
											  //Make custom check for json property value, if block return YES - use OtherJsonClassObject as object type
											  [TypeSelector selectorBy:@"type" byValueBlock:^BOOL(id value) {
												  NSNumber* numb = value;
												  if([numb intValue] == 0)
												  {
													  return YES;
												  }
												  return NO;
												  
											  } className:@"OtherJsonClassObject"],
											  //If any type selector does not match -  use 'self' if you want use class as default
											  [TypeSelector selectorBy:@"self" className:@"BaseOtherJsonClassObject"],
											  ];
	
	MappingDescriptor* descriptor = [MappingDescriptor descriptorBy:@"otherObjectsArray"
														   jsonName:@"objc_array"
													  typeSelectors:typeSelectors];
	[mapping.mapings addObject:descriptor];
	
	
	
	//Recognize object type with custom logic. In block -  'rootDictionary'(my be nil) - object that containt 'value' object
	//'value' object -  json object that should be deserialize.
	descriptor = [MappingDescriptor descriptorBy:@"otherObjectsWitBlockSelectorArray"
										jsonName:@"objc_array_v2" columnName:nil classNameBlock:^NSString *(id rootDictionary, id value) {
											if([value isKindOfClass:[NSDictionary class]])
											{
												int type =  [[(NSDictionary*)value valueForKey:@"type"] intValue];
												if(type == 0)
												{
													return @"OtherJsonClassObject";
												}
												else if(type == 1)
												{
													return @"SecondOtherJsonClassObject";
												}
												else if(type == 2)
												{
													return @"ThirdOtherJsonClassObject";
												}
											}
											return @"BaseOtherJsonClassObject";
											
										}];
	[mapping.mapings addObject:descriptor];
	
	
	return mapping;
}
@end

使用

SimpleJSONObject* obj = [[SimpleJSONObject alloc] init];
//Update object with JSON dictionary (in obj-c you can pass NSString, it will serialize to JSON inside)
[obj updateWithDictionary:dictionary];

//Serialize object to JSON Dictionary
[obj toDictionary];

FMDB 支持

如果您使用 FMDB SQLite 作为数据存储,那么使用 HPManagedObjects 会很方便。

//Setup table name
mapping.tableName = @"your_table_name";
//If you have primary key -  setup it
mapping.idPropertyName = @"dabaseId";
mapping.idName = @"_id";//Primary column name in table

//For peroperty description that should saved in Database set column name
descriptor.columnName = @"type";

您可以使用 'DatabaseHelper' 类,通过 fmdb 更新/插入/选择逻辑来简化与 HPBaseManagedObjects 的使用。

解析 FMResultSet 到对象使用

SimpleJSONObject* obj = [[SimpleJSONObject alloc] init];
[obj updateFromDbSet:fmresult];

要求

安装

HPManagedObjects 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中

pod "HPManagedObjects/Main"
#if you using FMDB - 
pod "HPManagedObjects"

作者

DimasSup, [email protected]

许可协议

HPManagedObjects 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。