Git Product home page Git Product logo

tidyjson's Introduction

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:

let jsonFalse : JSON = false
let jsonTrue : JSON = true
let jsonNull : JSON = nil
let jsonNumber : JSON = 1.2e-2
let jsonString : JSON = "hello swift"
let jsonArray : JSON = [12, "string", false, nil, true, ["nested array", 12, 1.2], ["nested dict": nil]]
let json : 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:

var json : JSON = []
// []

let child :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"]

var json1 : 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

let json1 = try! JSON.parse("{\"key\" : false }")
if let b = json1["key"].bool {
    print(b)
}
/*
false
*/

let json2 = try! JSON.parse("{\"key\" : [\" \\u0041334 \\n \\t \\\" \"]}")

if let x = json2["key"][0].string {
    print(x)
}
/*
 A334 
         " 
*/

Loop

let jsonArray : 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)
*/

let json : 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

let json2 = 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

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

License

MIT license

tidyjson's People

Contributors

benloong avatar dileping avatar ziligy avatar

Stargazers

Anton Begehr avatar Ravneet Singh avatar Mike Brave avatar weakish avatar Simon Strandgaard avatar im-ng avatar Owen Hael avatar Sergio Araki avatar  avatar Franco Rondini avatar  avatar Aleksandr Moshkin avatar Patrick Smith avatar  avatar  avatar Xie Wu avatar Padraic Renaghan avatar Mart Roosmaa avatar Pranav Lathigara avatar  avatar XTCm avatar Nini La avatar Neo Zang avatar Yanzhi Wang avatar qinhao avatar madfrog avatar jack avatar Soli Como avatar  avatar Jun Chen avatar SixPoems avatar parachvte avatar Kai Li avatar michealzh avatar Yansong Li avatar Tony Wang avatar danbao avatar Liu Yi avatar 达峰的夏天 avatar  avatar MarkNote avatar Curtis C. avatar Pratikbhai Patel avatar wulei avatar

Watchers

James Cloos avatar  avatar

tidyjson's Issues

Error using package-manager

error: the module at Tests/TestCases has an invalid name ('TestCases'): the name of a test module has noTestssuffix
fix: rename the module atTests/TestCasesto have aTestssuffix

run_tests.sh doesn't run on OS X

How do you think we could go about testing with corelibs-xctest on OS X. The run_tests.sh script seems to work fine on Linux, but it would be valuable to do the same on Darwin.

Thanks for a great project!

init? with Any

Hi guys, great job!

I'm interested in replacing our current library choice with TidyJSON for our project Swift Express.

What I really miss here is a constructor with Any argument, something like:

init(any:Any?) throws

or

init?(any:Any?)

or may be even always succeeding init, but being your built-in null if failed.

Any can be anything supported, like Dictionary, String, whatever...

Do you consider adding something like this?

Thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.