0001    //
0002    //  Encoder.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    /**
0029    Set of functions used to encode values to JSON
0030    */
0031    public struct Encoder
Encoder.swift:107
        return Encoder.encodeDate(key, dateFormatter: dateFormatter)
Encoder.swift:284
        return Encoder.encodeDateArray(key, dateFormatter: GlossDateFormatterISO8601())
Operators.swift:102
    return Encoder.encode(key)(property)
Operators.swift:109
    return Encoder.encodeEncodable(key)(property)
Operators.swift:116
    return Encoder.encodeEnum(key)(property)
Operators.swift:123
    return Encoder.encodeURL(key)(property)
Operators.swift:130
    return Encoder.encodeArray(key)(property)
Operators.swift:137
    return Encoder.encodeEncodableArray(key)(property)
Operators.swift:144
    return Encoder.encodeEnumArray(key)(property)
Operators.swift:151
    return Encoder.encodeEncodableDictionary(key)(property)
{ 0032 0033 // MARK: - Encoders 0034 0035 /** 0036 Returns function to encode value to JSON 0037 0038 :parameter: key Key used to create JSON property 0039 0040 :returns: Function decoding value to optional JSON 0041 */ 0042 public static func encode
Operators.swift:102
    return Encoder.encode(key)(property)
<T>(key: String) -> T? -> JSON? { 0043 return { 0044 property in 0045 0046 if let property = property as? AnyObject { 0047 return [key : property] 0048 } 0049 0050 return nil 0051 } 0052 } 0053 0054 /** 0055 Returns function to encode value to JSON 0056 for objects the conform to the Encodable protocol 0057 0058 :parameter: key Key used to create JSON property 0059 0060 :returns: Function decoding value to optional JSON 0061 */ 0062 public static func encodeEncodable
Operators.swift:109
    return Encoder.encodeEncodable(key)(property)
<T: Encodable>(key: String) -> T? -> JSON? { 0063 return { 0064 model in 0065 0066 if let model = model, json = model.toJSON() { 0067 return [key : json] 0068 } 0069 0070 return nil 0071 } 0072 } 0073 0074 /** 0075 Returns function to encode date as JSON 0076 0077 :parameter: key Key used to create JSON property 0078 :parameter: dateFormatter Formatter used to format date string 0079 0080 :returns: Function encoding date to optional JSON 0081 */ 0082 public static func encodeDate
Encoder.swift:107
        return Encoder.encodeDate(key, dateFormatter: dateFormatter)
(key: String, dateFormatter: NSDateFormatter) -> NSDate? -> JSON? { 0083 return { 0084 date in 0085 0086 if let date = date { 0087 return [key : dateFormatter.stringFromDate(date)] 0088 } 0089 0090 return nil 0091 } 0092 } 0093 0094 /** 0095 Returns function to encode ISO8601 date as JSON 0096 0097 :parameter: key Key used to create JSON property 0098 :parameter: dateFormatter Formatter used to format date string 0099 0100 :returns: Function encoding ISO8601 date to optional JSON 0101 */ 0102 public static func encodeDateISO8601(key: String) -> NSDate? -> JSON? { 0103 let dateFormatter = NSDateFormatter() 0104 dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") 0105 dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ" 0106 0107 return Encoder.encodeDate(key, dateFormatter: dateFormatter) 0108 } 0109 0110 /** 0111 Returns function to encode enum value as JSON 0112 0113 :parameter: key Key used to create JSON property 0114 0115 :returns: Function encoding enum value to optional JSON 0116 */ 0117 public static func encodeEnum
Operators.swift:116
    return Encoder.encodeEnum(key)(property)
<T: RawRepresentable>(key: String) -> T? -> JSON? { 0118 return { 0119 enumValue in 0120 0121 if let enumValue = enumValue { 0122 return [key : enumValue.rawValue as! AnyObject] 0123 } 0124 0125 return nil 0126 } 0127 } 0128 0129 /** 0130 Returns function to encode URL as JSON 0131 0132 :parameter: key Key used to create JSON property 0133 0134 :returns: Function encoding URL to optional JSON 0135 */ 0136 public static func encodeURL
Operators.swift:123
    return Encoder.encodeURL(key)(property)
(key: String) -> NSURL? -> JSON? { 0137 return { 0138 url in 0139 0140 if let url = url { 0141 return [key : url.absoluteString] 0142 } 0143 0144 return nil 0145 } 0146 } 0147 0148 /** 0149 Returns function to encode array as JSON 0150 0151 :parameter: key Key used to create JSON property 0152 0153 :returns: Function encoding array to optional JSON 0154 */ 0155 public static func encodeArray
Operators.swift:130
    return Encoder.encodeArray(key)(property)
<T>(key: String) -> [T]? -> JSON? { 0156 return { 0157 array in 0158 0159 if let array = array as? AnyObject { 0160 return [key : array] 0161 } 0162 0163 return nil 0164 } 0165 } 0166 0167 /** 0168 Returns function to encode array as JSON 0169 for objects the conform to the Encodable protocol 0170 0171 :parameter: key Key used to create JSON property 0172 0173 :returns: Function encoding array to optional JSON 0174 */ 0175 public static func encodeEncodableArray
Operators.swift:137
    return Encoder.encodeEncodableArray(key)(property)
<T: Encodable>(key: String) -> [T]? -> JSON? { 0176 return { 0177 array in 0178 0179 if let array = array { 0180 var encodedArray: [JSON] = [] 0181 0182 for model in array { 0183 if let json = model.toJSON() { 0184 encodedArray.append(json) 0185 } 0186 } 0187 0188 return [key : encodedArray] 0189 } 0190 0191 return nil 0192 } 0193 } 0194 0195 /** 0196 Returns function to encode a [String : Encodable] into JSON 0197 for objects the conform to the Encodable protocol 0198 0199 :parameter: key Key used to create JSON property 0200 0201 :returns: Function encoding dictionary to optional JSON 0202 */ 0203 public static func encodeEncodableDictionary
Operators.swift:151
    return Encoder.encodeEncodableDictionary(key)(property)
<T: Encodable>(key: String) -> [String : T]? -> JSON? { 0204 return { 0205 dictionary in 0206 0207 guard let dictionary = dictionary else { 0208 return nil 0209 } 0210 0211 return dictionary.flatMap { (key, value) in 0212 guard let json = value.toJSON() else { 0213 return nil 0214 } 0215 0216 return (key, json) 0217 } 0218 } 0219 } 0220 0221 /** 0222 Returns function to encode array as JSON 0223 of enum raw values 0224 0225 :parameter: key Key used to create JSON property 0226 0227 :returns: Function encoding array to optional JSON 0228 */ 0229 public static func encodeEnumArray
Operators.swift:144
    return Encoder.encodeEnumArray(key)(property)
<T: RawRepresentable>(key: String) -> [T]? -> JSON? { 0230 return { 0231 enumValues in 0232 0233 if let enumValues = enumValues { 0234 var rawValues: [T.RawValue] = [] 0235 0236 for enumValue in enumValues { 0237 rawValues.append(enumValue.rawValue) 0238 } 0239 0240 return [key : rawValues as! AnyObject] 0241 } 0242 0243 return nil 0244 } 0245 } 0246 0247 /** 0248 Returns function to encode date array as JSON 0249 0250 :parameter: key Key used to create JSON property 0251 :parameter: dateFormatter Formatter used to format date string 0252 0253 :returns: Function encoding date array to optional JSON 0254 */ 0255 public static func encodeDateArray
Encoder.swift:284
        return Encoder.encodeDateArray(key, dateFormatter: GlossDateFormatterISO8601())
(key: String, dateFormatter: NSDateFormatter) -> [NSDate]? -> JSON? { 0256 return { 0257 dates in 0258 0259 if let dates = dates { 0260 var dateStrings: [String] = [] 0261 0262 for date in dates { 0263 let dateString = dateFormatter.stringFromDate(date) 0264 0265 dateStrings.append(dateString) 0266 } 0267 0268 return [key : dateStrings] 0269 } 0270 0271 return nil 0272 } 0273 } 0274 0275 /** 0276 Returns function to encode ISO8601 date array as JSON 0277 0278 :parameter: key Key used to create JSON property 0279 :parameter: dateFormatter Formatter used to format date string 0280 0281 :returns: Function encoding ISO8601 date array to optional JSON 0282 */ 0283 public static func encodeDateISO8601Array(key: String) -> [NSDate]? -> JSON? { 0284 return Encoder.encodeDateArray(key, dateFormatter: GlossDateFormatterISO8601()) 0285 } 0286 0287 } 0288