otbivnoe / codablealamofire Goto Github PK
View Code? Open in Web Editor NEWAn extension for Alamofire that converts JSON data into Decodable objects.
License: MIT License
An extension for Alamofire that converts JSON data into Decodable objects.
License: MIT License
Can model structure with Object
& Decodable
?
Object
: This Realm object.
When using this DateFormatter
separately, I can successfully deserialize date string with it:
extension DateFormatter {
static let apiDateFormatter: DateFormatter = {
let formatter = DateFormatter()
//2019-08-05T08:41:06.590Z
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
return formatter
}()
}
If I use the same formatter when calling .responeDecodableObject(decoder: decoder)
, model properties (of Date type) are set to nil.
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(.apiDateFormatter)
Alamofire.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers)
.responseDecodableObject(decoder: decoder) { (response: DataResponse<T>) in
let data = response.data
}
Is there any more setup that I am not aware of? Thanks
Hello,
First of all thanks for such useful library.
It seems Alamofire 5.0.0-rc.3 is API stable now. Is there a way/branch to use that with CodableAlamofire? without forking?
Best
Swift 4 & Xcode 9.0 on mac OS High Sierra
...alamofireRequest.responseDecodableObject(
queue: nil,
keyPath: nil,
decoder: JSONDecoder()) {
(resp: DataResponse<GenralResponse<FlexibleValue>>) in
logger.verbose(resp)
}
// FlexibleValue
is for decoding the flexible part of response JSON.
struct FlexibleValue: Decodable {
var role: [String]
}
// GeneralResponse<T>
is the designed to decode general response from back-end, with a hosted type T
, which is decodable too.
struct GeneralResponse<T: Decodable>: Decodable {
struct Head: Decodable {
let version: String
let msgtype: String
let interface: String
let remark: String
}
struct Body: Decodable {
let ret: String
let retcode: String
let retinfo: String
let data: T?
}
let head: Head
let body: Body
}
Hello,
First of all thanks for such useful library.
It seems Alamofire 4.8.0 compatibiliy is missing.
Best
The Alamofire dependency is specified in such a manner that users of this pod are locked to a specific minor version. As Alamofire follows sematic versioning (see Alamofire/Changelog), locking this dependency to a major version is all that should be required.
Is it possible for
s.dependency 'Alamofire', '~> 5.0.0-rc.3'
to be replaced with
s.dependency 'Alamofire', '~> 5.0'
Hi,
I have a special case, I have a JSON response which sometimes returns a numeric value and sometimes a string value for the same key. Is there any way to handle such a JSON response.
I know, that this isn't a good idea in general, but I don't have access to the server backend ๐.
var id: String
var owner_id: String
var name: String
var active: Bool?
var share: Bool?
var date: Int?
var duration: Int
typealias DownloadComplete = () -> ()
}
extension ActiveChannelHangouts {
static func from(json: String, using encoding: String.Encoding = .utf8) -> ActiveChannelHangouts? {
guard let data = json.data(using: encoding) else { return nil }
return ActiveChannelHangouts.from(data: data)
}
static func from(data: Data) -> ActiveChannelHangouts? {
let decoder = JSONDecoder()
do {
return try decoder.decode(ActiveChannelHangouts.self, from: data)
} catch {
print(error.localizedDescription)
return nil
}
}
var jsonData: Data? {
let encoder = JSONEncoder()
do {
return try encoder.encode(self)
} catch {
print(error.localizedDescription)
return nil
}
}
var jsonString: String? {
guard let data = self.jsonData else { return nil }
return String(data: data, encoding: .utf8)
}
}
extension ActiveChannelHangouts {
enum CodingKeys: String, CodingKey {
case id = "id"
case ownerId = "ownerId"
case name = "name"
case active = "active"
case share = "share"
case date = "date"
case duration = "duration"
}
}
// from my APIClient struct trying to call this method
func responseDecodableObject<T: Decodable>(url: String, queue: DispatchQueue? = nil,
keyPath: String? = nil,
decoder: JSONDecoder = JSONDecoder(),
completionHandler: @escaping (DataResponse<T>) -> Void) {
if !PerfectLocalAuth.sessionid.isEmpty {
Alamofire.request(url).responseDecodableObject(queue: PerfectLocalAuth.setHeaders(), keyPath: keyPath, decoder: decoder) {
(response: DataResponse<[T]>) in
let repo = response.result.value
print(repo)
}
}
}
i am trying to create one func that can be handle for all my fetch request but i go this error
Cannot convert value of type 'HTTPHeaders' (aka 'Dictionary<String, String>') to expected argument type 'DispatchQueue?
Really handy library, thanks for making it.
I'm wondering if you think the responseDecodableObject[keyPath: ...]
is a good call. There's going to be performance hit deserializing to json then serializing back to data then deserializing to codable object.
Users might not look under the hood and see this. Isn't better they just make a container object or writing a customer coder method? Also asking for myself really.
Best,
Hi!
It will be nice to have something like Codable, but for Moya :)
This is a very good project. I have a problem.
How much is the project requirements of the IOS version? iOS 8?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.