Skip to content

Commit 367c120

Browse files
authoredMar 30, 2020
Fix assertion while decoding of JSON (non-JSONB) (vapor#177)
1 parent 69ccf32 commit 367c120

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed
 

‎Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ struct PostgreSQLDataDecoder {
3131

3232
func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
3333
let json: Data
34-
switch data.type {
35-
case .jsonb, .json:
36-
switch data.storage {
37-
case .binary(let data):
38-
assert(data[data.startIndex] == 0x01, "invalid JSONB data format")
39-
json = data.advanced(by: 1)
40-
case .text(let string): json = Data(string.utf8)
41-
default: throw PostgreSQLError.decode(JSON.self, from: data)
42-
}
43-
default: throw PostgreSQLError.decode(JSON.self, from: data)
34+
switch (data.type, data.storage) {
35+
case (.jsonb, .binary(let data)):
36+
assert(data[data.startIndex] == 0x01, "invalid JSONB data format")
37+
json = data.advanced(by: 1)
38+
case (.json, .binary(let data)):
39+
json = data
40+
case (.jsonb, .text(let string)),
41+
(.json, .text(let string)):
42+
json = Data(string.utf8)
43+
default:
44+
throw PostgreSQLError.decode(JSON.self, from: data)
4445
}
4546
let unwrapper = try JSONDecoder().decode(DecoderUnwrapper.self, from: json)
4647
return try unwrapper.decoder.container(keyedBy: Key.self)

0 commit comments

Comments
 (0)
Please sign in to comment.