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

[SR-7498] Decoder needs containerKeyedWithUnknownKeys #50041

Open
mattneub opened this issue Apr 22, 2018 · 2 comments
Open

[SR-7498] Decoder needs containerKeyedWithUnknownKeys #50041

mattneub opened this issue Apr 22, 2018 · 2 comments
Labels
Codable Area → standard library: `Codable` and co. improvement standard library Area: Standard library umbrella

Comments

@mattneub
Copy link

Previous ID SR-7498
Radar None
Original Reporter @mattneub
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Improvement, Codable
Assignee None
Priority Medium

md5: 7ce81accf23b6883abf11011686d9146

Issue 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.

@hamishknight
Copy link
Collaborator

Personally I think this would be best solved by adding an AnyCodingKey type to the standard library, which would look something like this (I was going to make a Swift Evolution pitch but never got round to it):

struct AnyCodingKey : CodingKey {
  var stringValue: String
  var intValue: Int?

  init(_ codingKey: CodingKey) {
    self.stringValue = codingKey.stringValue
    self.intValue = codingKey.intValue
  }

  init(stringValue: String) {
    self.stringValue = stringValue
    self.intValue = nil
  }

  init(intValue: Int) {
    self.stringValue = String(intValue)
    self.intValue = intValue
  }
}

Then you'd be able to ask for containers keyed by AnyCodingKey.self. Actually the original motivation for me wanting this was when using the .custom key strategy for JSONEncoder/JSONDecoder – it requires a closure that returns a CodingKey, so you have to manually build your own CodingKey wrapper, which feels pretty sub-optimal.

Adding AnyCodingKey would certainly reduce boilerplate in both cases.

@mattneub
Copy link
Author

Brilliant point, thanks @hamishknight

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Codable Area → standard library: `Codable` and co. improvement standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants