26 lines
717 B
Swift
26 lines
717 B
Swift
//
|
|
// Data+Extension.swift
|
|
//
|
|
//
|
|
// Created by Juraldinio on 12/6/22.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public extension Data {
|
|
|
|
func jsonDecoded<T: Decodable>(type: T.Type, userInfo: [CodingUserInfoKey: Any]? = nil) -> T? {
|
|
try? self.makeDecoder(userInfo: userInfo).decode(type, from: self)
|
|
}
|
|
|
|
func jsonDecoded<T: Decodable>(type: T.Type, userInfo: [CodingUserInfoKey: Any]? = nil) -> [T]? {
|
|
try? self.makeDecoder(userInfo: userInfo).decode([T].self, from: self)
|
|
}
|
|
|
|
private func makeDecoder(userInfo: [CodingUserInfoKey: Any]?) -> JSONDecoder {
|
|
let jsonDecoder = JSONDecoder()
|
|
userInfo >>- { jsonDecoder.userInfo = $0 }
|
|
return jsonDecoder
|
|
}
|
|
}
|