Created
May 2, 2020 03:43
-
-
Save amomchilov/053e120553c8af42f2687288f2a47eb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct S<T> { | |
let elements: [T] | |
} | |
extension S: Decodable where T: Decodable { | |
init(from decoder: Decoder) throws { | |
let container = try decoder.unkeyedContainer() | |
let s = sequence(state: container, next: { | |
$0.isAtEnd ? nil : try? $0.decode(T.self) | |
}) | |
self.init(elements: Array(s)) | |
} | |
} | |
let json = """ | |
[1, 2, 3, 4, 5, 6, 7, 8, 9] | |
""".data(using: .utf8)! | |
let result = try JSONDecoder().decode(S<Int>.self, from: json) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment