Skip to content

Commit a2aa24d

Browse files
Merge pull request #96 from benrimmington/complex-unkeyed
[Complex] Encode and decode using unkeyedContainer
2 parents 7bd2909 + eda4321 commit a2aa24d

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

Sources/Complex/Complex.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Numerics open source project
44
//
5-
// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors
5+
// Copyright (c) 2019 - 2020 Apple Inc. and the Swift Numerics project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -344,11 +344,23 @@ extension Complex: Hashable {
344344
}
345345

346346
// MARK: - Conformance to Codable
347-
// The synthesized conformance works correction for this protocol, unlike
348-
// Hashable and Equatable; all we need to do is specify that we conform.
349347
// FloatingPoint does not refine Codable, so this is a conditional conformance.
350-
extension Complex: Encodable where RealType: Encodable { }
351-
extension Complex: Decodable where RealType: Decodable { }
348+
extension Complex: Decodable where RealType: Decodable {
349+
public init(from decoder: Decoder) throws {
350+
var unkeyedContainer = try decoder.unkeyedContainer()
351+
let x = try unkeyedContainer.decode(RealType.self)
352+
let y = try unkeyedContainer.decode(RealType.self)
353+
self.init(x, y)
354+
}
355+
}
356+
357+
extension Complex: Encodable where RealType: Encodable {
358+
public func encode(to encoder: Encoder) throws {
359+
var unkeyedContainer = encoder.unkeyedContainer()
360+
try unkeyedContainer.encode(x)
361+
try unkeyedContainer.encode(y)
362+
}
363+
}
352364

353365
// MARK: - Formatting
354366
extension Complex: CustomStringConvertible {

Tests/ComplexTests/PropertyTests.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift Numerics open source project
44
//
5-
// Copyright (c) 2019 Apple Inc. and the Swift Numerics project authors
5+
// Copyright (c) 2019 - 2020 Apple Inc. and the Swift Numerics project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -102,4 +102,31 @@ final class PropertyTests: XCTestCase {
102102
testEquatableHashable(Float80.self)
103103
#endif
104104
}
105+
106+
func testCodable<T: Codable & Real>(_ type: T.Type) throws {
107+
let encoder = JSONEncoder()
108+
encoder.nonConformingFloatEncodingStrategy = .convertToString(
109+
positiveInfinity: "inf",
110+
negativeInfinity: "-inf",
111+
nan: "nan")
112+
113+
let decoder = JSONDecoder()
114+
decoder.nonConformingFloatDecodingStrategy = .convertFromString(
115+
positiveInfinity: "inf",
116+
negativeInfinity: "-inf",
117+
nan: "nan")
118+
119+
for expected: Complex<T> in [.zero, .one, .i, .infinity] {
120+
let data = try encoder.encode(expected)
121+
// print("*** \(String(decoding: data, as: Unicode.UTF8.self)) ***")
122+
let actual = try decoder.decode(Complex<T>.self, from: data)
123+
XCTAssertEqual(actual, expected)
124+
}
125+
}
126+
127+
func testCodable() throws {
128+
try testCodable(Float32.self)
129+
try testCodable(Float64.self)
130+
// Float80 doesn't conform to Codable.
131+
}
105132
}

0 commit comments

Comments
 (0)