FEMSerializer.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // For License please refer to LICENSE file in the root of FastEasyMapping project
  2. #import "FEMSerializer.h"
  3. #import "FEMAttribute.h"
  4. #import "FEMTypeIntrospection.h"
  5. #import "FEMRelationship.h"
  6. @implementation FEMSerializer
  7. + (NSDictionary *)_serializeObject:(id)object usingMapping:(FEMMapping *)mapping {
  8. NSMutableDictionary *representation = [NSMutableDictionary dictionary];
  9. for (FEMAttribute *fieldMapping in mapping.attributes) {
  10. [self setValueOnRepresentation:representation fromObject:object withFieldMapping:fieldMapping];
  11. }
  12. for (FEMRelationship *relationshipMapping in mapping.relationships) {
  13. [self setRelationshipObjectOn:representation usingMapping:relationshipMapping fromObject:object];
  14. }
  15. return representation;
  16. }
  17. + (NSDictionary *)serializeObject:(id)object usingMapping:(FEMMapping *)mapping {
  18. NSDictionary *representation = [self _serializeObject:object usingMapping:mapping];
  19. return mapping.rootPath.length > 0 ? @{mapping.rootPath : representation} : representation;
  20. }
  21. + (id)_serializeCollection:(NSArray *)collection usingMapping:(FEMMapping *)mapping {
  22. NSMutableArray *representation = [NSMutableArray new];
  23. for (id object in collection) {
  24. NSDictionary *objectRepresentation = [self _serializeObject:object usingMapping:mapping];
  25. [representation addObject:objectRepresentation];
  26. }
  27. return representation;
  28. }
  29. + (id)serializeCollection:(NSArray *)collection usingMapping:(FEMMapping *)mapping {
  30. NSArray *representation = [self _serializeCollection:collection usingMapping:mapping];
  31. return mapping.rootPath.length > 0 ? @{mapping.rootPath: representation} : representation;
  32. }
  33. + (void)setValueOnRepresentation:(NSMutableDictionary *)representation fromObject:(id)object withFieldMapping:(FEMAttribute *)fieldMapping {
  34. id returnedValue = [object valueForKey:fieldMapping.property];
  35. if (returnedValue) {
  36. returnedValue = [fieldMapping reverseMapValue:returnedValue] ?: [NSNull null];
  37. [self setValue:returnedValue forKeyPath:fieldMapping.keyPath inRepresentation:representation];
  38. }
  39. }
  40. + (void)setValue:(id)value forKeyPath:(NSString *)keyPath inRepresentation:(NSMutableDictionary *)representation {
  41. NSArray *keyPathComponents = [keyPath componentsSeparatedByString:@"."];
  42. if ([keyPathComponents count] == 1) {
  43. [representation setObject:value forKey:keyPath];
  44. } else if ([keyPathComponents count] > 1) {
  45. NSString *attributeKey = [keyPathComponents lastObject];
  46. NSMutableArray *subPaths = [NSMutableArray arrayWithArray:keyPathComponents];
  47. [subPaths removeLastObject];
  48. id currentPath = representation;
  49. for (NSString *key in subPaths) {
  50. id subPath = [currentPath valueForKey:key];
  51. if (subPath == nil) {
  52. subPath = [NSMutableDictionary new];
  53. [currentPath setValue:subPath forKey:key];
  54. }
  55. currentPath = subPath;
  56. }
  57. [currentPath setValue:value forKey:attributeKey];
  58. }
  59. }
  60. + (void)setRelationshipObjectOn:(NSMutableDictionary *)representation
  61. usingMapping:(FEMRelationship *)relationshipMapping
  62. fromObject:(id)object {
  63. id value = [object valueForKey:relationshipMapping.property];
  64. if (value) {
  65. id relationshipRepresentation = nil;
  66. if (relationshipMapping.isToMany) {
  67. relationshipRepresentation = [self _serializeCollection:value usingMapping:relationshipMapping.mapping];
  68. } else {
  69. relationshipRepresentation = [self _serializeObject:value usingMapping:relationshipMapping.mapping];
  70. }
  71. if (relationshipMapping.keyPath.length > 0) {
  72. [representation setObject:relationshipRepresentation forKey:relationshipMapping.keyPath];
  73. } else {
  74. NSParameterAssert([relationshipRepresentation isKindOfClass:NSDictionary.class]);
  75. [representation addEntriesFromDictionary:relationshipRepresentation];
  76. }
  77. }
  78. }
  79. @end