diff --git a/Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift b/Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift index d416267b..646b3f98 100644 --- a/Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift +++ b/Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift @@ -31,16 +31,17 @@ struct PostgreSQLDataDecoder { func container(keyedBy type: Key.Type) throws -> KeyedDecodingContainer where Key : CodingKey { let json: Data - switch data.type { - case .jsonb, .json: - switch data.storage { - case .binary(let data): - assert(data[data.startIndex] == 0x01, "invalid JSONB data format") - json = data.advanced(by: 1) - case .text(let string): json = Data(string.utf8) - default: throw PostgreSQLError.decode(JSON.self, from: data) - } - default: throw PostgreSQLError.decode(JSON.self, from: data) + switch (data.type, data.storage) { + case (.jsonb, .binary(let data)): + assert(data[data.startIndex] == 0x01, "invalid JSONB data format") + json = data.advanced(by: 1) + case (.json, .binary(let data)): + json = data + case (.jsonb, .text(let string)), + (.json, .text(let string)): + json = Data(string.utf8) + default: + throw PostgreSQLError.decode(JSON.self, from: data) } let unwrapper = try JSONDecoder().decode(DecoderUnwrapper.self, from: json) return try unwrapper.decoder.container(keyedBy: Key.self)