Skip to content

Commit

Permalink
support for Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, Numbe…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Lebel committed Mar 5, 2015
1 parent 9e8367e commit 104d9cd
Show file tree
Hide file tree
Showing 4 changed files with 370 additions and 261 deletions.
7 changes: 7 additions & 0 deletions MongoObjCDriver Tests/MODJsonParsingTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,11 @@ - (void)testTrueFalseParsing
shouldEqual:[MODSortedDictionary sortedDictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], @"false", [NSNumber numberWithBool:YES], @"true", nil]];
}

- (void)testInfinityAndNaN
{
[self jsonTesterWithJsonToParse:@"{\"infinity\":Number.POSITIVE_INFINITY,\"-infinity\":Number.NEGATIVE_INFINITY,\"NaN\":Number.NaN}"
jsonExpected:nil
shouldEqual:[MODSortedDictionary sortedDictionaryWithObjectsAndKeys:[NSNumber numberWithDouble:INFINITY], @"infinity", [NSNumber numberWithDouble:-INFINITY], @"-infinity", [NSNumber numberWithDouble:-NAN], @"NaN", nil]];
}

@end
23 changes: 16 additions & 7 deletions Sources/MODClient_utils.m
Original file line number Diff line number Diff line change
Expand Up @@ -659,14 +659,23 @@ static void convertValueToJson(NSMutableString *result, int indent, id value, NS
[result appendString:@"false"];
}
} else if (strcmp([value objCType], @encode(double)) == 0 || strcmp([value objCType], @encode(float)) == 0) {
NSMutableString *stringValue;
NSString *stringValue;

// make sure a double always ends with .0 (at least)
stringValue = [NSMutableString stringWithFormat:@"%.20g", [value doubleValue]];
if ([stringValue rangeOfString:@"."].location == NSNotFound) {
[stringValue appendString:@".0"];
}
[result appendString:stringValue];
if ([value doubleValue] == INFINITY) {
stringValue = @"Number.POSITIVE_INFINITY";
} else if ([value doubleValue] == -INFINITY) {
stringValue = @"Number.NEGATIVE_INFINITY";
} else if (isnan([value doubleValue])) {
stringValue = @"Number.NaN";
} else {
// make sure a double always ends with .0 (at least)
NSMutableString *mutableStringValue = [NSMutableString stringWithFormat:@"%.20g", [value doubleValue]];
if ([mutableStringValue rangeOfString:@"."].location == NSNotFound) {
[mutableStringValue appendString:@".0"];
}
stringValue = mutableStringValue;
}
[result appendString:stringValue];
} else if (strcmp([value objCType], @encode(long long)) == 0 || strcmp([value objCType], @encode(unsigned long long)) == 0) {
[result appendFormat:@"NumberLong(%@)", [value description]];
} else {
Expand Down
Loading

0 comments on commit 104d9cd

Please sign in to comment.