Codable enum's associated value is unexpectedly using positional name

I wonder if we could have something like this (pseudocode):

enum Response: String, Codable {
    case error(ErrorRecord) = "Error"
    case hello(HelloRecord) // only specified when needed
    case list(ListRecord) = "List"
}

I know it looks a bit weird with associated values.

Or this:

enum Response: Codable {
    case error(ErrorRecord) #jsonname("Error")
    case hello(HelloRecord) // only specified when needed
    case list(ListRecord)   #jsonname("List")
}

Or perhaps we should have this:

decoder.keyDecodingStrategy = .convertFromPascalCase

Personally I'd talk to whoever makes this JSON and ask them nicely to change the case of their keys.

Oh, yeah, I slipped on that one. default case handling is now removed above.

Someone already implemented convertFromPascalCase decoding strategy.

Unfortunately the schema is a little loopy, to be charitable. It's also set in stone because I'm implementing the protocol of another application. There are a lot of cases I have Bool? types because it interchangeably uses false and null, passing doubles and strings in the same field, or worse, a case where it can be a boolean or the string "null" (I wish I was joking...). I have to do manual decoding a lot for these situations. Realistically, these are bugs in the server implementation, but one no one really noticed because the client and server are Python.

Good tip on the key decoding strategy - thankfully it only uses it though on the root value.

I just wanted to point out that the reason why this case is encoded to

{"List": {"_0": /* my [String: [String: ListUser]} */}}

is not because it has only one argument, but because the argument is not named. This

case list(rooms: [String: [String: ListUser]])

should give you this instead:

{"List": {"rooms": /* my [String: [String: ListUser]} */}}

If you have control over the JSON to be decoded, it might be easier to add the extra rooms key than implementing a custom Response initializer.