0001 // 0002 // Gloss.swift 0003 // Gloss 0004 // 0005 // Copyright (c) 2015 Harlan Kellaway 0006 // 0007 // Permission is hereby granted, free of charge, to any person obtaining a copy 0008 // of this software and associated documentation files (the "Software"), to deal 0009 // in the Software without restriction, including without limitation the rights 0010 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0011 // copies of the Software, and to permit persons to whom the Software is 0012 // furnished to do so, subject to the following conditions: 0013 // 0014 // The above copyright notice and this permission notice shall be included in 0015 // all copies or substantial portions of the Software. 0016 // 0017 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0018 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0019 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0020 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0021 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0022 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 0023 // THE SOFTWARE. 0024 // 0025 0026 import Foundation 0027 0028 // MARK: - Types 0029 0030 public typealias JSON= [String : AnyObject] 0031 0032 // MARK: - Protocols 0033 0034 /** 0035 Convenience protocol for objects that can be 0036 translated from and to JSON 0037 */ 0038 public protocol Glossy: Decodable, Encodable { } 0039 0040 /** 0041 Enables an object to be decoded from JSON 0042 */ 0043 public protocol Decodable
Decoder.swift:43 public static func decode<T>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> T? {Decoder.swift:64 public static func decodeDecodable<T: Decodable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> T? {Decoder.swift:68 if let subJSON = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? JSON {Decoder.swift:86 public static func decodeDate(key: String, dateFormatter: NSDateFormatter, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> NSDate? {Decoder.swift:107 public static func decodeDateISO8601(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> NSDate? {Decoder.swift:123 public static func decodeEnum<T: RawRepresentable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> T? {Decoder.swift:143 public static func decodeURL(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> NSURL? {Decoder.swift:165 public static func decodeDecodableArray<T: Decodable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [T]? {Decoder.swift:169 if let jsonArray = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? [JSON] {Decoder.swift:194 public static func decodeDecodableDictionary<T:Decodable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [String:T]? {Decoder.swift:198 guard let dictionary = json.valueForKeyPath(key, withDelimiter: keyPathDelimiter) as? [String : JSON] else {Decoder.swift:221 public static func decodeEnumArray<T: RawRepresentable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [T]? {Decoder.swift:250 public static func decodeDateArray(key: String, dateFormatter: NSDateFormatter, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [NSDate]? {Decoder.swift:279 public static func decodeDateISO8601Array(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [NSDate]? {Decoder.swift:295 public static func decodeURLArray(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [NSURL]? {Encoder.swift:42 public static func encode<T>(key: String) -> T? -> JSON? {Encoder.swift:62 public static func encodeEncodable<T: Encodable>(key: String) -> T? -> JSON? {Encoder.swift:82 public static func encodeDate(key: String, dateFormatter: NSDateFormatter) -> NSDate? -> JSON? {Encoder.swift:102 public static func encodeDateISO8601(key: String) -> NSDate? -> JSON? {Encoder.swift:117 public static func encodeEnum<T: RawRepresentable>(key: String) -> T? -> JSON? {Encoder.swift:136 public static func encodeURL(key: String) -> NSURL? -> JSON? {Encoder.swift:155 public static func encodeArray<T>(key: String) -> [T]? -> JSON? {Encoder.swift:175 public static func encodeEncodableArray<T: Encodable>(key: String) -> [T]? -> JSON? {Encoder.swift:180 var encodedArray: [JSON] = []Encoder.swift:203 public static func encodeEncodableDictionary<T: Encodable>(key: String) -> [String : T]? -> JSON? {Encoder.swift:229 public static func encodeEnumArray<T: RawRepresentable>(key: String) -> [T]? -> JSON? {Encoder.swift:255 public static func encodeDateArray(key: String, dateFormatter: NSDateFormatter) -> [NSDate]? -> JSON? {Encoder.swift:283 public static func encodeDateISO8601Array(key: String) -> [NSDate]? -> JSON? {ExtensionArray.swift:38 static func fromJSONArray(jsonArray: [JSON]) -> [Element] {ExtensionArray.swift:61 func toJSONArray() -> [JSON]? {ExtensionArray.swift:62 var jsonArray: [JSON] = []ExtensionDictionary.swift:55 if !keys.isEmpty, let subDict = value as? JSON {ExtensionDictionary.swift:130 var subdict: JSON = [:]ExtensionDictionary.swift:132 if let sub = self[first] as? JSON {Gloss.swift:50 init?(json: JSON)Gloss.swift:62 func toJSON() -> JSON?Gloss.swift:105 public func jsonify(array: [JSON?], keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON? {Gloss.swift:105 public func jsonify(array: [JSON?], keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON? {Gloss.swift:106 var json: JSON = [:]Operators.swift:38 public func <~~ <T>(key: String, json: JSON) -> T? {Operators.swift:45 public func <~~ <T: Decodable>(key: String, json: JSON) -> T? {Operators.swift:52 public func <~~ <T: RawRepresentable>(key: String, json: JSON) -> T? {Operators.swift:59 public func <~~ (key: String, json: JSON) -> NSURL? {Operators.swift:66 public func <~~ <T: RawRepresentable>(key: String, json: JSON) -> [T]? {Operators.swift:73 public func <~~ <T: Decodable>(key: String, json: JSON) -> [T]? {Operators.swift:80 public func <~~ (key: String, json: JSON) -> [NSURL]? {Operators.swift:87 public func <~~ <T: Decodable>(key: String, json: JSON) -> [String : T]? {Operators.swift:101 public func ~~> <T>(key: String, property: T?) -> JSON? {Operators.swift:108 public func ~~> <T: Encodable>(key: String, property: T?) -> JSON? {Operators.swift:115 public func ~~> <T: RawRepresentable>(key: String, property: T?) -> JSON? {Operators.swift:122 public func ~~> (key: String, property: NSURL?) -> JSON? {Operators.swift:129 public func ~~> <T>(key: String, property: [T]?) -> JSON? {Operators.swift:136 public func ~~> <T: Encodable>(key: String, property: [T]?) -> JSON? {Operators.swift:143 public func ~~> <T: RawRepresentable>(key: String, property: [T]?) -> JSON? {Operators.swift:150 public func ~~> <T: Encodable>(key: String, property: [String : T]?) -> JSON? {{ 0044 0045 /** 0046 Returns new instance created from provided JSON 0047 0048 :parameter: json JSON representation of object 0049 */ 0050 init
Decoder.swift:64 public static func decodeDecodable<T: Decodable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> T? {Decoder.swift:165 public static func decodeDecodableArray<T: Decodable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [T]? {Decoder.swift:194 public static func decodeDecodableDictionary<T:Decodable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [String:T]? {Gloss.swift:38 public protocol Glossy: Decodable, Encodable { }Operators.swift:45 public func <~~ <T: Decodable>(key: String, json: JSON) -> T? {Operators.swift:73 public func <~~ <T: Decodable>(key: String, json: JSON) -> [T]? {Operators.swift:87 public func <~~ <T: Decodable>(key: String, json: JSON) -> [String : T]? {?(json: JSON) 0051 0052 } 0053 0054 /** 0055 Enables an object to be encoded to JSON 0056 */ 0057 public protocol Encodable
Decoder.swift:69 return T(json: subJSON)Decoder.swift:173 if let model = T(json: subJSON) {Decoder.swift:203 guard let decoded = T(json: value) else {ExtensionArray.swift:42 let model = Element(json: json){ 0058 0059 /** 0060 Object encoded as JSON 0061 */ 0062 func toJSON
Encoder.swift:62 public static func encodeEncodable<T: Encodable>(key: String) -> T? -> JSON? {Encoder.swift:175 public static func encodeEncodableArray<T: Encodable>(key: String) -> [T]? -> JSON? {Encoder.swift:203 public static func encodeEncodableDictionary<T: Encodable>(key: String) -> [String : T]? -> JSON? {Gloss.swift:38 public protocol Glossy: Decodable, Encodable { }Operators.swift:108 public func ~~> <T: Encodable>(key: String, property: T?) -> JSON? {Operators.swift:136 public func ~~> <T: Encodable>(key: String, property: [T]?) -> JSON? {Operators.swift:150 public func ~~> <T: Encodable>(key: String, property: [String : T]?) -> JSON? {() -> JSON? 0063 0064 } 0065 0066 // MARK: - Global functions 0067 0068 private var dateFormatterISO8601
Encoder.swift:66 if let model = model, json = model.toJSON() {Encoder.swift:183 if let json = model.toJSON() {Encoder.swift:212 guard let json = value.toJSON() else {ExtensionArray.swift:65 if let json = json.toJSON() {: NSDateFormatter? 0069 0070 /** 0071 Date formatter used for ISO8601 dates. 0072 0073 - returns: Date formatter. 0074 */ 0075 public func GlossDateFormatterISO8601
Gloss.swift:76 if let _ = dateFormatterISO8601 {Gloss.swift:77 return dateFormatterISO8601!Gloss.swift:80 dateFormatterISO8601 = NSDateFormatter()Gloss.swift:81 dateFormatterISO8601!.locale = NSLocale(localeIdentifier: "en_US_POSIX")Gloss.swift:82 dateFormatterISO8601!.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"Gloss.swift:84 return dateFormatterISO8601!() -> NSDateFormatter { 0076 if let _ = dateFormatterISO8601 { 0077 return dateFormatterISO8601! 0078 } 0079 0080 dateFormatterISO8601 = NSDateFormatter() 0081 dateFormatterISO8601!.locale = NSLocale(localeIdentifier: "en_US_POSIX") 0082 dateFormatterISO8601!.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ" 0083 0084 return dateFormatterISO8601! 0085 } 0086 0087 /** 0088 Default delimiter used for nested key path keys 0089 */ 0090 public func GlossKeyPathDelimiter
Encoder.swift:284 return Encoder.encodeDateArray(key, dateFormatter: GlossDateFormatterISO8601())() -> String { 0091 0092 return "." 0093 0094 } 0095 0096 /** 0097 Transforms an array of JSON optionals 0098 to a single optional JSON dictionary 0099 0100 :parameter: array Array of JSON to transform 0101 :parameter: keyPathDelimiter Delimiter used for nested keypath keys 0102 0103 - returns: JSON 0104 */ 0105 public func jsonify(array: [JSON?], keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON? { 0106 var json: JSON = [:] 0107 0108 for j in array { 0109 if(j != nil) { 0110 json.add(j!, delimiter: keyPathDelimiter) 0111 } 0112 } 0113 0114 return json 0115 } 0116
Decoder.swift:43 public static func decode<T>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> T? {Decoder.swift:64 public static func decodeDecodable<T: Decodable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> T? {Decoder.swift:86 public static func decodeDate(key: String, dateFormatter: NSDateFormatter, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> NSDate? {Decoder.swift:107 public static func decodeDateISO8601(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> NSDate? {Decoder.swift:123 public static func decodeEnum<T: RawRepresentable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> T? {Decoder.swift:143 public static func decodeURL(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> NSURL? {Decoder.swift:165 public static func decodeDecodableArray<T: Decodable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [T]? {Decoder.swift:194 public static func decodeDecodableDictionary<T:Decodable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [String:T]? {Decoder.swift:221 public static func decodeEnumArray<T: RawRepresentable>(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [T]? {Decoder.swift:250 public static func decodeDateArray(key: String, dateFormatter: NSDateFormatter, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [NSDate]? {Decoder.swift:279 public static func decodeDateISO8601Array(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [NSDate]? {Decoder.swift:295 public static func decodeURLArray(key: String, keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON -> [NSURL]? {ExtensionDictionary.swift:41 public func valueForKeyPath(keyPath: String, withDelimiter delimiter: String = GlossKeyPathDelimiter()) -> AnyObject? {ExtensionDictionary.swift:97 mutating func add(other: Dictionary, delimiter: String = GlossKeyPathDelimiter()) -> () {ExtensionDictionary.swift:116 private mutating func setValue(valueToSet val: Any, forKeyPath keyPath: String, withDelimiter delimiter: String = GlossKeyPathDelimiter()) {Gloss.swift:105 public func jsonify(array: [JSON?], keyPathDelimiter: String = GlossKeyPathDelimiter()) -> JSON? {