Skip to content

Commit

Permalink
Fixing crash in ROAD when serialization get nil as parameter #315
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuru Taustahuzau committed Jun 13, 2014
1 parent 93291da commit d35783d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,29 @@


/**
Convenience methods to return JSON dictionary from strings.
* Convenience methods to return JSON dictionary from strings.
*/
@interface NSJSONSerialization (RFJSONStringHandling)

/**
Returns a JSON object from a JSON string with the specified options.
@param string The JSON string.
@param options The NSJSONReadingOptions.
@param error The error of the parsing.
* Returns a JSON object from a JSON string with the specified options.
* @param string The JSON string.
* @param options The NSJSONReadingOptions.
* @param error The error of the parsing.
*/
+ (id)RF_JSONObjectWithString:(NSString * const)string options:(const NSJSONReadingOptions)options error:(NSError * __autoreleasing *)error;

/**
Returns a JSON object from a JSON string with default options. In case of error, it returns nil, but no error information.
@param string The JSON string.
* Returns a JSON object from a JSON string with default options. In case of error, it returns nil, but no error information.
* @param string The JSON string.
*/
+ (id)RF_JSONObjectWithString:(NSString * const)string;

/**
* Safe for nil method to deserialize data. Prints error if any.
* @param jsonData JSON data to deserialize.
* @return Deserialized object.
*/
+ (id)RF_decodeJSONData:(NSData *const)jsonData;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@


#import "NSJSONSerialization+RFJSONStringHandling.h"
#import "RFSerializationLog.h"


@implementation NSJSONSerialization (RFJSONStringHandling)

+ (id)RF_JSONObjectWithString:(NSString * const)string options:(const NSJSONReadingOptions)options error:(NSError * __autoreleasing *)error {
if ([string length] == 0) {
return nil;
}
NSData * const jsonData = [string dataUsingEncoding:NSUTF8StringEncoding];
id result = [self JSONObjectWithData:jsonData options:options error:error];
return result;
Expand All @@ -46,4 +50,17 @@ + (id)RF_JSONObjectWithString:(NSString * const)string {
return [self RF_JSONObjectWithString:string options:NSJSONReadingAllowFragments error:nil];
}

+ (id)RF_decodeJSONData:(NSData *const)jsonData {
if (!jsonData || [jsonData length] == 0) {
return nil;
}

NSError *error;
id value = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
if (error) {
RFSCLogError(@"ROADSerialization: Error when trying to deserialize data.\nError details: %@\nData: %@", error, [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
}
return value;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ + (id)decodeJSONString:(NSString *const)jsonString {
}

+ (id)decodeJSONData:(NSData * const)jsonData withSerializtionRoot:(NSString *)serializationRoot rootClassNamed:(NSString * const)rootClassName {
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
id const jsonObject = [NSJSONSerialization RF_decodeJSONData:jsonData];
id partOfJsonObject = jsonObject;
if (serializationRoot.length) {
partOfJsonObject = [self jsonObjectForKeyPath:serializationRoot atJsonObject:jsonObject];
Expand All @@ -92,7 +92,7 @@ + (id)decodeJSONString:(NSString *const)jsonString withRootClassNamed:(NSString
}

+ (id)decodeJSONData:(NSData * const)jsonData withRootClassNamed:(NSString * const)rootClassName {
id const jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
id const jsonObject = [NSJSONSerialization RF_decodeJSONData:jsonData];

return [self decodePredeserializedObject:jsonObject withRootClassName:rootClassName];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,12 @@ - (void)testSerializationOfBigUnixTimestamps {
XCTAssertTrue(fabs([deserializedTestObject.unixTimestamp timeIntervalSince1970] - timeInterval) < 1000, @"Big time intervale was corrupted");
}

- (void)testNilInDecoder {
id nil1 = [RFAttributedDecoder decodeJSONData:nil withRootClassNamed:@""];
id nil2 = [RFAttributedDecoder decodeJSONString:nil withRootClassNamed:@""];

XCTAssertNil(nil1, @"RFAttrbutedCoder returned value for nil data");
XCTAssertNil(nil2, @"RFAttrbutedCoder returned value for nil data");
}

@end

0 comments on commit d35783d

Please sign in to comment.