Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example on how to integrate IkigaJSON with Vapor 4
import Vapor
import IkigaJSON
// ...
public func configure(_ app: Application) throws {
var decoder = IkigaJSONDecoder()
decoder.settings.dateDecodingStrategy = .iso8601
ContentConfiguration.global.use(decoder: decoder, for: .json)
var encoder = IkigaJSONEncoder()
encoder.settings.dateDecodingStrategy = .iso8601
ContentConfiguration.global.use(encoder: encoder, for: .json)
// ...
}
import Vapor
import IkigaJSON
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)
}
}
extension IkigaJSONDecoder: ContentDecoder {
public func decode<D: Decodable>(
_ decodable: D.Type,
from body: ByteBuffer,
headers: HTTPHeaders
) throws -> D {
guard headers.contentType == .json || headers.contentType == .jsonAPI else {
throw Abort(.unsupportedMediaType)
}
return try self.decode(D.self, from: body)
}
}
// swift-tools-version:5.2
import PackageDescription
let package = Package(
name: "app",
platforms: [
.macOS(.v10_15),
],
products: [
.executable(name: "Run", targets: ["Run"]),
.library(name: "App", targets: ["App"]),
],
dependencies: [
// ...
.package(url: "https://github.com/Ikiga/IkigaJSON.git", from: "2.0.0")
],
targets: [
.target(name: "App", dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "IkigaJSON", package: "IkigaJSON")
// ...
]),
// ...
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment