Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONEncoder hasn't implemented nestedContainer/nestedUnkeyedContainer #19

Closed
adam-fowler opened this issue Mar 19, 2020 · 0 comments · Fixed by #24
Closed

JSONEncoder hasn't implemented nestedContainer/nestedUnkeyedContainer #19

adam-fowler opened this issue Mar 19, 2020 · 0 comments · Fixed by #24
Labels
bug Something isn't working
Milestone

Comments

@adam-fowler
Copy link

The following code produces a Fatal error: unimplemented error when container.nestedContainer is called. Looking at the code I can see also nestedUnkeyedContainer isn't implemented.

import PureSwiftJSONCoding // fabianfett/pure-swift-json

struct Object: Encodable {
    let firstName: String
    let surname: String

    init(firstName: String, surname: String) {
        self.firstName = firstName
        self.surname = surname
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        var nameContainer = container.nestedContainer(keyedBy: NameCodingKeys.self, forKey: .name)
        try nameContainer.encode(firstName, forKey: .firstName)
        try nameContainer.encode(surname, forKey: .surname)
    }

    private enum CodingKeys: String, CodingKey {
        case name = "name"
    }

    private enum NameCodingKeys: String, CodingKey {
        case firstName = "firstName"
        case surname = "surname"
    }
}
let object = Object(firstName: "Adam", surname: "Fowler")
let json = try JSONEncoder().encode(object)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants