Skip to content

Commit

Permalink
🐛 Fix serialization of NSNumber values that are not actually booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
iujames committed Sep 26, 2022
1 parent 52e29fd commit e38f4ad
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Sources/AppcuesKit/Data/Networking/DynamicCodingKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ extension KeyedEncodingContainer where K == DynamicCodingKeys {
mutating func encodeSkippingInvalid(_ dict: [String: Any]?) throws {
var encodingErrorKeys: [String] = []

// swiftlint:disable:next closure_body_length
try dict?.forEach { key, value in
let codingKey = DynamicCodingKeys(key: key)

Expand All @@ -50,11 +51,15 @@ extension KeyedEncodingContainer where K == DynamicCodingKeys {
try self.encode(string, forKey: codingKey)
case let url as URL:
try self.encode(url.absoluteString, forKey: codingKey)
case let bool as Bool:
try self.encode(bool, forKey: codingKey)
// swiftlint:disable:next legacy_objc_type
case let number as NSNumber:
try self.encode(number.decimalValue, forKey: codingKey)
if isBoolNumber(number), let bool = number as? Bool {
try self.encode(bool, forKey: codingKey)
} else {
try self.encode(number.decimalValue, forKey: codingKey)
}
case let bool as Bool:
try self.encode(bool, forKey: codingKey)
case let date as Date:
try self.encode(date, forKey: codingKey)
default:
Expand All @@ -72,4 +77,12 @@ extension KeyedEncodingContainer where K == DynamicCodingKeys {
)
}
}

// helper to determine if an NSNumber is actually containing a Boolean value
// swiftlint:disable:next legacy_objc_type
private func isBoolNumber(_ num: NSNumber) -> Bool {
let boolID = CFBooleanGetTypeID() // the type ID of CFBoolean
let numID = CFGetTypeID(num) // the type ID of num
return numID == boolID
}
}

0 comments on commit e38f4ad

Please sign in to comment.