TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | Apache 2 |
ReleasedLast Release | Jan 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✓ |
Maintained by Mohsan Khan.
Parse JSON data, simple, lightweight & without noise.
Enjoy the playground, it also contains some tests & an example json file.
// init with String, specifying single or double quotes
JSONX(with:"{'name':'Khan Solo'}", usesSingleQuotes:true)
// init with file path
JSONX(with:path)
// init with file URL
JSONX(with:url)
// init with Data
JSONX(with:data)
// init with Dictionary<String, Any>
JSONX(with:["name":"Khan-Solo", "level":50, "skills":[1,2,3], "droids":["shiny":9]])
Supported Data Types: Bool, UInt,/Int, Float/Double, String, Array, Dictionary, Raw uncasted format
All accessor functions have the ability to define a default value:
// without default value
jsonx.asString("thisKeyDoesNotExist") // returns nil
// with default value
jsonx.asString("thisKeyDoesNotExist", default:"Default string") // returns "Default string"
Search using key paths:
{
"parent": {
"child": {
"puppy": {
"name": "voffy"
}
}
}
}
jsonx.asString(inKeyPath:"parent.child.puppy.name") // returns "voffy"
{
"Person": {
"name": "Khan Solo",
"age": 99
}
}
struct PersonModel
{
var name:String?
var age:Int?
static func `init`(jsonx:JSONX)
-> PersonModel
{
var pm:PersonModel = PersonModel()
pm.name = jsonx.asString("name", default:"Johnny Appleseed")
pm.age = jsonx.asInt("age", default:0)
return pm
}
}
PersonModel.init(jsonx:myJSONXObject)
Just type .as to see the function lookup with prefixed function names :-)
The provided XCTest measures the performance when it comes to finding a key in a hierarchy. Usually you would only do this once and cache the value, but it is interesting to see how JSONX compares to other alternatives. JSONX is actually so fast, you can use it in realtime without worrying about performance!
Tests were run on a MacBook Pro (Retina, 15-inch, Late 2013), 16 GB 1600 MHz DDR3, macOS 10.11.6 (15G1217)
jsonx.asString(inKeyPath:"key1.key2.key3.key4.key5")
jayson["key1"]["key2"]["key3"]["key4"]["key5"].string
swiftyjson["key1"]["key2"]["key3"]["key4"]["key5"].string
The test measures each call 10000 times, lower result is better & faster.
JSONX 1.1 | JAYSON 0.6.2 | SwiftyJSON 3.1.4 | |
---|---|---|---|
Average Time Seconds: | 0.078 | 0.163 | 1.097 |
Passed Time Seconds: | 1.035 | 1.884 | 11.659 |
By average time JSONX is 2.08x faster than JAYSON and 14.06x faster than SwiftyJSON.
By passed time JSONX is 1.82x faster than JAYSON, and 11.26x faster than SwiftyJSON.
There is no framework/library distibution, I recommend that you add the JSONX/Sources to your project. As this will allow you to easily find & read the JSONX API, it will also allow JSONX to compile using your apps build settings.
git clone https://github.com/MKGitHub/JSONX.git
then Drag & Drop the JSONX/Sources into your Xcode project.
Drag & Drop the JSONX/Sources into your Xcode project.
github "MKGitHub/JSONX" ~> 1.1.0
then carthage update --no-build
then Drag & Drop the JSONX/Sources into your Xcode project.
swift build
or swift package generate-xcodeproj
pod 'JSONX', '~> 1.1.0'
Go to the documentation index page.
JSONX is used in production in the following apps/games (known to me), these apps are together used by many millions of users every day. Please let me know if you use JSONX.
https://github.com/MKGitHub/JSONX
Copyright 2016 Mohsan Khan
Licensed under the Apache License, Version 2.0.