Skip to content

Instantly share code, notes, and snippets.

@bubudrc
Forked from vzsg/IkigaJSON+Content.swift
Created December 10, 2021 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bubudrc/7c7aec71e61f7b805404ad92c71a963b to your computer and use it in GitHub Desktop.
Save bubudrc/7c7aec71e61f7b805404ad92c71a963b to your computer and use it in GitHub Desktop.
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