Integrate IkigaJSON with Vapor 4
masterTo use IkigaJSON in Vapor 4, extend IkigaJSONEncoder to conform to ContentEncoder and IkigaJSONDecoder to conform to ContentDecoder. You must then register them with ContentConfiguration.global.
extension IkigaJSONEncoder: ContentEncoder {
public func encode<E: Encodable>(
_ encodable: E,
to body: inout ByteBuffer,
headers: inout HTTPHeaders
) throws {
headers.contentType = .json
try self.encodeAndWrite(encodable, into: &body)
}
public func encode<E>(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders, userInfo: [CodingUserInfoKey : Sendable]) throws where E : Encodable {
var encoder = self
encoder.userInfo = userInfo
headers.contentType = .json
try encoder.encodeAndWrite(encodable, into: &body)
}
public func encode<E>(_ encodable: E, to body: inout ByteBuffer, headers: inout HTTPHeaders, userInfo: [CodingUserInfoKey : Any]) throws where E : Encodable {
var encoder = self
encoder.userInfo = userInfo
headers.contentType = .json
try encoder.encodeAndWrite(encodable, into: &body)
}
}
extension IkigaJSONDecoder: ContentDecoder {
public func decode<D: Decodable>(
_ decodable: D.Type,
from body: ByteBuffer,
headers: HTTPHeaders
) throws -> D {
return try self.decode(D.self, from: body)
}
public func decode<D>(_ decodable: D.Type, from body: ByteBuffer, headers: HTTPHeaders, userInfo: [CodingUserInfoKey : Sendable]) throws -> D where D : Decodable {
let decoder = IkigaJSONDecoder(settings: settings)
decoder.settings.userInfo = userInfo
return try decoder.decode(D.self, from: body)
}
public func decode<D>(_ decodable: D.Type, from body: ByteBuffer, headers: HTTPHeaders, userInfo: [CodingUserInfoKey : Any]) throws -> D where D : Decodable {
let decoder = IkigaJSONDecoder(settings: settings)
decoder.settings.userInfo = userInfo
return try decoder.decode(D.self, from: body)
}
}
// Registration
var decoder = IkigaJSONDecoder()
decoder.settings.dateDecodingStrategy = .iso8601
ContentConfiguration.global.use(decoder: decoder, for: .json)
var encoder = IkigaJSONEncoder()
encoder.settings.dateEncodingStrategy = .iso8601
ContentConfiguration.global.use(encoder: encoder, for: .json)