Skip to content

Commit 3ac4c8c

Browse files
committed
Prioritize Bool in AnyCodable encode/decode
Reorder type checks in AnyCodable's Decodable/Encodable implementation to test Bool before Int/Double. This ensures boolean values are correctly decoded and encoded (avoiding misinterpretation as numeric types, e.g. via NSNumber bridging). Changes made in Sources/Commons/AnyCodable.swift.
1 parent 3117833 commit 3ac4c8c

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

Sources/Commons/AnyCodable.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ extension AnyCodable: Decodable, Encodable {
161161
}
162162
value = result
163163
} else if let container = try? decoder.singleValueContainer() {
164-
if let intVal = try? container.decode(Int.self) {
164+
if let boolVal = try? container.decode(Bool.self) {
165+
value = boolVal
166+
} else if let intVal = try? container.decode(Int.self) {
165167
value = intVal
166168
} else if let doubleVal = try? container.decode(Double.self) {
167169
value = doubleVal
168-
} else if let boolVal = try? container.decode(Bool.self) {
169-
value = boolVal
170170
} else if let stringVal = try? container.decode(String.self) {
171171
value = stringVal
172172
} else if container.decodeNil() {
@@ -199,12 +199,12 @@ extension AnyCodable: Decodable, Encodable {
199199
// ignoring that key
200200
} else {
201201
var container = encoder.singleValueContainer()
202-
if let intVal = value as? Int {
202+
if let boolVal = value as? Bool {
203+
try container.encode(boolVal)
204+
} else if let intVal = value as? Int {
203205
try container.encode(intVal)
204206
} else if let doubleVal = value as? Double {
205207
try container.encode(doubleVal)
206-
} else if let boolVal = value as? Bool {
207-
try container.encode(boolVal)
208208
} else if let stringVal = value as? String {
209209
try container.encode(stringVal)
210210
} else {

0 commit comments

Comments
 (0)