Skip to content

Repository files navigation

TidyJSON

TidyJSON is a neat and tidy JSON package run on all Swift platforms (Linux, iOS, OS X, tvOS, watchOS)

This branch using Swift 3.0

Build Status

Goals

  • simple, neat and tidy json lib
  • safe static typed without Any or AnyObject
  • performance
  • pure Swift, only dependent to builtin types
  • compatible with all Swift platforms (Linux, iOS, OS X, tvOS, watchOS)
  • concise usage
  • boxing dict and array type for mutating data
  • modify json via subscript operator
  • fully tested
  • better parse error report
  • Swift Package Manager, Carthage support

Usage

Literal Convertible

Literal value can be type safe converted to TidyJSON.JSON like this:

letjsonFalse:JSON=falseletjsonTrue:JSON=trueletjsonNull:JSON=nilletjsonNumber:JSON=1.2e-2letjsonString:JSON="hello swift"letjsonArray:JSON=[12,"string",false,nil,true,["nested array",12,1.2],["nested dict":nil]]letjson:JSON=["key":false,"key2":true,"key3":[1,"hello",3,"world",["key4":nil,"key5":12.03,"key6":12E-2,"key7":-12e-2]]]

Access value

Get and Set value via Subscripts:

varjson:JSON=[]
// []
letchild:JSON="string"
json.append(child)
// ["string"]
json[0]="STRING"
// ["STRING"]
json.append(JSON([]))
// ["STRING", []]
json[1].append(JSON("hello"))
// ["STRING", ["hello"]]
json[1][0]="world" // ["STRING", ["world"]]
json[1].removeAtIndex(0) // ["STRING", []]
json.removeAtIndex(1)
// ["STRING"]
varjson1:JSON=[:]
// {}
json1["hello"]=false // {"hello":false}
json1["world"]=true
// {"hello":false, "world":true}
// remove a key
json1["hello"]=nil
// {"world":true}
json1["world"]=[1,2,3]
// {"world":[1,2,3]}

Parse from String

letjson1=try!JSON.parse("{\"key\" : false }")iflet b =json1["key"].bool {print(b)}
/*
false
*/
letjson2=try!JSON.parse("{\"key\" : [\"\\u0041334 \\n \\t \\\"\"]}")iflet x =json2["key"][0].string {print(x)}
/*
A334 " */

Loop

letjsonArray:JSON=[12,"string",false,nil,true]
// for Array, key is string of current index
for(k, v)in jsonArray {print("key: \(k), value: \(v)")}
/* key: 0, value: Number(12.0)
key: 1, value: String("string")
key: 2, value: Boolean(false)
key: 3, value: Null
key: 4, value: Boolean(true)
*/
letjson:JSON=["key1":false,"key2":true,"key3":123,"key4":"hello"]for(k, v)in json {print("key: \(k), value: \(v)")}
/*
key: key, value: Boolean(false)
key: key2, value: Boolean(true)
key: key4, value: String("hello")
key: key1, value: Boolean(false)
key: key3, value: Number(123.0)
*/

Dump to String

letjson2=try!JSON.parse("{\"key\" : \"\\u0041334 \\n \\t \\\"\"}")print(json2.dump())
// {"key":" A334 \n \t \" "}

Test

On Linux platform you need install XCTest

After install XCTest, run swift build then swift test

Integration

Carthage

To integrate TidyJSON into your Xcode project using Carthage, specify it in your Cartfile:

github "benloong/TidyJSON"

Swift Package Manager

Currently support Swift Package Manager to install TidyJSON by adding the proper description to your Package.swift file:

import PackageDescription
letpackage=Package(
name:"YOUR_PROJECT_NAME",
targets:[],
dependencies:[.Package(url:"https://github.com/benloong/TidyJSON.git", majorVersion:1)])

License

MIT license

About

A neat and tidy JSON package run on all Swift platforms (Linux, iOS, OS X)

Topics

Resources

Stars

45 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages