Jastor.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Jastor.m
  3. // wq
  4. //
  5. // Created by berwin on 13-7-11.
  6. // Copyright (c) 2013年 Weqia. All rights reserved.
  7. //
  8. #import "Jastor.h"
  9. #import "JastorRuntimeHelper.h"
  10. #import "JSONKit.h"
  11. @implementation Jastor
  12. @synthesize objectId;
  13. static NSString *idPropertyName = @"idProperty";
  14. static NSString *idPropertyNameOnObject = @"objectId";
  15. Class nsDictionaryClass;
  16. Class nsArrayClass;
  17. + (id)objectFromDictionary:(NSDictionary*)dictionary {
  18. id item = [[self alloc] initWithDictionary:dictionary];
  19. return item;
  20. }
  21. - (id)initWithDictionary:(NSDictionary *)dictionary {
  22. if (!nsDictionaryClass) nsDictionaryClass = [NSDictionary class];
  23. if (!nsArrayClass) nsArrayClass = [NSArray class];
  24. if ((self = [super init])) {
  25. for (NSString *key in [JastorRuntimeHelper propertyNames:[self class]]) {
  26. id value = [dictionary valueForKey:key];
  27. if (value == [NSNull null] || value == nil) {
  28. continue;
  29. }
  30. if ([JastorRuntimeHelper isPropertyReadOnly:[self class] propertyName:key]) {
  31. continue;
  32. }
  33. if ([value isKindOfClass:nsDictionaryClass]) {
  34. }
  35. else if ([value isKindOfClass:nsArrayClass]) {
  36. }
  37. [self setValue:value forKey:key];
  38. }
  39. id objectIdValue;
  40. if ((objectIdValue = [dictionary objectForKey:idPropertyName]) && objectIdValue != [NSNull null]) {
  41. if (![objectIdValue isKindOfClass:[NSString class]]) {
  42. objectIdValue = [NSString stringWithFormat:@"%@", objectIdValue];
  43. }
  44. [self setValue:objectIdValue forKey:idPropertyNameOnObject];
  45. }
  46. }
  47. return self;
  48. }
  49. - (void)encodeWithCoder:(NSCoder*)encoder {
  50. [encoder encodeObject:self.objectId forKey:idPropertyNameOnObject];
  51. for (NSString *key in [JastorRuntimeHelper propertyNames:[self class]]) {
  52. [encoder encodeObject:[self valueForKey:key] forKey:key];
  53. }
  54. }
  55. - (id)initWithCoder:(NSCoder *)decoder {
  56. if ((self = [super init])) {
  57. [self setValue:[decoder decodeObjectForKey:idPropertyNameOnObject] forKey:idPropertyNameOnObject];
  58. for (NSString *key in [JastorRuntimeHelper propertyNames:[self class]]) {
  59. if ([JastorRuntimeHelper isPropertyReadOnly:[self class] propertyName:key]) {
  60. continue;
  61. }
  62. id value = [decoder decodeObjectForKey:key];
  63. if (value != [NSNull null] && value != nil) {
  64. [self setValue:value forKey:key];
  65. }
  66. }
  67. }
  68. return self;
  69. }
  70. - (NSMutableDictionary *)toDictionary {
  71. return [self toDictionaryWithProperty:nil];
  72. }
  73. -(NSMutableDictionary*)toDictionaryWithProperty:(BOOL(^)(NSString * propertyName))property
  74. {
  75. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  76. if (self.objectId) {
  77. [dic setObject:self.objectId forKey:idPropertyName];
  78. }
  79. for (NSString *key in [JastorRuntimeHelper propertyNames:[self class]]) {
  80. if(property){
  81. if(!property(key))
  82. continue;
  83. }
  84. id value = [self valueForKey:key];
  85. if (value && [value isKindOfClass:[Jastor class]]) {
  86. [dic setObject:[value toDictionary] forKey:key];
  87. } else if (value && [value isKindOfClass:[NSArray class]] && ((NSArray*)value).count > 0) {
  88. id internalValue = [value objectAtIndex:0];
  89. if (internalValue && [internalValue isKindOfClass:[Jastor class]]) {
  90. NSMutableArray *internalItems = [NSMutableArray array];
  91. for (id item in value) {
  92. [internalItems addObject:[item toDictionary]];
  93. }
  94. [dic setObject:internalItems forKey:key];
  95. } else {
  96. [dic setObject:value forKey:key];
  97. }
  98. }else if([value isKindOfClass:[NSDictionary class]]&&value!=nil){
  99. BOOL can=YES;
  100. NSArray * keys=[value allKeys];
  101. for(NSString *key in keys){
  102. id Value=[((NSDictionary*)value) objectForKey:key];
  103. if([Value isKindOfClass:[NSData class]])
  104. {
  105. can=NO;
  106. break;
  107. }
  108. }
  109. if(can){
  110. NSString * string=[(NSDictionary*)value JSONString];
  111. [dic setObject:string forKey:key];
  112. }else{
  113. [dic setObject:value forKey:key];
  114. }
  115. }
  116. else if (value != nil) {
  117. [dic setObject:value forKey:key];
  118. }
  119. }
  120. return dic;
  121. }
  122. - (NSString *)description {
  123. NSMutableDictionary *dic = [self toDictionary];
  124. return [NSString stringWithFormat:@"#<%@: id = %@ %@>", [self class], self.objectId, [dic description]];
  125. }
  126. - (BOOL)isEqual:(id)object {
  127. if (object == nil || ![object isKindOfClass:[Jastor class]]) return NO;
  128. Jastor *model = (Jastor *)object;
  129. return [self.objectId isEqualToString:model.objectId];
  130. }
  131. @end