forked from loafle/openapi-generator-original
* [swift5] Add useSPMFileStructure Signed-off-by: Ayman Bagabas <ayman.bagabas@gmail.com> * [swift5] Add swiftPackagePath Prioritize swiftPackagePath over useSPMFileStructure * [swift5] Add cli options and update docs * [swift5] Fix tests * [swift5] Update XcodeGen source path * [swift5] Update samples and docs Add useSPMFileStructure to URLSession library
50 lines
1.7 KiB
Swift
50 lines
1.7 KiB
Swift
//
|
|
// CodableHelper.swift
|
|
//
|
|
// Generated by openapi-generator
|
|
// https://openapi-generator.tech
|
|
//
|
|
|
|
import Foundation
|
|
|
|
open class CodableHelper {
|
|
private static var customDateFormatter: DateFormatter?
|
|
private static var defaultDateFormatter: DateFormatter = OpenISO8601DateFormatter()
|
|
|
|
private static var customJSONDecoder: JSONDecoder?
|
|
private static var defaultJSONDecoder: JSONDecoder = {
|
|
let decoder = JSONDecoder()
|
|
decoder.dateDecodingStrategy = .formatted(CodableHelper.dateFormatter)
|
|
return decoder
|
|
}()
|
|
|
|
private static var customJSONEncoder: JSONEncoder?
|
|
private static var defaultJSONEncoder: JSONEncoder = {
|
|
let encoder = JSONEncoder()
|
|
encoder.dateEncodingStrategy = .formatted(CodableHelper.dateFormatter)
|
|
encoder.outputFormatting = .prettyPrinted
|
|
return encoder
|
|
}()
|
|
|
|
public static var dateFormatter: DateFormatter {
|
|
get { return customDateFormatter ?? defaultDateFormatter }
|
|
set { customDateFormatter = newValue }
|
|
}
|
|
public static var jsonDecoder: JSONDecoder {
|
|
get { return customJSONDecoder ?? defaultJSONDecoder }
|
|
set { customJSONDecoder = newValue }
|
|
}
|
|
public static var jsonEncoder: JSONEncoder {
|
|
get { return customJSONEncoder ?? defaultJSONEncoder }
|
|
set { customJSONEncoder = newValue }
|
|
}
|
|
|
|
open class func decode<T>(_ type: T.Type, from data: Data) -> Swift.Result<T, Error> where T: Decodable {
|
|
return Swift.Result { try jsonDecoder.decode(type, from: data) }
|
|
}
|
|
|
|
open class func encode<T>(_ value: T) -> Swift.Result<Data, Error> where T: Encodable {
|
|
return Swift.Result { try jsonEncoder.encode(value) }
|
|
}
|
|
}
|