@@ -96,33 +96,19 @@ extension Duration {
96
96
97
97
@available ( SwiftStdlib 5 . 7 , * )
98
98
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 , * )
114
100
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) )
121
105
}
122
106
107
+ @available ( SwiftStdlib 5 . 7 , * )
123
108
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)
126
112
}
127
113
}
128
114
0 commit comments