Skip to content

[SR-6629] JSONEncoder with convertToSnakeCase encodes myURLProperty into my_url_property, but JSONDecoder with convertFromSnakeCase does not decode myURLProperty from my_url_property. #3752

Description

@norio-nomura
Previous ID SR-6629
Radar rdar://problem/41038908
Original Reporter @norio-nomura
Type Bug
Environment

swift-4.1-DEVELOPMENT-SNAPSHOT-2017-12-15-a

Additional Detail from JIRA
Votes 1
Component/s Foundation
Labels Bug, Codable
Assignee @itaiferber
Priority Medium

md5: fee6142a597261d7125a4f80b8b8ceb2

Issue Description:

Because JSONDecoder coverts my_url_property to myUrlProperty.

import Foundation

struct S: Codable {
    var myURLProperty: String
}

let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
let data = try encoder.encode(S(myURLProperty: "property"))
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
do {
    let decoded = try decoder.decode(S.self, from: data)
} catch {
    print(error)
// keyNotFound(CodingKeys(stringValue: "myURLProperty", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"myURLProperty\", intValue: nil) (\"myURLProperty\"), converted to my_url_property.", underlyingError: nil))
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions