Skip to content

Commit

Permalink
ASCIICharacter: Updated Codable to encode to a character instead …
Browse files Browse the repository at this point in the history
…of ASCII value
  • Loading branch information
orchetect committed Jul 24, 2022
1 parent 1b1f3ea commit e36bede
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions Sources/SwiftASCII/ASCIICharacter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,11 @@ extension ASCIICharacter: Equatable {

extension ASCIICharacter: Codable {

enum CodingKeys: String, CodingKey {

case asciiValue

}

public init(from decoder: Decoder) throws {

let container = try decoder.container(keyedBy: CodingKeys.self)
let asciiValue = try container.decode(UInt8.self, forKey: .asciiValue)
guard let newInstance = Self(asciiValue) else {
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self)
guard let newInstance = Self(exactly: string) else {
throw DecodingError.dataCorrupted(
.init(codingPath: container.codingPath,
debugDescription: "Value was not valid ASCII character number.")
Expand All @@ -210,9 +204,9 @@ extension ASCIICharacter: Codable {

public func encode(to encoder: Encoder) throws {

var container = encoder.container(keyedBy: CodingKeys.self)
var container = encoder.singleValueContainer()

try container.encode(asciiValue, forKey: .asciiValue)
try container.encode(String(characterValue))

}

Expand Down

0 comments on commit e36bede

Please sign in to comment.