Parse and serialize url-encoded form data with Codable support.
- This is an (En/De)coder that converts URL query item parameters to Codable.
- The original source has been modified to use the url-encoded-form created by the Vapor team through Foundation.
The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift
compiler.
Once you have your Swift package set up, adding Alamofire as a dependency is as easy as adding it to the dependencies
value of your Package.swift
.
dependencies: [
.package(url: "https://github.com/interactord/URLEncodedForm", .upToNextMajor(from: "1.0.0"))
]
It supports Codable provided by the Swift Foundation.
struct MyInfo: Codable {
let height: Double
let name: String
let friends: [String]
let family: Family
struct Family: Codable {
let names: [String]
}
}
let me = MyInfo(
height: 172.2,
name: "Scott",
friends: ["tom", "john", "mike"],
family: .init(
names: ["Father", "Mother", "Brother"]))
do {
let data = try URLEncodedFormEncoder().encode(me)
print(String(data: data, encoding: .utf8) ?? "nil")
let origin = "name=Scott&height=172.2&friends[]=tom&friends[]=john&friends[]=mike&family[names][]=Father&family[names][]=Mother&family[names][]=Brother"
let obj = try URLEncodedFormDecoder().decode(MyInfo.self, from: origin)
print(obj)
} catch {
print(error)
}
URLEncodedForm is released under the MIT license. See LICENSE for details.