Skip to content

Commit 0423af9

Browse files
author
“saiHemak”
committed
Fix for [SR-1250] NSJSONSerialization emits non-floating-point numbers as Double
1 parent 4eac74e commit 0423af9

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

Foundation/NSJSONSerialization.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -702,21 +702,16 @@ private struct JSONReader {
702702
defer { intEndPointer.deallocate(capacity: 1) }
703703
let doubleEndPointer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1)
704704
defer { doubleEndPointer.deallocate(capacity: 1) }
705-
706705
let intResult = strtol(startPointer, intEndPointer, 10)
707706
let intDistance = startPointer.distance(to: intEndPointer[0]!)
708707
let doubleResult = strtod(startPointer, doubleEndPointer)
709708
let doubleDistance = startPointer.distance(to: doubleEndPointer[0]!)
710-
709+
let roundedDouble = doubleResult.rounded()
711710
guard intDistance > 0 || doubleDistance > 0 else {
712711
return nil
713712
}
714-
715-
if intDistance == doubleDistance {
716-
return (intResult, intDistance)
717-
}
718-
guard doubleDistance > 0 else {
719-
return nil
713+
if doubleResult == roundedDouble || doubleResult == 0.0 {
714+
return (Int(doubleResult), doubleDistance)
720715
}
721716
return (doubleResult, doubleDistance)
722717
}

TestFoundation/TestNSJSONSerialization.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ extension TestNSJSONSerialization {
264264
XCTAssertEqual(result?[1] as? Int, -1)
265265
XCTAssertEqual(result?[2] as? Double, 1.3)
266266
XCTAssertEqual(result?[3] as? Double, -1.3)
267-
XCTAssertEqual(result?[4] as? Double, 1000)
267+
XCTAssertEqual(result?[4] as? Int, 1000)
268268
XCTAssertEqual(result?[5] as? Double, 0.001)
269269
}
270270
} catch {
@@ -598,6 +598,7 @@ extension TestNSJSONSerialization {
598598
("test_invalidJsonObjectToStreamBuffer", test_invalidJsonObjectToStreamBuffer),
599599
("test_jsonObjectToOutputStreamInsufficeintBuffer", test_jsonObjectToOutputStreamInsufficeintBuffer),
600600
("test_booleanJSONObject", test_booleanJSONObject),
601+
("test_serializeDecimalNumberJSONObject", test_serializeDecimalNumberJSONObject),
601602
]
602603
}
603604

@@ -941,6 +942,21 @@ extension TestNSJSONSerialization {
941942
XCTAssertTrue(JSONSerialization.isValidJSONObject([true]))
942943
}
943944

945+
func test_serializeDecimalNumberJSONObject() {
946+
let decimalArray = "[12.1,10.0,0.0,0.0001,20]"
947+
do {
948+
let data = decimalArray.data(using: String.Encoding.utf8)
949+
let result = try JSONSerialization.jsonObject(with: data!, options: []) as? [Any]
950+
XCTAssertEqual(result?[0] as! Double,12.1)
951+
XCTAssertEqual(result?[1] as! Int,10)
952+
XCTAssertEqual(result?[2] as! Int, 0)
953+
XCTAssertEqual(result?[3] as! Double,0.0001)
954+
XCTAssertEqual(result?[4] as! Int,20)
955+
} catch {
956+
XCTFail("Failed during serialization")
957+
}
958+
}
959+
944960
private func createTestFile(_ path: String,_contents: Data) -> String? {
945961
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
946962
do {

0 commit comments

Comments
 (0)