Skip to content
This repository was archived by the owner on Apr 18, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion BaseModels/MSObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,46 @@ -(NSMutableDictionary *)getDictionary

- (NSData *)getSerializedDataWithError:(NSError *__autoreleasing *)error
{
return [NSJSONSerialization dataWithJSONObject:self.dictionary options:kNilOptions error:error];
NSDictionary *convertedDictionary = [self recursivelyConvertObjectsToDictionariesInDictionary:self.dictionary];
return [NSJSONSerialization dataWithJSONObject:convertedDictionary options:kNilOptions error:error];
}

- (NSDictionary *)recursivelyConvertObjectsToDictionariesInDictionary:(NSDictionary *)dictionary {
NSMutableDictionary *convertedDictionary = [NSMutableDictionary dictionary];
for (NSString *key in dictionary) {
if ([dictionary[key] isKindOfClass:[MSObject class]]) {
MSObject *object = dictionary[key];
convertedDictionary[key] = [self recursivelyConvertObjectsToDictionariesInDictionary:object.dictionary];
} else if ([dictionary[key] isKindOfClass:[NSArray class]]) {
NSArray *array = dictionary[key];
convertedDictionary[key] = [self recursivelyConvertObjectsToDictionariesInArray:array];
} else if ([dictionary[key] respondsToSelector:@selector(ms_toString)]) {
NSString *enumValue = [dictionary[key] performSelector:@selector(ms_toString)];
convertedDictionary[key] = enumValue;
} else {
convertedDictionary[key] = dictionary[key];
}
}
return convertedDictionary;
}

- (NSArray *)recursivelyConvertObjectsToDictionariesInArray:(NSArray *)array {
NSMutableArray *convertedArray = [NSMutableArray array];
for (id item in array) {
if ([item isKindOfClass:[MSObject class]]) {
MSObject *object = item;
[convertedArray addObject:[self recursivelyConvertObjectsToDictionariesInDictionary:object.dictionary]];
} else if ([item isKindOfClass:[NSArray class]]) {
NSArray *array = item;
[convertedArray addObject:[self recursivelyConvertObjectsToDictionariesInArray:array]];
} else if ([item respondsToSelector:@selector(ms_toString)]) {
NSString *enumValue = [item performSelector:@selector(ms_toString)];
[convertedArray addObject:enumValue];
} else {
[convertedArray addObject:item];
}
}
return convertedArray;
}

@end