Giter Club home page Giter Club logo

sexyjson's Introduction

SexyJson

Build Status Pod Version Platform Pod License

SexyJson is Swift3.+ json parse open source library quickly and easily, perfect supporting class and struct model, support the KVC model, fully oriented protocol architecture, support iOS and MAC OS X

Objective-c version πŸ‘‰ WHC_Model

Note

  • The definition of model must implement SexyJson protocol
  • If you have any enumeration type must be specified in the definition of model data type and implementation SexyJsonEnumType protocol
  • To KVC model must inherit NSObject and NSCoding protocol implementation

Require

  • iOS 8.0+ / Mac OS X 10.11+ / tvOS 9.0+
  • Xcode 8.0 or later
  • Swift 3.0+

Install

  • CocoaPods: pod 'SexyJson'

Model

This is an example of json:

let json = "{\"age\":25,\"enmuStr\":\"Work\",\"url\":\"https:\\/\\/www.baidu.com\",
            \"subArray\":[{\"test3\":\"test3\",\"test2\":\"test2\",\"cls\":{\"age\":10,
            \"name\":\"swift\"},\"test1\":\"test1\"},{\"test3\":\"test3\",\"test2\":\"test2\",
            \"cls\":{\"age\":10,\"name\":\"swift\"},\"test1\":\"test1\"}],\"color\":\"0xffbbaa\",
            \"nestArray\":[[{\"test\":\"test1\"},{\"test\":\"test2\"}],[{\"test\":\"test3\"},
            {\"test\":\"test4\"}]],\"enmuInt\":10,\"sub\":{\"test1\":\"test1\",\"test2\":\"test2\",
            \"test3\":\"test3\"},\"height\":175,\"intArray\":[1,2,3,4],\"name\":\"吴桷袅\",
            \"learn\":[\"iOS\",\"android\",\"js\",\"nodejs\",\"python\"]}"

The model class:

enum WorkEnum: String,SexyJsonEnumType {
    case null = "nil"
    case one = "Work"
    case two = "Not Work"
}

enum IntEnum: Int,SexyJsonEnumType {
    case zero = 0
    case hao = 10
    case xxx = 20
}

struct Model :SexyJson {

    var age: Int = 0
    var enmuStr: WorkEnum!
    var url: URL!
    var subArray: [SubArray]!
    var color: UIColor!
    var nestArray: [[NestArray]]?
    var enmuInt: IntEnum = .xxx
    var sub: Sub!
    var height: Int = 0
    var intArray: [Int]!
    var name: String!
    var learn: [String]!

    /// Model mapping
    public mutating func sexyMap(_ map: [String : Any]) {
        age      <<<   map["age"]
        enmuStr  <<<   map["enmuStr"]
        url      <<<   map["url"]
        subArray <<<   map["subArray"]
        color    <<<   map["color"]
        nestArray <<<  map["nestArray"]
        enmuInt   <<<  map["enmuInt"]
        sub       <<<  map["sub"]
        height    <<<  map["height"]
        intArray  <<<  map["intArray"]
        name      <<<  map["name"]
        learn     <<<  map["learn"]
    }
}

You don't need to manually create the SexyJson model class you can use open source tools with the help of WHC_DataModel.app automatically created SexyJson model

Usage

Json is converted into a model object(json -> model)

let model = Model.sexy_json(json)

Model object converted into the dictionary(model -> dictionary)

let dictionary = model.sexy_dictionary()

Model object converted into the json string(model -> json)

let jsonStr = model.sexy_json()

SexyJson support json parse the key path

let subArrayModel = SubArray.sexy_json(json, keyPath: "subArray[0]")
let subNestArray = NestArray.sexy_json(json, keyPath: "nestArray[0][0]")
let test = String.sexy_json(json, keyPath: "nestArray[0][0].test")

Json is converted into a model array object(json -> [model])

let arrayModel = [Model].sexy_json(json)

Model object array converted into the array([model] -> array)

let array = arrayModel.sexy_array()

Model object array converted into the json string([model] -> json)

let arrayJson = arrayModel.sexy_json()

SexyJson support model kvc

let sub = Sub.sexy_json(json, keyPath: "sub")
let subData = NSKeyedArchiver.archivedData(withRootObject: sub!)
let subCoding = NSKeyedUnarchiver.unarchiveObject(with: subData) as? Sub
let subPy = subCoding?.copy() as? Sub

Prompt

If you want to view the analytical results, please download this demo to check the specific usage

Licenses

All source code is licensed under the MIT License.

sexyjson's People

Contributors

netyouli avatar

Watchers

James Cloos avatar Lerist avatar  avatar

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.