Skip to content

Commit 38b7293

Browse files
committed
JSONSerialization: Update error message to match Darwin
1 parent a20d25f commit 38b7293

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Foundation/JSONSerialization.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ open class JSONSerialization : NSObject {
6262
return true
6363
}
6464

65-
// object is Swift.String, NSNull, Int, Bool, or UInt
6665
if obj is String || obj is NSNull || obj is Int || obj is Bool || obj is UInt ||
6766
obj is Int8 || obj is Int16 || obj is Int32 || obj is Int64 ||
6867
obj is UInt8 || obj is UInt16 || obj is UInt32 || obj is UInt64 {
@@ -435,8 +434,18 @@ private struct JSONWriter {
435434

436435
mutating func serializeNumber(_ num: NSNumber) throws {
437436
if CFNumberIsFloatType(num._cfObject) {
438-
if !num.doubleValue.isFinite {
439-
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "Number cannot be infinity or NaN"])
437+
let dv = num.doubleValue
438+
if !dv.isFinite {
439+
let value: String
440+
if dv.isNaN {
441+
value = "NaN"
442+
} else if dv.isInfinite {
443+
value = "infinite"
444+
} else {
445+
value = String(dv)
446+
}
447+
448+
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.propertyListReadCorrupt.rawValue, userInfo: ["NSDebugDescription" : "Invalid number value (\(value)) in JSON write"])
440449
}
441450

442451
let string = CFNumberFormatterCreateStringWithNumber(nil, _numberformatter, num._cfObject)._swiftObject

0 commit comments

Comments
 (0)