Skip to content

Repository files navigation

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
publicprotocolJSONDeserializable{init(json:JSONDictionary)}
/// Use for Primitive Type
publicprotocolJSONConvertible{staticfunc convert(json:JSONObject)->Self?}

Any type that confirm JSONSerializable can be parse to JSON.

/// convert to JSON object
publicprotocolJSONSerializable{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

enumGender:Int{case Unknown =0case Male =1case Female =2}classPerson:JSONDeserializable,JSONSerializable{varname:String?varage:Int?varbirthday:Double?varweight:Float?varadult:Bool=falsevargender:Gender=.Unknown
vargirlFriend:Person?varfriends=[Person]()varluckyNumbers=[Int]()varfavouredSingers=[String: Person]()varvips=[Int: Person]()varpreferNumbers=[Int: Int]()varorientation=[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{varjson=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.

About

Simple JSON Model Parser written in Swift

Resources

Stars

251 stars

Watchers

8 watching

Forks

Releases

Packages

Contributors

Languages