File tree 4 files changed +85
-0
lines changed
4 files changed +85
-0
lines changed Original file line number Diff line number Diff line change @@ -186,6 +186,38 @@ extension ASCIICharacter: Equatable {
186
186
187
187
}
188
188
189
+ extension ASCIICharacter : Codable {
190
+
191
+ enum CodingKeys : String , CodingKey {
192
+
193
+ case asciiValue
194
+
195
+ }
196
+
197
+ public init ( from decoder: Decoder ) throws {
198
+
199
+ let container = try decoder. container ( keyedBy: CodingKeys . self)
200
+ let asciiValue = try container. decode ( UInt8 . self, forKey: . asciiValue)
201
+ guard let newInstance = Self ( asciiValue) else {
202
+ throw DecodingError . dataCorrupted (
203
+ . init( codingPath: container. codingPath,
204
+ debugDescription: " Value was not valid ASCII character number. " )
205
+ )
206
+ }
207
+ self = newInstance
208
+
209
+ }
210
+
211
+ public func encode( to encoder: Encoder ) throws {
212
+
213
+ var container = encoder. container ( keyedBy: CodingKeys . self)
214
+
215
+ try container. encode ( asciiValue, forKey: . asciiValue)
216
+
217
+ }
218
+
219
+ }
220
+
189
221
extension ASCIICharacter {
190
222
191
223
public static func + ( lhs: ASCIICharacter , rhs: ASCIICharacter ) -> ASCIIString {
Original file line number Diff line number Diff line change @@ -147,6 +147,31 @@ extension ASCIIString: Equatable {
147
147
148
148
}
149
149
150
+ extension ASCIIString : Codable {
151
+
152
+ public init ( from decoder: Decoder ) throws {
153
+
154
+ let container = try decoder. singleValueContainer ( )
155
+ let stringValue = try container. decode ( String . self)
156
+ guard let newInstance = Self ( exactly: stringValue) else {
157
+ throw DecodingError . dataCorrupted (
158
+ . init( codingPath: container. codingPath,
159
+ debugDescription: " Encoded string is not a valid ASCII string. " )
160
+ )
161
+ }
162
+ self = newInstance
163
+
164
+ }
165
+
166
+ public func encode( to encoder: Encoder ) throws {
167
+
168
+ var container = encoder. singleValueContainer ( )
169
+ try container. encode ( stringValue)
170
+
171
+ }
172
+
173
+ }
174
+
150
175
extension ASCIIString {
151
176
152
177
public static func + ( lhs: ASCIIString , rhs: ASCIIString ) -> ASCIIString {
Original file line number Diff line number Diff line change @@ -129,6 +129,20 @@ class ASCIICharacterTests: XCTestCase {
129
129
130
130
}
131
131
132
+ func testCodable( ) throws {
133
+
134
+ let encoder = JSONEncoder ( )
135
+ let decoder = JSONDecoder ( )
136
+
137
+ let str = ASCIICharacter ( " A " )
138
+
139
+ let encoded = try encoder. encode ( str)
140
+ let decoded = try decoder. decode ( ASCIICharacter . self, from: encoded)
141
+
142
+ XCTAssertEqual ( str, decoded)
143
+
144
+ }
145
+
132
146
func testStaticInits( ) {
133
147
134
148
let str : ASCIICharacter = . lossy( " 😃 " )
Original file line number Diff line number Diff line change @@ -96,6 +96,20 @@ class ASCIIStringTests: XCTestCase {
96
96
97
97
}
98
98
99
+ func testCodable( ) throws {
100
+
101
+ let encoder = JSONEncoder ( )
102
+ let decoder = JSONDecoder ( )
103
+
104
+ let str = ASCIIString ( " A string " )
105
+
106
+ let encoded = try encoder. encode ( str)
107
+ let decoded = try decoder. decode ( ASCIIString . self, from: encoded)
108
+
109
+ XCTAssertEqual ( str, decoded)
110
+
111
+ }
112
+
99
113
func testStaticInits( ) {
100
114
101
115
let str : ASCIIString = . lossy( " Emöji 😃 " )
You can’t perform that action at this time.
0 commit comments