QuerySerialization

Swift 4.0 License CocoaPods Carthage compatible Build Status

QuerySerialization is a Swift library that allows you to encode dictionaries into query strings, and to decode query strings into dictionaries. It also supports automatic percent encoding/decoding.

📚 Documentation

Platforms

  • iOS 8.0+
  • macOS 10.9+
  • tvOS 9.0+
  • watchOS 2.0+
  • Linux

Installation

QuerySerialization version vs. Swift version

Swift QuerySerialization
4.X >= 1.1.0
3.X >= 1.0.0

Swift Package Manager

Add this line to your Package.swift:

.Package(url: "https://github.com/alexaubry/QuerySerialization.git", from: "1.1.0")

CocoaPods

Add this line to your Podfile:

pod "QuerySerialization"

Carthage

Add this line to your Cartfile:

github "alexaurby/QuerySerialization"

Manually

Drag the QuerySerialization.swift file into your project.

Usage

You use the QuerySerialization class to encode or decode query strings.

Encoding

To encode a dictionary into a query string, call:

let queryElements = ["key":"value","message":"Hello world"]
let queryString = QuerySerialization.queryString(fromDictionary: queryElements)

// queryString = "key=value&message=Hello%20world"

As you may notice, percent encoding is added automatically by default. You can opt-out this feature by using this instead:

let queryElements = ["key":"value","message":"Hello world"]
let queryString = QuerySerialization.queryString(fromDictionary: queryElements, urlEncode: false)

// queryString = "key=value&message=Hello world"

Decoding

To decode a query string into a Dictionary, call:

let queryString = "key=value&message=Hello%20world"
let queryElements = QuerySerialization.decode(queryString: queryString)

// queryElements = ["key":"value","message":"Hello world"]

If a key or value contains percent encoding, it will be removed automatically. You can opt-out this feature by using this instead:

let queryString = "key=value&message=Hello%20world"
let queryElements = QuerySerialization.decode(queryString: queryString, removePercentEncoding: false)

// queryElements = ["key":"value","message":"Hello%20world"]

Author

License

QuerySerialization is available under the MIT License. See the LICENSE file for more info.