Git Product home page Git Product logo

caesarparser's Introduction

CaesarParser

Build Status

CaesarParser is a framework written in Swift for you to parse Model from JSON or to JSON.

Features

  • JSON deserialization, parse JSON to Model
  • JSON serialization, parse Model to JSON
  • Support nested class, stand along, in arrays or in dictionaries
  • Custom converter during parsing
  • Support struct, primitive type, raw representable enum

Requirements

Cocoa Touch Framework requires iOS 8 or later.

Manual add CaesarParser to your project requires iOS 7 or later.

Installation

###Carthage

Add the following line to your Cartfile.

github "lancy/CaesarParser"

Then do carthage update. After that, add the framework to your project.

###Cocoapods

Add the following line in your Podfile.

pod "CaesarParser", :git => 'https://github.com/lancy/CaesarParser.git'

Basic Usages

Any type that confirm JSONDeserializable or JSONConvertible protocol can be parse. Besides you can use custom value converter during parsing.

/// Use for Class, Nested Type
public protocol JSONDeserializable {
    init(json: JSONDictionary)
}

/// Use for Primitive Type
public protocol JSONConvertible {
    static func convert(json: JSONObject) -> Self?
}

Any type that confirm JSONSerializable can be parse to JSON.

/// convert to JSON object
public protocol JSONSerializable {
    func toJSONObject() -> JSONObject
}

Build-in Support

  • Int
  • String
  • Double
  • Float
  • Bool
  • NSURL
  • NSDate (unix_timestamp to NSDate, can be custom by build-in DateFormatConverter)
  • NSURL (string to URL)
  • Raw representable enums which raw value confirm to JSONConvertible
  • Array<JSONConvertible or JSONDeserializable>
  • Dictionary<JSONConvertible and Hashable, JSONConvertible or JSONDeserializable>

Demo Code

enum Gender: Int {
	case Unknown = 0
	case Male = 1
	case Female = 2
}

class Person: JSONDeserializable, JSONSerializable {
    var name: String?
    var age: Int?
    var birthday: Double?
    var weight: Float?
    var adult: Bool = false
    var gender: Gender = .Unknown
    var girlFriend: Person?
    var friends = [Person]()
    var luckyNumbers = [Int]()
    var favouredSingers = [String: Person]()
    var vips = [Int: Person]()
    var preferNumbers = [Int: Int]()
    var orientation = [Gender]()

    init(json: JSONDictionary) {
        name <-- json["name"]
        age <-- json["age"]
        birthday <-- json["birthday"]
        weight <-- json["weight"]
        adult <-- json["adult"]
        gender <-- json["gender"]
        girlFriend <-- json["girlFriend"]
        friends <-- json["friends"]
        luckyNumbers <-- json["luckyNumbers"]
        favouredSingers <-- json["favouredSingers"]
        vips <-- json["vips"]
        preferNumbers <-- json["preferNumbers"]
        orientation <-- json["orientation"]
    }

    func toJSONObject() -> JSONObject {
        var json = JSONDictionary()

        name --> json["name"]
        age --> json["age"]
        birthday --> json["birthday"]
        weight --> json["weight"]
        adult --> json["adult"]
        gender --> json["gender"]
        girlFriend --> json["girlFriend"]
        friends --> json["friends"]
        luckyNumbers --> json["luckyNumbers"]
        favouredSingers --> json["favouredSingers"]
        vips --> json["vips"]
        preferNumbers --> json["preferNumbers"]
        orientation --> json["orientation"]

        return json
    }
}

Acknowledgements

  • JSONHelper CaesarParser is inspired by JSONHelper a lot, thanks for their great work.

License

CaesarParser is available under the MIT license.

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.