Skip to content

Commit e36bede

Browse files
committed
ASCIICharacter: Updated Codable to encode to a character instead of ASCII value
1 parent 1b1f3ea commit e36bede

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

Sources/SwiftASCII/ASCIICharacter.swift

+5-11
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,11 @@ extension ASCIICharacter: Equatable {
188188

189189
extension ASCIICharacter: Codable {
190190

191-
enum CodingKeys: String, CodingKey {
192-
193-
case asciiValue
194-
195-
}
196-
197191
public init(from decoder: Decoder) throws {
198192

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 {
193+
let container = try decoder.singleValueContainer()
194+
let string = try container.decode(String.self)
195+
guard let newInstance = Self(exactly: string) else {
202196
throw DecodingError.dataCorrupted(
203197
.init(codingPath: container.codingPath,
204198
debugDescription: "Value was not valid ASCII character number.")
@@ -210,9 +204,9 @@ extension ASCIICharacter: Codable {
210204

211205
public func encode(to encoder: Encoder) throws {
212206

213-
var container = encoder.container(keyedBy: CodingKeys.self)
207+
var container = encoder.singleValueContainer()
214208

215-
try container.encode(asciiValue, forKey: .asciiValue)
209+
try container.encode(String(characterValue))
216210

217211
}
218212

0 commit comments

Comments
 (0)