From 4db11acb927cb017abb98b2aa8c90642f70d38ae Mon Sep 17 00:00:00 2001 From: Chris Brandow Date: Tue, 24 Jul 2018 14:47:22 -0700 Subject: [PATCH] Add `Codable` Conformance This was done quickly and seems to work, though I haven't added any tests. --- NonEmptyArray/NonEmptyArray.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/NonEmptyArray/NonEmptyArray.swift b/NonEmptyArray/NonEmptyArray.swift index d96f8f8..dc7fcad 100644 --- a/NonEmptyArray/NonEmptyArray.swift +++ b/NonEmptyArray/NonEmptyArray.swift @@ -130,6 +130,19 @@ extension NonEmptyArray: CustomDebugStringConvertible { } } +extension NonEmptyArray: Codable where Element : Codable { + + public init(from decoder: Decoder) throws { + let values = try decoder.container(keyedBy: CodingKeys.self) + let array: [Element] = try values.decode([Element].self, forKey: .elements) + self.elements = array + } + + private enum CodingKeys: String, CodingKey { + case elements + } +} + extension NonEmptyArray: Collection { public typealias Index = Int