diff --git a/BaseModels/MSObject.m b/BaseModels/MSObject.m index 8b3b1f1f..4d808ecb 100644 --- a/BaseModels/MSObject.m +++ b/BaseModels/MSObject.m @@ -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