Pure-Swift JSON parser & formatter. Linux & OS X ready. Replacement for NSJSONSerialization.
Jay conforms to the following specifications:
- JSON RFC4627
We all use JSON. Especially when writing server-side Swift that needs to run on Linux. #0dependencies
NSJSONSerialization
is not yet fully implemented in the Swift standard libraries, so this is my take on how parsers 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 😇
- Parsing: data -> JSON object
- Formatting: JSON object -> data
do {
//get data from disk/network
let data: [UInt8] = ...
//ask Jay to parse your data
let json = try Jay().jsonFromData(data) // Any
//if it doesn't throw an error, all went well
if let dictionary = json as? [String: Any] {
//you have a dictionary root object
} else if let array = json as? [Any] {
//you have an array root object
}
} catch {
print("Parsing error: \(error)")
}
do {
//get a json object
let json: [String: Any] = ... // [String: Any] or [Any]
//ask Jay to generate data
let data = try Jay().dataFromJson(json) // [UInt8]
//send data over network, save to disk
} catch {
print("Formatting error: \(error)")
}
.Package(url: "https://github.com/czechboy0/Jay.git", majorVersion: 0)
pod 'Jay'
This project was originally created for a Swift server running on Linux, so it's always built (and assumes) the latest Swift and SwiftPM versions. To manage Swift versions easily, I recommend you use swiftenv. You can see which Swift version Jay requires at the moment in the .swift-version
file in the root of the repository. Please make sure you're always running at least that version if you want to use Jay in your project.
Please create an issue with a description of your problem or open a pull request with a fix.
MIT
Honza Dvorsky - http://honzadvorsky.com, @czechboy0