diff --git a/Sources/Complex/Complex.swift b/Sources/Complex/Complex.swift index f888014c..00fe5ca0 100644 --- a/Sources/Complex/Complex.swift +++ b/Sources/Complex/Complex.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift Numerics open source project // -// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +// Copyright (c) 2019 - 2020 Apple Inc. and the Swift Numerics project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information @@ -344,11 +344,23 @@ extension Complex: Hashable { } // MARK: - Conformance to Codable -// The synthesized conformance works correction for this protocol, unlike -// Hashable and Equatable; all we need to do is specify that we conform. // FloatingPoint does not refine Codable, so this is a conditional conformance. -extension Complex: Encodable where RealType: Encodable { } -extension Complex: Decodable where RealType: Decodable { } +extension Complex: Decodable where RealType: Decodable { + public init(from decoder: Decoder) throws { + var unkeyedContainer = try decoder.unkeyedContainer() + let x = try unkeyedContainer.decode(RealType.self) + let y = try unkeyedContainer.decode(RealType.self) + self.init(x, y) + } +} + +extension Complex: Encodable where RealType: Encodable { + public func encode(to encoder: Encoder) throws { + var unkeyedContainer = encoder.unkeyedContainer() + try unkeyedContainer.encode(x) + try unkeyedContainer.encode(y) + } +} // MARK: - Formatting extension Complex: CustomStringConvertible { diff --git a/Tests/ComplexTests/PropertyTests.swift b/Tests/ComplexTests/PropertyTests.swift index 475fd279..6c21791a 100644 --- a/Tests/ComplexTests/PropertyTests.swift +++ b/Tests/ComplexTests/PropertyTests.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift Numerics open source project // -// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors +// Copyright (c) 2019 - 2020 Apple Inc. and the Swift Numerics project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information @@ -102,4 +102,31 @@ final class PropertyTests: XCTestCase { testEquatableHashable(Float80.self) #endif } + + func testCodable(_ type: T.Type) throws { + let encoder = JSONEncoder() + encoder.nonConformingFloatEncodingStrategy = .convertToString( + positiveInfinity: "inf", + negativeInfinity: "-inf", + nan: "nan") + + let decoder = JSONDecoder() + decoder.nonConformingFloatDecodingStrategy = .convertFromString( + positiveInfinity: "inf", + negativeInfinity: "-inf", + nan: "nan") + + for expected: Complex in [.zero, .one, .i, .infinity] { + let data = try encoder.encode(expected) + // print("*** \(String(decoding: data, as: Unicode.UTF8.self)) ***") + let actual = try decoder.decode(Complex.self, from: data) + XCTAssertEqual(actual, expected) + } + } + + func testCodable() throws { + try testCodable(Float32.self) + try testCodable(Float64.self) + // Float80 doesn't conform to Codable. + } }