Skip to content

Fix for [SR-1250] NSJSONSerialization emits non-floating-point number… #914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Foundation/NSJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -716,22 +716,25 @@ private struct JSONReader {
defer { intEndPointer.deallocate(capacity: 1) }
let doubleEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
defer { doubleEndPointer.deallocate(capacity: 1) }

let intResult = strtol(startPointer, intEndPointer, 10)
let intDistance = startPointer.distance(to: intEndPointer[0]!)
let doubleResult = strtod(startPointer, doubleEndPointer)
let doubleDistance = startPointer.distance(to: doubleEndPointer[0]!)

guard intDistance > 0 || doubleDistance > 0 else {
return nil
}

if intDistance == doubleDistance {
return (intResult, intDistance)
}
guard doubleDistance > 0 else {
return nil
}

if doubleResult == doubleResult.rounded() {
return (Int(doubleResult), doubleDistance)
}
return (doubleResult, doubleDistance)
}
}
Expand Down
19 changes: 18 additions & 1 deletion TestFoundation/TestNSJSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ extension TestNSJSONSerialization {
XCTAssertEqual(result?[1] as? Int, -1)
XCTAssertEqual(result?[2] as? Double, 1.3)
XCTAssertEqual(result?[3] as? Double, -1.3)
XCTAssertEqual(result?[4] as? Double, 1000)
XCTAssertEqual(result?[4] as? Int, 1000)
XCTAssertEqual(result?[5] as? Double, 0.001)
}
} catch {
Expand Down Expand Up @@ -871,6 +871,7 @@ extension TestNSJSONSerialization {
("test_jsonObjectToOutputStreamInsufficientBuffer", test_jsonObjectToOutputStreamInsufficientBuffer),
("test_booleanJSONObject", test_booleanJSONObject),
("test_serialize_dictionaryWithDecimal", test_serialize_dictionaryWithDecimal),
("test_serializeDecimalNumberJSONObject", test_serializeDecimalNumberJSONObject),
]
}

Expand Down Expand Up @@ -1214,6 +1215,22 @@ extension TestNSJSONSerialization {
XCTAssertTrue(JSONSerialization.isValidJSONObject([true]))
}

func test_serializeDecimalNumberJSONObject() {
let decimalArray = "[12.1,10.0,0.0,0.0001,20,\(Int.max)]"
do {
let data = decimalArray.data(using: String.Encoding.utf8)
let result = try JSONSerialization.jsonObject(with: data!, options: []) as? [Any]
XCTAssertEqual(result?[0] as! Double, 12.1)
XCTAssertEqual(result?[1] as! Int, 10)
XCTAssertEqual(result?[2] as! Int, 0)
XCTAssertEqual(result?[3] as! Double, 0.0001)
XCTAssertEqual(result?[4] as! Int, 20)
XCTAssertEqual(result?[5] as! Int, Int.max)
} catch {
XCTFail("Failed during serialization")
}
}

fileprivate func createTestFile(_ path: String,_contents: Data) -> String? {
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
do {
Expand Down