Skip to content

Commit

Permalink
Fix assertion while decoding of JSON (non-JSONB) (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffried authored Mar 30, 2020
1 parent 69ccf32 commit 367c120
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ struct PostgreSQLDataDecoder {

func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> 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)
Expand Down

0 comments on commit 367c120

Please sign in to comment.