Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amomchilov/053e120553c8af42f2687288f2a47eb3 to your computer and use it in GitHub Desktop.
Save amomchilov/053e120553c8af42f2687288f2a47eb3 to your computer and use it in GitHub Desktop.
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