Skip to content
New issue

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

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

Already on GitHub? # to your account

JSONEncoder.codingPath stack is not updated #21

Closed
adam-fowler opened this issue Mar 19, 2020 · 1 comment · Fixed by #25
Closed

JSONEncoder.codingPath stack is not updated #21

adam-fowler opened this issue Mar 19, 2020 · 1 comment · Fixed by #25
Labels
bug Something isn't working
Milestone

Comments

@adam-fowler
Copy link

The coding path stack is not updated as you create/delete containers.

@adam-fowler
Copy link
Author

The following outputs {"sub":{}} when it should output {"sub":{"key":"sub","value":12}}. The encoders are expected to keep the codingPath up to date but the use cases are fairly obscure so this isn't a huge issue.

struct SubObject: Encodable {
    let value: Int

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        if let key = encoder.codingPath.last {
            try container.encode(key.stringValue, forKey: .key)
            try container.encode(value, forKey: .value)
        }
    }

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

struct Object: Encodable {
    let sub: SubObject
}

let o = Object(sub: SubObject(value: 12))
let json = try PureSwiftJSONCoding.JSONEncoder().encode(o)
print(String(decoding: json, as: Unicode.UTF8.self))

@fabianfett fabianfett added the bug Something isn't working label Mar 20, 2020
@fabianfett fabianfett added this to the v0.2.0 milestone Mar 20, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants