๐ Give this repo a star and help its development grow! ๐
Parse JSON data, simple, lightweight & without noise.
Enjoy the playground, it also contains some tests & an example JSON file.
// init with a `String`
JSONX(string:"{'name':'Khan Solo'}", usesSingleQuotes:true)
// init with a file path
JSONX(filepath:path)
// init with a file `URL`
JSONX(url:url)
// init with `Data`
JSONX(data:data)
// init with a `Dictionary`
JSONX(dictionary:["name":"Khan-Solo", "level":50, "skills":[1,2,3], "droids":["shiny":9]])
// convert dictionary to `JSONX`
let dict = ["key1":"value1", "key2":"value2"]
let jsonX = dict.toJSONX(context:"converting stuff")
Supported Data Types:
Bool, UInt/Int, Float/Double, String, Array, Dictionary, Raw uncasted format
Default Values:
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" ๐คฉ
// example
{
"Person": {
"name": "Khan Solo",
"age": 99
}
}
// example
struct PersonModel
{
var name:String!
var age:Int!
static func `init`(jsonx:JSONX) -> PersonModel
{
var pm = PersonModel()
pm.name = jsonx.asString("name", default:"John Doe")
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 fast, you can use it in realtime without worrying about performance!
jsonx.asString(inKeyPath:"key1.key2.key3.key4.key5") // much nicer syntax ๐
swiftyjson["key1"]["key2"]["key3"]["key4"]["key5"].string
The test measures each call 10000 times, lower result is better & faster.
- Version 1.3 updates for Swift 4.2.
- Added
dictionary.toJSONX(:)
function which creates a JSONX object from a dictionary. - Updated documentation + comments.
- Added
- Version 1.2.2 bug fix for null values, bug fix for non-existing key in dictionary key path crash.
- Version 1.2.1 adds support for Swift 4 and Xcode 9.
- Version 1.2.0 adds support for Swift 4 and Xcode 9.
- Version 1.1.0 improves performance as well as minor refactorings (Swift 3.0.1/3.1/3.2).
- Swift Version 4.2
- Xcode 10
There is no framework/library distibution, I recommend that you simply add the JSONX.swift
to your project. As this will allow you to easily find & read the JSONX API, and it will also allow JSONX to compile using your apps build settings.
- Using Git:
git clone https://github.com/MKGitHub/JSONX.git
then addJSONX.swift
to your Xcode project. - Manual Way: Add
JSONX.swift
to your Xcode project. - Using Carthage: In your Cartfile add
github "MKGitHub/JSONX" ~> 1.3
thencarthage update --no-build
then addJSONX.swift
to your Xcode project. - Using Swift Package Manager: swift-tools-version:4.0
- CocoaPods support has been removed! ๐๐๐ Never use CocoaPods! ๐
Go to the documentation index page.
JSONX is used in production in the following apps/games (that I'm aware of), these apps are together used by millions of users. Please let me know if you use JSONX.
- Hoppa
- McDonald's apps
- Lรฅnekoll
https://github.com/MKGitHub/JSONX
Copyright 2016/2017/2018 Mohsan Khan
Licensed under the Apache License, Version 2.0.