Skip to content

Commit

Permalink
Make JSONDecodable handle String->Double conversion too
Browse files Browse the repository at this point in the history
# Conflicts:
#	Sources/JSONDecodable.swift
  • Loading branch information
cbrauchli authored and Alexander Sporn committed Sep 16, 2016
1 parent 33a5880 commit b688b5b
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion Tests/JSONParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class JSONParserTests: XCTestCase {
("-123.45e-2", -123.45e-2),
("0.1e-1", 0.01),
("-0.1E-1", -0.01),
("1472861857112", 1472861857112),
] {
do {
let value = try JSONFromString(string).double()
Expand Down Expand Up @@ -288,10 +289,46 @@ class JSONParserTests: XCTestCase {

// The Freddy parser behaves consistently across architectures.
XCTAssertEqual(try? json.int("exceedsIntMax"), nil, "as int")
XCTAssertEqual(try? json.double("exceedsIntMax"), nil, "as double")
XCTAssertEqual(try? json.double("exceedsIntMax"), Double(anyValueExceedingIntMax), "as double")
XCTAssertEqual(try? json.string("exceedsIntMax"), anyValueExceedingIntMax.description, "as string")
}

// This test should also be run on the iPhone 5 simulator to check 32-bit support.
func testOverflowingInt32ResultsInIntWhenAppropriateWithFreddyParser() {
let anyValueExceedingInt32Max: Int64 = 4611686018427387901 // This is a 62-bit number that is not accurately representable as a double
let jsonString = "{\"exceedsInt32Max\": \(anyValueExceedingInt32Max)}"

let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!
guard let json = try? JSON(data: data) else {
XCTFail("Failed to even parse JSON: \(jsonString)")
return
}

let expectedIntValue: Int64? = Int64(Int.max) > anyValueExceedingInt32Max ? anyValueExceedingInt32Max : nil
let actualIntValue: Int64?
do {
actualIntValue = try Int64(json.int("exceedsInt32Max"))
} catch {
actualIntValue = nil
}

// The Freddy parser behaves consistently across architectures.
XCTAssertEqual(actualIntValue, expectedIntValue, "as int")
XCTAssertEqual(try? json.double("exceedsInt32Max"), Double(anyValueExceedingInt32Max), "as double")
XCTAssertEqual(try? json.string("exceedsInt32Max"), anyValueExceedingInt32Max.description, "as string")
XCTAssertNotEqual(anyValueExceedingInt32Max, Int64(try! json.double("exceedsInt32Max")), "as double to int64")
}

// This test should also be run on the iPhone 5 simulator to check 32-bit support.
func testLargeIntResultsInStringOrIntWithFreddyParser() {
let largeValue = "1472861857112"
let expectedResult = Int(largeValue) // nil if on 32-bit, an Int otherwise

let value = try? JSONFromString("\(largeValue)").int()
XCTAssertEqual(value, expectedResult)
XCTAssertEqual(try? JSONFromString("\(largeValue)").string(), largeValue)
}

// This was tripping a fatalError with the Freddy parser for 64-bit at one point:
// fatal error: floating point value can not be converted to Int because it is greater than Int.max
// because we assumed the double would be in range of Int.
Expand Down

0 comments on commit b688b5b

Please sign in to comment.