Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. enum CodingKeys: String, CodingKey {
  2. case attributeCode = "attribute_code"
  3. case value = "value"
  4. }
  5.  
  6. init(from decoder: Decoder) throws {
  7. let container = try decoder.singleValueContainer()
  8. if let x = try? container.decode([String].self) {
  9. self = .stringArray(x)
  10. return
  11. }
  12. if let x = try? container.decode(String.self) {
  13. self = .string(x)
  14. return
  15. }
  16. throw DecodingError.typeMismatch(ValueUnion.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ValueUnion"))
  17. }
  18.  
  19. func encode(to encoder: Encoder) throws {
  20. var container = encoder.singleValueContainer()
  21. switch self {
  22. case .string(let x):
  23. try container.encode(x)
  24. case .stringArray(let x):
  25. try container.encode(x)
  26. }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement