Pure-Swift JSON parser & formatter. Fully streamable input and output. Linux & OS X ready. Replacement for NSJSONSerialization.

132
19
Swift

Jay

Build Status
Latest Release
Platforms
Package Managers

Pure-Swift JSON parser & formatter. Fully streamable input and output. Linux & OS X ready. Replacement for NSJSONSerialization.

Jay conforms to the following specifications:

For extra convenience functions when working with the JSON enum, check out Jay-Extras.

❓ Why?

We all use JSON. Especially when writing server-side Swift that needs to run on Linux. #0dependencies

This is my take on how a JSON parser should work. This is not another JSON mapping utility library. This is an actual JSON parser and formatter. Check out the code, it was fun to write 😇

Features

  • [x] Parsing: data -> JSON object
  • [x] Formatting: JSON object -> data
  • [x] Pretty printing
  • [x] Streaming input and output, low memory footprint

Usage

Parsing from data (deserialization)

do {
	//get data from disk/network
	let data: [UInt8] = ...

	//ask Jay to parse your data
	let json = try Jay().jsonFromData(data) // JSON
	//or
	let json = try Jay().anyJsonFromData(data) // [String: Any] or [Any]

	//if it doesn't throw an error, all went well
	if let tasks = json.dictionary?["today"]?.array {
	    //you have a dictionary root object, with an array under the key "today"
	    print(tasks) //["laundry", "cook dinner for gf"]
	} 
} catch {
	print("Parsing error: \(error)")
}

Formatting into data (serialization)

do {
	//get a json object (works for both [String: Any] and typesafe versions - JSON)

	//ask Jay to generate data
	let anyContainer = ... // [String: Any] or [Any]
	let data = try Jay(formatting: .prettified).dataFromJson(any: json) // [UInt8]
	//or
	let json: JSON = ... // JSON
	let data = try Jay(formatting: .prettified).dataFromJson(json: json) // [UInt8]

	//send data over network, save to disk
} catch {
	print("Formatting error: \(error)")
}

Installation

Swift Package Manager

.Package(url: "https://github.com/czechboy0/Jay.git", majorVersion: 1)

💙 Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

💝 Contributing

Please create an issue with a description of your problem or open a pull request with a fix.

✌️ License

MIT

👽 Author

Honza Dvorsky - http://honzadvorsky.com, @czechboy0