diff --git a/RollbarCommon/Sources/RollbarCommon/DTOs/RollbarDTO+Protected.m b/RollbarCommon/Sources/RollbarCommon/DTOs/RollbarDTO+Protected.m index 440ca097..810ced3d 100644 --- a/RollbarCommon/Sources/RollbarCommon/DTOs/RollbarDTO+Protected.m +++ b/RollbarCommon/Sources/RollbarCommon/DTOs/RollbarDTO+Protected.m @@ -143,7 +143,16 @@ - (void)setNumber:(NSNumber *)data forKey:(NSString *)key { - (RollbarDTO *)safelyGetDataTransferObjectByKey:(NSString *)key { - RollbarDTO *result = [[RollbarDTO alloc] initWithDictionary:[self getDataByKey:key]]; + RollbarDTO *result = [RollbarDTO alloc]; + + NSDictionary *data = [self getDataByKey:key]; + if (data) { + result = [result initWithDictionary:[self getDataByKey:key]]; + } + else { + result = [result init]; + } + return result; } diff --git a/RollbarCommon/Sources/RollbarCommon/DTOs/RollbarDTO.m b/RollbarCommon/Sources/RollbarCommon/DTOs/RollbarDTO.m index eca90101..dda7e55c 100644 --- a/RollbarCommon/Sources/RollbarCommon/DTOs/RollbarDTO.m +++ b/RollbarCommon/Sources/RollbarCommon/DTOs/RollbarDTO.m @@ -8,6 +8,7 @@ #import "RollbarDTO.h" #import "RollbarSdkLog.h" +#import "NSJSONSerialization+Rollbar.h" //#import //#import "objc/runtime.h" @@ -74,46 +75,57 @@ + (nullable NSData *)dataWithJSONObject:(id)obj } + (NSMutableDictionary *)safeDataFromJSONObject:(id)obj { - NSMutableDictionary *safeData = [NSMutableDictionary new]; - [obj enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { - if ([obj isKindOfClass:[NSDictionary class]]) { - [safeData setObject:[[self class] safeDataFromJSONObject:obj] forKey:key]; - } else if ([obj isKindOfClass:[NSArray class]]) { - [safeData setObject:((NSArray *)obj).mutableCopy forKey:key]; - } else if ([obj isKindOfClass:[NSNumber class]]) { - [safeData setObject:obj forKey:key]; - } else if ([obj isKindOfClass:[NSString class]]) { - [safeData setObject:obj forKey:key]; - } else if ([obj isKindOfClass:[NSDate class]]) { - [safeData setObject:[obj description] forKey:key]; - } else if ([obj isKindOfClass:[NSURL class]]) { - [safeData setObject:[obj absoluteString] forKey:key]; - } else if ([obj isKindOfClass:[NSError class]]) { - [safeData setObject:[[self class] safeDataFromJSONObject:[obj userInfo]] forKey:key]; - } else if ([obj isKindOfClass:[NSHTTPURLResponse class]]) { - [safeData setObject:[obj allHeaderFields] forKey:key]; - } else if ([obj isKindOfClass:[NSSet class]]) { - [safeData setObject:[[obj allObjects] componentsJoinedByString:@","] forKey:key]; - } else if ([obj isKindOfClass:[NSData class]]) { - NSError* error = nil; - NSMutableDictionary* json = - [NSJSONSerialization JSONObjectWithData:obj - options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves) - error:&error]; - - if (error == nil) { - [safeData setObject:[[self class] safeDataFromJSONObject:json] forKey:key]; - } else { - RollbarSdkLog(@"Error serializing NSData: %@", [error localizedDescription]); - } - } else if ([NSJSONSerialization isValidJSONObject:@{key:obj}]) { - [safeData setObject:obj forKey:key]; - } else { - RollbarSdkLog(@"Error serializing class '%@' using NSJSONSerialization", - NSStringFromClass([obj class])); - } - }]; - return safeData; + + return [NSJSONSerialization rollbar_safeDataFromJSONObject:obj]; + +// NSMutableDictionary *safeData = [NSMutableDictionary new]; +// [obj enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { +// +// if ([obj isKindOfClass:[NSDictionary class]]) { +// [safeData setObject:[[self class] safeDataFromJSONObject:obj] forKey:key]; +// } else if ([obj isKindOfClass:[NSArray class]]) { +// [safeData setObject:((NSArray *)obj).mutableCopy forKey:key]; +// } else if ([obj isKindOfClass:[NSNumber class]]) { +// [safeData setObject:obj forKey:key]; +// } else if ([obj isKindOfClass:[NSString class]]) { +// [safeData setObject:obj forKey:key]; +// } else if ([obj isKindOfClass:[NSDate class]]) { +// [safeData setObject:[obj description] forKey:key]; +// } else if ([obj isKindOfClass:[NSURL class]]) { +// NSString *url = [obj absoluteString]; +// if (url) { +// [safeData setObject:[obj absoluteString] forKey:key]; +// } +// else if([obj description]) { +// [safeData setObject:[obj description] forKey:key]; +// } +// } else if ([obj isKindOfClass:[NSError class]]) { +// [safeData setObject:[[self class] safeDataFromJSONObject:[obj userInfo]] forKey:key]; +// } else if ([obj isKindOfClass:[NSHTTPURLResponse class]]) { +// [safeData setObject:[obj allHeaderFields] forKey:key]; +// } else if ([obj isKindOfClass:[NSSet class]]) { +// [safeData setObject:[[obj allObjects] componentsJoinedByString:@","] forKey:key]; +// } else if ([obj isKindOfClass:[NSData class]]) { +// NSError* error = nil; +// NSMutableDictionary* json = +// [NSJSONSerialization JSONObjectWithData:obj +// options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves) +// error:&error]; +// +// if (error == nil) { +// [safeData setObject:[[self class] safeDataFromJSONObject:json] forKey:key]; +// } else { +// RollbarSdkLog(@"Error serializing NSData: %@", [error localizedDescription]); +// } +// } else if ([NSJSONSerialization isValidJSONObject:@{key:obj}]) { +// [safeData setObject:obj forKey:key]; +// } else { +// RollbarSdkLog(@"Error serializing class '%@' using NSJSONSerialization", +// NSStringFromClass([obj class])); +// } +// }]; +// +// return safeData; } + (unsigned long)measureJSONDataByteSize:(NSData*)jsonData { @@ -207,16 +219,21 @@ - (NSArray *)getDefinedProperties { NSMutableArray *result = [NSMutableArray array]; - unsigned int outCount, i; + unsigned int outCount; objc_property_t *properties = class_copyPropertyList([self class], &outCount); + if (!properties) { + return result; + } - for(i = 0; i < outCount; ++i) { + for(unsigned int i = 0; i < outCount; ++i) { objc_property_t property = properties[i]; const char *propName = property_getName(property); if(propName) { NSString *propertyName = [NSString stringWithCString:propName encoding:[NSString defaultCStringEncoding]]; - [result addObject:propertyName]; + if (propertyName) { + [result addObject:propertyName]; + } } } diff --git a/RollbarCommon/Sources/RollbarCommon/NSJSONSerialization+Rollbar.m b/RollbarCommon/Sources/RollbarCommon/NSJSONSerialization+Rollbar.m index 978098ce..8963c0e5 100644 --- a/RollbarCommon/Sources/RollbarCommon/NSJSONSerialization+Rollbar.m +++ b/RollbarCommon/Sources/RollbarCommon/NSJSONSerialization+Rollbar.m @@ -40,15 +40,28 @@ + (NSMutableDictionary *)rollbar_safeDataFromJSONObject:(id)obj { NSMutableDictionary *safeData = [NSMutableDictionary new]; [obj enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { + if ([obj isKindOfClass:[NSDictionary class]]) { [safeData setObject:[[self class] rollbar_safeDataFromJSONObject:obj] forKey:key]; + } else if ([obj isKindOfClass:[NSArray class]]) { + [safeData setObject:((NSArray *)obj).mutableCopy forKey:key]; } else if ([NSJSONSerialization isValidJSONObject:@{key:obj}]) { [safeData setObject:obj forKey:key]; + } else if ([obj isKindOfClass:[NSNumber class]]) { + [safeData setObject:obj forKey:key]; + } else if ([obj isKindOfClass:[NSString class]]) { + [safeData setObject:obj forKey:key]; } else if ([obj isKindOfClass:[NSDate class]]) { [safeData setObject:[obj description] forKey:key]; } else if ([obj isKindOfClass:[NSURL class]]) { - [safeData setObject:[obj absoluteString] forKey:key]; - } else if ([obj isKindOfClass:[NSError class]]) { + NSString *url = [obj absoluteString]; + if (url) { + [safeData setObject:[obj absoluteString] forKey:key]; + } + else if([obj description]) { + [safeData setObject:[obj description] forKey:key]; + } + } else if ([obj isKindOfClass:[NSError class]] && [obj userInfo]) { [safeData setObject:[[self class] rollbar_safeDataFromJSONObject:[obj userInfo]] forKey:key]; } else if ([obj isKindOfClass:[NSHTTPURLResponse class]]) { [safeData setObject:[obj allHeaderFields] forKey:key]; diff --git a/RollbarCommon/Sources/RollbarCommon/include/NSJSONSerialization+Rollbar.h b/RollbarCommon/Sources/RollbarCommon/include/NSJSONSerialization+Rollbar.h index 59df6b5e..02fc973b 100644 --- a/RollbarCommon/Sources/RollbarCommon/include/NSJSONSerialization+Rollbar.h +++ b/RollbarCommon/Sources/RollbarCommon/include/NSJSONSerialization+Rollbar.h @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN /// Turns JSON-like object structure into a valid NSJSONSerialization structure /// @param obj JSON-like object -+ (NSDictionary *)rollbar_safeDataFromJSONObject:(id)obj; ++ (NSMutableDictionary *)rollbar_safeDataFromJSONObject:(id)obj; /// Byte-length of a NSData representation of a JSON structure /// @param jsonData NSData representation of a JSON structure diff --git a/RollbarCommon/Sources/RollbarCommon/include/RollbarDTO+Protected.h b/RollbarCommon/Sources/RollbarCommon/include/RollbarDTO+Protected.h index c8742025..903b68b4 100644 --- a/RollbarCommon/Sources/RollbarCommon/include/RollbarDTO+Protected.h +++ b/RollbarCommon/Sources/RollbarCommon/include/RollbarDTO+Protected.h @@ -41,7 +41,7 @@ NS_ASSUME_NONNULL_BEGIN - (NSMutableDictionary *)safelyGetDictionaryByKey:(NSString *)key; - (NSMutableArray *)safelyGetArrayByKey:(NSString *)key; - (NSMutableString *)safelyGetStringByKey:(NSString *)key; -- (NSNumber *)safelyGetNumberByKey:(NSString *)key; +- (nullable NSNumber *)safelyGetNumberByKey:(NSString *)key; #pragma mark - Core API: data setters by key