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

Decoder needs `containerKeyedWithUnknownKeys`

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Standard Library
    • Labels:

      Description

      Currently, if the keys to my JSON dictionary are unknown in advance, I have to write a "mop-up" CodingKey adopter struct, like this:

       

              struct Top : Decodable {
                  init(from decoder: Decoder) throws {
                      struct CK : CodingKey {
                          var stringValue: String
                          init?(stringValue: String) {
                              self.stringValue = stringValue
                          }
                          var intValue: Int?
                          init?(intValue: Int) {
                              return nil
                          }
                      }
                      let con = try! decoder.container(keyedBy: CK.self)
                      for key in con.allKeys {
                          // find out what the keys are, decode the values, deal with it
                      }
                  }
              }
      

       This is boilerplate so it shouldn't be necessary. Decoder should provide a method e.g. `containerKeyedWithUnknownKeys` that does the work for me.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              mattneub Matt Neuburg
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: