Skip to content

Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die

License

Notifications You must be signed in to change notification settings

karmaios/JSONNeverDie

 
 

Repository files navigation

JSONNeverDie

Platform License Language Carthage compatible Travis

JSONNeverDie is an auto reflection tool from JSON to Model, a user friendly JSON encoder / decoder, aims to never die.

logo

Example

set up a Model:

class People: JSONNDModel {
    var name = ""
}

reflex JSON to Model automatic:

let json = JSONND(dictionary: ["name": "JohnLui"])
let people = People(JSONNDObject: json)
print(people.name) // get "JohnLui"

Features

reflection features

  • JSON to Model reflection automatic
  • auto reflection with no need of init()
  • supports multi-level reflection

Read the documentation of auto reflection.

JSON encode / decode features

  • supports all types: Int, Float, Bool, String, Array
  • user friendly: Xcode can prompt all available types
  • provides both Optional-type(Int?) and Original-type(Int)

And JSONNeverDie is well tested.

Requirements

  • iOS 7.0+
  • Xcode 7

Use

import

import JSONNeverDie

basic decoding example

parse a JSON from network and get a string:

let data = NSData(contentsOfURL: NSURL(string: "http://httpbin.org/get?hello=world")!)!
let json = JSONND.initWithData(data)
print(json["args"]["hello"].stringValue)

generate a JSONND object

// init from NSData
let data = NSData(contentsOfURL: NSURL(string: "http://httpbin.org/get?hello=world")!)!
let json = JSONND.initWithData(data)

// init from Array
let arrayJSON = JSONND(array: ["hello", "world", 100])

// init from Dictionary
let dictionaryJSON = JSONND(dictionary: ["hello": "world", "hey": "guys"])

get values

let value = json["key"].int
let value = json["key"].intValue
let value = json["key"].string
let value = json["key"].stringValue
let value = json["key"].float
let value = json["key"].floatValue
let value = json["key"].bool
let value = json["key"].boolValue
let value = json["key"].array
let value = json["key"].arrayValue

// more than one level
let value = json["key"]["key1"]["key2"].int

deal with array

if let jsonArray = json["array"].array {
    for jsonItem in jsonArray {
        let value = jsonItem["key"].int
    }
}

// or just
let value = json["array"].arrayValue[0]["key"].int

Xcode can prompt all available types

pic

get raw string from a JSONND object

let string = json.jsonString

Installation

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

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

github "JohnLui/JSONNeverDie"

Then fetch and build JSONNeverDie:

carthage update

At last, add it to "Embedded Binaries" in the general panel use the "Add Other..." button. The JSONNeverDie.framework binary file is lying in ./Carthage/Build/iOS directory.

Manually

git submodule add https://github.com/johnlui/JSONNeverDie.git
open JSONNeverDie

then drag JSONNeverDie.xcodeproj to your Project, that's it!

If you want to run your project on device with JSONNeverDie, just go to PROJECT->TARGETS->[your project name]->General->Embedded Binaries, click +, select JSONNeverDie.frameWork and click "Add".

Source File

Clone all files in the Source directory into your project.

##Contribution

You are welcome to fork and submit pull requests.

##License

JSONNeverDie is open-sourced software licensed under the MIT license.

About

Auto reflection tool from JSON to Model, user friendly JSON encoder / decoder, aims to never die

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 100.0%