Skip to content

Commit 5c24901

Browse files
committed
Avoid string based encoding and resort back to high and low bit encoding/decoding but as unkeyed
1 parent 294290f commit 5c24901

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

stdlib/public/core/Duration.swift

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,33 +96,19 @@ extension Duration {
9696

9797
@available(SwiftStdlib 5.7, *)
9898
extension Duration: Codable {
99-
private enum CodingKeys: String, CodingKey {
100-
case attoseconds = "attoseconds"
101-
}
102-
103-
private enum DecodingFailure: Error, CustomStringConvertible {
104-
case conversionFailure(String)
105-
106-
var description: String {
107-
switch self {
108-
case .conversionFailure(let str):
109-
return "Unable to convert \(str) to Int128"
110-
}
111-
}
112-
}
113-
99+
@available(SwiftStdlib 5.7, *)
114100
public init(from decoder: Decoder) throws {
115-
let container = try decoder.container(keyedBy: CodingKeys.self)
116-
let str = try container.decode(String.self, forKey: .attoseconds)
117-
guard let attoseconds = _Int128(str, radix: 10) else {
118-
throw DecodingFailure.conversionFailure(str)
119-
}
120-
self.init(_attoseconds: attoseconds)
101+
let container = try decoder.unkeyedContainer()
102+
let high = container.decode(Int64.self)
103+
let low = container.decode(UInt64.self)
104+
self.init(_attoseconds: _Int128(high: high, low: low))
121105
}
122106

107+
@available(SwiftStdlib 5.7, *)
123108
public func encode(to encoder: Encoder) throws {
124-
var container = encoder.container(keyedBy: CodingKeys.self)
125-
try container.encode(String(_attoseconds, radix: 10), forKey: .attoseconds)
109+
var container = encoder.unkeyedContainer()
110+
try container.encode(_high)
111+
try container.encode(_low)
126112
}
127113
}
128114

0 commit comments

Comments
 (0)