Skip to content

Parse and serialize url-encoded form data with Codable support.

License

Notifications You must be signed in to change notification settings

forXifLess/URLEncodedForm

Repository files navigation

Swift Platforms Swift Package Manager

URLEncodedForm

Parse and serialize url-encoded form data with Codable support.

🙏🏻 Notice

  • 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.

Installation

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"))
]

✍🏻 Usage

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)
}

License

URLEncodedForm is released under the MIT license. See LICENSE for details.

About

Parse and serialize url-encoded form data with Codable support.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages