-
-
Save bubudrc/7c7aec71e61f7b805404ad92c71a963b to your computer and use it in GitHub Desktop.
Example on how to integrate IkigaJSON with Vapor 4
This file contains hidden or 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
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) | |
// ... | |
} |
This file contains hidden or 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
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) | |
} | |
} |
This file contains hidden or 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
// 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