Skip to content

Commit

Permalink
Use PostgresNIO's default JSON coders (#195)
Browse files Browse the repository at this point in the history
* Use postgres-nio's `_defaultJSONEncoder` and `_defaultJSONDecoder` global variables as the default values for the JSON coders in `PostgresDataEncoder` and `PostgresDataDecoder`

* Deprecate the previous "jsonDecoder" variable of PostgresDataDecoder and rename it to "json"

* Fix spacing slightly in PostgresDataDecoder

* Fix spacing in PostgresDataDecoder
  • Loading branch information
jordanebelanger authored Sep 9, 2020
1 parent 806a513 commit cbbe3ef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import class Foundation.JSONDecoder

extension PostgresDataDecoder {
@available(*, deprecated, renamed: "json")
public var jsonDecoder: JSONDecoder {
return self.json as! JSONDecoder
}
}
18 changes: 10 additions & 8 deletions Sources/PostgresKit/PostgresDataDecoder.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import Foundation
import protocol PostgresNIO.PostgresJSONDecoder
import var PostgresNIO._defaultJSONDecoder

public final class PostgresDataDecoder {
public let jsonDecoder: JSONDecoder
public let json: PostgresNIO.PostgresJSONDecoder

public init(json: JSONDecoder = JSONDecoder()) {
self.jsonDecoder = json
public init(json: PostgresNIO.PostgresJSONDecoder = PostgresNIO._defaultJSONDecoder) {
self.json = json
}

public func decode<T>(_ type: T.Type, from data: PostgresData) throws -> T
Expand All @@ -19,7 +21,7 @@ public final class PostgresDataDecoder {
}
return value as! T
} else {
return try T.init(from: _Decoder(data: data, json: self.jsonDecoder))
return try T.init(from: _Decoder(data: data, json: self.json))
}
}

Expand Down Expand Up @@ -47,9 +49,9 @@ public final class PostgresDataDecoder {
}

let data: PostgresData
let json: JSONDecoder
let json: PostgresJSONDecoder

init(data: PostgresData, json: JSONDecoder) {
init(data: PostgresData, json: PostgresJSONDecoder) {
self.data = data
self.json = json
}
Expand Down Expand Up @@ -93,7 +95,7 @@ public final class PostgresDataDecoder {
var currentIndex: Int = 0

let data: [PostgresData]
let json: JSONDecoder
let json: PostgresJSONDecoder
var codingPath: [CodingKey] {
[]
}
Expand Down Expand Up @@ -128,7 +130,7 @@ public final class PostgresDataDecoder {

struct _ValueDecoder: SingleValueDecodingContainer {
let data: PostgresData
let json: JSONDecoder
let json: PostgresJSONDecoder
var codingPath: [CodingKey] {
[]
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/PostgresKit/PostgresDataEncoder.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Foundation
import protocol PostgresNIO.PostgresJSONEncoder
import var PostgresNIO._defaultJSONEncoder

public final class PostgresDataEncoder {
public let json: JSONEncoder
public let json: PostgresJSONEncoder

public init(json: JSONEncoder = JSONEncoder()) {
public init(json: PostgresJSONEncoder = PostgresNIO._defaultJSONEncoder) {
self.json = json
}

Expand Down

0 comments on commit cbbe3ef

Please sign in to comment.