From 4c0cddcd733f36de0243266d3e72a5a24b4ac5fe Mon Sep 17 00:00:00 2001 From: Vasily Ulianov Date: Fri, 18 May 2018 20:02:05 +0300 Subject: [PATCH] Add JSONB version to PostgreSQLJSONCustomConvertible Fix bug "unsupported jsonb version number 123" --- .../Data/PostgreSQLJSONCustomConvertible.swift | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Sources/PostgreSQL/Data/PostgreSQLJSONCustomConvertible.swift b/Sources/PostgreSQL/Data/PostgreSQLJSONCustomConvertible.swift index d1e134dd..e7c7fe34 100644 --- a/Sources/PostgreSQL/Data/PostgreSQLJSONCustomConvertible.swift +++ b/Sources/PostgreSQL/Data/PostgreSQLJSONCustomConvertible.swift @@ -42,10 +42,15 @@ extension PostgreSQLJSONCustomConvertible { guard let encodable = self as? Encodable else { fatalError("`\(Self.self)` is not `Encodable`.") } - return try PostgreSQLData( + + // JSONB requires version number in a first byte + let jsonBVersion: [UInt8] = [0x01] + let jsonData = try JSONEncoder().encode(EncoderWrapper(encodable)) + + return PostgreSQLData( type: .jsonb, - format: .text, - data: JSONEncoder().encode(EncoderWrapper(encodable)) + format: .binary, + data: jsonBVersion + jsonData ) } }