Compatibility
- 1.2.25.35.25.15.04.2
- master5.35.25.15.04.2
- 1.2.2iOSmacOS(Intel)macOS(ARM)LinuxtvOSwatchOS
- masteriOSmacOS(Intel)macOS(ARM)LinuxtvOSwatchOS
Parse JSON data, simple, lightweight & without noise.
🌟 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.
dictionary.toJSONX(:)
function which creates a JSONX object from a dictionary.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.
git clone https://github.com/MKGitHub/JSONX.git
then add JSONX.swift
to your Xcode project.JSONX.swift
to your Xcode project.github "MKGitHub/JSONX" ~> 1.3
then carthage update --no-build
then add JSONX.swift
to your Xcode project.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.
https://github.com/MKGitHub/JSONX
Copyright 2016/2017/2018 Mohsan Khan
Licensed under the Apache License, Version 2.0.