Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum CodingKeys: String, CodingKey {
- case attributeCode = "attribute_code"
- case value = "value"
- }
- init(from decoder: Decoder) throws {
- let container = try decoder.singleValueContainer()
- if let x = try? container.decode([String].self) {
- self = .stringArray(x)
- return
- }
- if let x = try? container.decode(String.self) {
- self = .string(x)
- return
- }
- throw DecodingError.typeMismatch(ValueUnion.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ValueUnion"))
- }
- func encode(to encoder: Encoder) throws {
- var container = encoder.singleValueContainer()
- switch self {
- case .string(let x):
- try container.encode(x)
- case .stringArray(let x):
- try container.encode(x)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement