Uploaded image for project: 'Swift'
  1. Swift
  2. SR-6629

JSONEncoder with convertToSnakeCase encodes myURLProperty into my_url_property, but JSONDecoder with convertFromSnakeCase does not decode myURLProperty from my_url_property.

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Foundation
    • Labels:
    • Environment:

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

      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))
      }
      

        Attachments

          Activity

            People

            • Assignee:
              itaiferber Itai Ferber
              Reporter:
              norio_nomura Norio Nomura
            • Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated: