JSONKeyMapper.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // JSONKeyMapper.m
  3. // JSONModel
  4. //
  5. #import "JSONKeyMapper.h"
  6. @implementation JSONKeyMapper
  7. - (instancetype)initWithJSONToModelBlock:(JSONModelKeyMapBlock)toModel modelToJSONBlock:(JSONModelKeyMapBlock)toJSON
  8. {
  9. return [self initWithModelToJSONBlock:toJSON];
  10. }
  11. - (instancetype)initWithModelToJSONBlock:(JSONModelKeyMapBlock)toJSON
  12. {
  13. if (!(self = [self init]))
  14. return nil;
  15. _modelToJSONKeyBlock = toJSON;
  16. return self;
  17. }
  18. - (instancetype)initWithDictionary:(NSDictionary *)map
  19. {
  20. NSDictionary *toJSON = [JSONKeyMapper swapKeysAndValuesInDictionary:map];
  21. return [self initWithModelToJSONDictionary:toJSON];
  22. }
  23. - (instancetype)initWithModelToJSONDictionary:(NSDictionary <NSString *, NSString *> *)toJSON
  24. {
  25. if (!(self = [super init]))
  26. return nil;
  27. _modelToJSONKeyBlock = ^NSString *(NSString *keyName)
  28. {
  29. return [toJSON valueForKeyPath:keyName] ?: keyName;
  30. };
  31. return self;
  32. }
  33. - (JSONModelKeyMapBlock)JSONToModelKeyBlock
  34. {
  35. return nil;
  36. }
  37. + (NSDictionary *)swapKeysAndValuesInDictionary:(NSDictionary *)dictionary
  38. {
  39. NSArray *keys = dictionary.allKeys;
  40. NSArray *values = [dictionary objectsForKeys:keys notFoundMarker:[NSNull null]];
  41. return [NSDictionary dictionaryWithObjects:keys forKeys:values];
  42. }
  43. - (NSString *)convertValue:(NSString *)value isImportingToModel:(BOOL)importing
  44. {
  45. return [self convertValue:value];
  46. }
  47. - (NSString *)convertValue:(NSString *)value
  48. {
  49. return _modelToJSONKeyBlock(value);
  50. }
  51. + (instancetype)mapperFromUnderscoreCaseToCamelCase
  52. {
  53. return [self mapperForSnakeCase];
  54. }
  55. + (instancetype)mapperForSnakeCase
  56. {
  57. return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName)
  58. {
  59. NSMutableString *result = [NSMutableString stringWithString:keyName];
  60. NSRange range;
  61. // handle upper case chars
  62. range = [result rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]];
  63. while (range.location != NSNotFound)
  64. {
  65. NSString *lower = [result substringWithRange:range].lowercaseString;
  66. [result replaceCharactersInRange:range withString:[NSString stringWithFormat:@"_%@", lower]];
  67. range = [result rangeOfCharacterFromSet:[NSCharacterSet uppercaseLetterCharacterSet]];
  68. }
  69. // handle numbers
  70. range = [result rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]];
  71. while (range.location != NSNotFound)
  72. {
  73. NSRange end = [result rangeOfString:@"\\D" options:NSRegularExpressionSearch range:NSMakeRange(range.location, result.length - range.location)];
  74. // spans to the end of the key name
  75. if (end.location == NSNotFound)
  76. end = NSMakeRange(result.length, 1);
  77. NSRange replaceRange = NSMakeRange(range.location, end.location - range.location);
  78. NSString *digits = [result substringWithRange:replaceRange];
  79. [result replaceCharactersInRange:replaceRange withString:[NSString stringWithFormat:@"_%@", digits]];
  80. range = [result rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet] options:0 range:NSMakeRange(end.location + 1, result.length - end.location - 1)];
  81. }
  82. return result;
  83. }];
  84. }
  85. + (instancetype)mapperForTitleCase
  86. {
  87. return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName)
  88. {
  89. return [keyName stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[keyName substringToIndex:1].uppercaseString];
  90. }];
  91. }
  92. + (instancetype)mapperFromUpperCaseToLowerCase
  93. {
  94. return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName)
  95. {
  96. return keyName.uppercaseString;
  97. }];
  98. }
  99. + (instancetype)mapper:(JSONKeyMapper *)baseKeyMapper withExceptions:(NSDictionary *)exceptions
  100. {
  101. NSDictionary *toJSON = [JSONKeyMapper swapKeysAndValuesInDictionary:exceptions];
  102. return [self baseMapper:baseKeyMapper withModelToJSONExceptions:toJSON];
  103. }
  104. + (instancetype)baseMapper:(JSONKeyMapper *)baseKeyMapper withModelToJSONExceptions:(NSDictionary *)toJSON
  105. {
  106. return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName)
  107. {
  108. if (!keyName)
  109. return nil;
  110. if (toJSON[keyName])
  111. return toJSON[keyName];
  112. return baseKeyMapper.modelToJSONKeyBlock(keyName);
  113. }];
  114. }
  115. @end