File tree Expand file tree Collapse file tree 2 files changed +11
-17
lines changed Expand file tree Collapse file tree 2 files changed +11
-17
lines changed Original file line number Diff line number Diff line change @@ -1615,27 +1615,15 @@ extension _JSONDecoder {
1615
1615
guard let value = value else { return nil }
1616
1616
guard !( value is NSNull ) else { return nil }
1617
1617
1618
- // if let number = value as? NSNumber {
1619
- // // TODO: Add a flag to coerce non-boolean numbers into Bools?
1620
- // if number === kCFBooleanTrue as NSNumber {
1621
- // return true
1622
- // } else if number === kCFBooleanFalse as NSNumber {
1623
- // return false
1624
- // }
1625
- //
1626
- // /* FIXME: If swift-corelibs-foundation doesn't change to use NSNumber, this code path will need to be included and tested:
1627
- // } else if let bool = value as? Bool {
1628
- // return bool
1629
- // */
1630
- //
1631
- // }
1632
- //
1633
- // throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
1634
-
1635
1618
guard let number = value as? NSNumber else {
1636
1619
throw DecodingError . _typeMismatch ( at: self . codingPath, expectation: type, reality: value)
1637
1620
}
1638
1621
1622
+ // TODO: Add a flag to coerce non-boolean numbers into Bools?
1623
+ guard number. _objCType == . Bool else {
1624
+ throw DecodingError . _typeMismatch ( at: self . codingPath, expectation: type, reality: value)
1625
+ }
1626
+
1639
1627
let bool = number. boolValue
1640
1628
guard NSNumber ( value: bool) == number else {
1641
1629
throw DecodingError . dataCorrupted ( DecodingError . Context ( codingPath: self . codingPath, debugDescription: " Parsed JSON number < \( number) > does not fit in \( type) . " ) )
Original file line number Diff line number Diff line change @@ -321,6 +321,12 @@ class TestJSONEncoder : XCTestCase {
321
321
// MARK: - Test encoding and decoding of built-in Codable types
322
322
func test_codingOfBool( ) {
323
323
test_codingOf ( value: Bool ( true ) , toAndFrom: " true " )
324
+ test_codingOf ( value: Bool ( false ) , toAndFrom: " false " )
325
+
326
+ do {
327
+ _ = try JSONDecoder ( ) . decode ( [ Bool ] . self, from: " [1] " . data ( using: . utf8) !)
328
+ XCTFail ( " Coercing non-boolean numbers into Bools was expected to fail " )
329
+ } catch { }
324
330
}
325
331
326
332
func test_codingOfInt8( ) {
You can’t perform that action at this time.
0 commit comments