Skip to content

Commit 193217d

Browse files
stephencelismluisbrown
authored andcommitted
Fix BindableState: Codable strategies (#781)
1 parent 348c4ca commit 193217d

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Sources/ComposableArchitecture/SwiftUI/Binding.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,23 @@
205205

206206
extension BindableState: Decodable where Value: Decodable {
207207
public init(from decoder: Decoder) throws {
208-
self.init(wrappedValue: try Value(from: decoder))
208+
do {
209+
let container = try decoder.singleValueContainer()
210+
self.init(wrappedValue: try container.decode(Value.self))
211+
} catch {
212+
self.init(wrappedValue: try Value(from: decoder))
213+
}
209214
}
210215
}
211216

212217
extension BindableState: Encodable where Value: Encodable {
213218
public func encode(to encoder: Encoder) throws {
214-
try self.wrappedValue.encode(to: encoder)
219+
do {
220+
var container = encoder.singleValueContainer()
221+
try container.encode(self.wrappedValue)
222+
} catch {
223+
try self.wrappedValue.encode(to: encoder)
224+
}
215225
}
216226
}
217227

0 commit comments

Comments
 (0)