Description
Swift 4 introduces a new Codable protocol that lets you serialize and deserialize custom data types without writing any special code and without having to worry about losing your value types. ๐
Awesome, isn't it? And this library helps you write less code! It's an extension for Alamofire that converts JSON data into Decodable object.
CodableAlamofire alternatives and similar libraries
Based on the "JSON" category.
Alternatively, view CodableAlamofire alternatives based on common mentions on social networks and blogs.
-
SwiftyJSON
The better way to deal with JSON data in Swift. -
ObjectMapper
Simple JSON Object mapping written in Swift -
HandyJSON
A handy swift json-object serialization/deserialization library -
AlamofireObjectMapper
An Alamofire extension which converts JSON response data into swift objects using ObjectMapper -
Gloss
[Deprecated] A shiny JSON parsing library in Swift :sparkles: Loved by many from 2015-2021 -
EVReflection
Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift -
Decodable
[Probably deprecated] Swift 2/3 JSON unmarshalling done (more) right -
JSONHelper
โ Convert anything into anything in one operation; JSON data into class instances, hex strings into UIColor/NSColor, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of! -
Genome
A simple, type safe, failure driven mapping library for serializing JSON to models in Swift 3.0 (Supports Linux) -
Himotoki
A type-safe JSON decoding library purely written in Swift -
json-swift
A basic library for working with JSON in Swift. -
Elevate
Elevate is a JSON parsing framework that leverages Swift to make parsing simple, reliable and composable. -
JSONCodable
Hassle-free JSON encoding and decoding in Swift -
JSONNeverDie
Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die -
ModelRocket
An iOS framework for creating JSON-based models. Written in Swift. -
Tailor
:necktie:A super fast & convenient object mapper tailored for your needs -
AlamofireJsonToObjects
An Alamofire extension which converts JSON response data into swift objects using EVReflection -
Alembic
:alembic: Functional JSON Parser - Linux Ready :penguin: -
Brick
:droplet: A generic view model for both basic and complex scenarios -
Alexander
An extremely simple JSON helper written in Swift. -
PPJSONSerialization
The Ultimate JSON Serialization for Swift. -
JSONParserSwift
Framework for easily parsing your JSON data directly to Swift object. -
JsonSwiftson
A JSON parser with concise API written in Swift. -
SafeDecoder
A Codable extension to decode arrays and to catch & log all decoding failures
Appwrite - The open-source backend cloud platform
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of CodableAlamofire or a related project?
README
Swift 4 introduces a new Codable
protocol that lets you serialize and deserialize custom data types without writing any special code and without having to worry about losing your value types. ๐
Awesome, isn't it? And this library helps you write less code! It's an extension for Alamofire
that converts JSON
data into Decodable
object.
Useful Resources:
- Encoding and Decoding Custom Types - Article by Apple
- Using JSON with Custom Types - Swift Playground
- Also there is a special session from
WWDC2017
that covers this new feature - What's New in Foundation
Usage
Let's decode a simple json file:
{
"result": {
"libraries": [
{
"name": "Alamofire",
"stars": 23857,
"url": "https://github.com/Alamofire/Alamofire",
"random_date_commit": 1489276800
},
{
"name": "RxSwift",
"stars": 9600,
"url": "https://github.com/ReactiveX/RxSwift",
"random_date_commit": 1494547200
}
]
}
}
with the following Swift model:
private struct Repo: Decodable {
let name: String
let stars: Int
let url: URL
let randomDateCommit: Date
private enum CodingKeys: String, CodingKey {
case name
case stars
case url
case randomDateCommit = "random_date_commit"
}
}
There is a similar method to responseData
, responseJSON
- responseDecodableObject
:
func responseDecodableObject<T: Decodable>(queue: DispatchQueue? = nil, keyPath: String? = nil, decoder: JSONDecoder = JSONDecoder(), completionHandler: @escaping (AFDataResponse<T>) -> Void)
queue
- The queue on which the completion handler is dispatched.keyPath
- The keyPath where object decoding should be performed.decoder
- The decoder that performs the decoding of JSON into semanticDecodable
type.
let url = URL(string: "https://raw.githubusercontent.com/otbivnoe/CodableAlamofire/master/keypathArray.json")!
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970 // It is necessary for correct decoding. Timestamp -> Date.
AF.request(url).responseDecodableObject(keyPath: "result.libraries", decoder: decoder) { (response: AFDataResponse<[Repo]>) in
let repo = response.value
print(repo)
}
Note:
- For array:
DataResponse<[Repo]>
- For single object:
DataResponse<Repo>
Requirements
- Swift 4+
- Xcode 9+
Installation ๐ฅ
CocoaPods
CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over eighteen thousand libraries and can help you scale your projects elegantly. You can install it with the following command:
$ sudo gem install cocoapods
To integrate CodableAlamofire, simply add the following line to your Podfile
:
target 'Test' do
use_frameworks!
pod 'CodableAlamofire'
end
Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate CodableAlamofire into your Xcode project using Carthage, specify it in your Cartfile
:
github "Otbivnoe/CodableAlamofire"
Run carthage update
to build the framework and drag the built CodableAlamofire.framework
into your Xcode project.