-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support non Foundation JSON coders * add a `PostgresJSONEncoder` protocol with an API that mirrors the Foundation `JSONEncoder`'s decoding function * add global `_defaultJSONEncoder` variable used in the JSON and JSONB type and that is defaulted to a Foundation `JSONEncoder` * add a `PostgresJSONDecoder` protocol with an API that mirrors the Foundation `JSONDecoder`'s decoding function * add global `_defaultJSONDecoder` variable used in the JSON and JSONB type and that is defaulted to a Foundation `JSONDecoder` * Add docblocks for _defaultJSON{encoder,decoder} and their respective unit tests
- Loading branch information
1 parent
dddb196
commit 3cf2496
Showing
5 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Foundation | ||
|
||
/// A protocol that mimmicks the Foundation `JSONDecoder.decode(_:from:)` function. | ||
/// Conform a non-Foundation JSON decoder to this protocol if you want PostgresNIO to be | ||
/// able to use it when decoding JSON & JSONB values (see `PostgresNIO._defaultJSONDecoder`) | ||
public protocol PostgresJSONDecoder { | ||
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable | ||
} | ||
|
||
extension JSONDecoder: PostgresJSONDecoder {} | ||
|
||
/// The default JSON decoder used by PostgresNIO when decoding JSON & JSONB values. | ||
/// As `_defaultJSONDecoder` will be reused for decoding all JSON & JSONB values | ||
/// from potentially multiple threads at once, you must ensure your custom JSON decoder is | ||
/// thread safe internally like `Foundation.JSONDecoder`. | ||
public var _defaultJSONDecoder: PostgresJSONDecoder = JSONDecoder() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Foundation | ||
|
||
/// A protocol that mimmicks the Foundation `JSONEncoder.encode(_:)` function. | ||
/// Conform a non-Foundation JSON encoder to this protocol if you want PostgresNIO to be | ||
/// able to use it when encoding JSON & JSONB values (see `PostgresNIO._defaultJSONEncoder`) | ||
public protocol PostgresJSONEncoder { | ||
func encode<T>(_ value: T) throws -> Data where T : Encodable | ||
} | ||
|
||
extension JSONEncoder: PostgresJSONEncoder {} | ||
|
||
/// The default JSON encoder used by PostgresNIO when encoding JSON & JSONB values. | ||
/// As `_defaultJSONEncoder` will be reused for encoding all JSON & JSONB values | ||
/// from potentially multiple threads at once, you must ensure your custom JSON encoder is | ||
/// thread safe internally like `Foundation.JSONEncoder`. | ||
public var _defaultJSONEncoder: PostgresJSONEncoder = JSONEncoder() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters