CaesarParser - Swift中的JSON模型解析器

jopen 8年前發布 | 9K 次閱讀 Apple Swift開發 CaesarParser

CaesarParser

CaesarParser 是一個框架采用 Swift 開發,用于解析JSON模型。

特性

  • 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

Cocoa Touch Framework requires iOS 8 or later.

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

基本用法

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

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

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

Any type that confirm Serializable can be parse to JSON.

/// convert to JSON object
public protocol Serializable {
    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 Convertible
  • Array<Convertible or Deserializable>
  • Dictionary<Convertible and Hashable, Convertible or Deserializable>

Demo Code

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

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

    init(json: JSONDictionary) {
        name <-- json["name"]
        age <-- json["age"]
        birthday <-- json["birthday"]
        weight <-- json["weight"]
        adult <-- json["adult"]
        sex <-- json["sex"]
        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"]
        sex --> json["sex"]
        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.



項目地址: https://github.com/lancy/CaesarParser

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!