I am retrieving a complex nested object from my JSON REST API.
DocumentDraft
- uuid: String
- schema: Schema // Very complicated object with many variations
- url: String
- values: [Value]
- successors: [String]
- predecessors: [String]
Value
- key: String
- val: String? OR [String]? // <-- This is the problem
I suppose the proper way to deal with this is to introduce a generic type.
struct Value<V: Decodable>: Decodable {
let key: String
let val: V?
}
... but even so, values
could be a mixed array, so I do not see how declaring what V
is would help.
But then, of course the generic type propagates all the way up the hierarchy, to the DocumentDraft
object, to the publisher, to my API calls, etc. polluting the entire chain of otherwise very clean and readable calls and objects. I would like to deal with this only on the level of Value
, and let the JSONDecoder simply return one of the two somehow.
Is there another way to deal with the two possibilities of the optional val
as either String
or [String]
without changing the entire parent object?