0001    //
0002    //  Operators.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: - Operator <~~ (Decode)
0029    
0030    /**
0031    Decode custom operator
0032    */
0033    infix operator <~~ { associativity left precedence 150 }
0034    
0035    /**
0036    Convenience operator for decoding JSON to generic value
0037    */
0038    public func <~~ <T>(key: String, json: JSON) -> T? {
0039        return Decoder.decode(key)(json)
0040    }
0041    
0042    /**
0043    Convenience operator for decoding JSON to Decodable object
0044    */
0045    public func <~~ <T: Decodable>(key: String, json: JSON) -> T? {
0046        return Decoder.decodeDecodable(key)(json)
0047    }
0048    
0049    /**
0050    Convenience operator for decoding JSON to enum value
0051    */
0052    public func <~~ <T: RawRepresentable>(key: String, json: JSON) -> T? {
0053        return Decoder.decodeEnum(key)(json)
0054    }
0055    
0056    /**
0057    Convenience operator for decoding JSON to NSURL
0058    */
0059    public func <~~ (key: String, json: JSON) -> NSURL? {
0060        return Decoder.decodeURL(key)(json)
0061    }
0062    
0063    /**
0064    Convenience operator for decoding JSON to array of enum values
0065    */
0066    public func <~~ <T: RawRepresentable>(key: String, json: JSON) -> [T]? {
0067        return Decoder.decodeEnumArray(key)(json)
0068    }
0069    
0070    /**
0071    Convenience operator for decoding JSON to array of Decodable objects
0072    */
0073    public func <~~ <T: Decodable>(key: String, json: JSON) -> [T]? {
0074        return Decoder.decodeDecodableArray(key)(json)
0075    }
0076    
0077    /**
0078     Convenience operator for decoding JSON to array of URLs
0079     */
0080    public func <~~ (key: String, json: JSON) -> [NSURL]? {
0081        return Decoder.decodeURLArray(key)(json)
0082    }
0083    
0084    /**
0085     Convenience operator for decoding JSON to Dictionary of String, Decodable objects
0086     */
0087    public func <~~ <T: Decodable>(key: String, json: JSON) -> [String : T]? {
0088        return Decoder.decodeDecodableDictionary(key)(json)
0089    }
0090    
0091    // MARK: - Operator ~~> (Encode)
0092    
0093    /**
0094    Encode custom operator
0095    */
0096    infix operator ~~> { associativity left precedence 150 }
0097    
0098    /**
0099    Convenience operator for encoding generic value to JSON
0100    */
0101    public func ~~> <T>(key: String, property: T?) -> JSON? {
0102        return Encoder.encode(key)(property)
0103    }
0104    
0105    /**
0106    Convenience operator for encoding Encodable object to JSON
0107    */
0108    public func ~~> <T: Encodable>(key: String, property: T?) -> JSON? {
0109        return Encoder.encodeEncodable(key)(property)
0110    }
0111    
0112    /**
0113    Convenience operator for encoding enum value to JSON
0114    */
0115    public func ~~> <T: RawRepresentable>(key: String, property: T?) -> JSON? {
0116        return Encoder.encodeEnum(key)(property)
0117    }
0118    
0119    /**
0120    Convenience operator for encoding NSURL to JSON
0121    */
0122    public func ~~> (key: String, property: NSURL?) -> JSON? {
0123        return Encoder.encodeURL(key)(property)
0124    }
0125    
0126    /**
0127    Convenience operator for encoding array of generic objects to JSON
0128    */
0129    public func ~~> <T>(key: String, property: [T]?) -> JSON? {
0130        return Encoder.encodeArray(key)(property)
0131    }
0132    
0133    /**
0134    Convenience operator for encoding array of Encodable objects to JSON
0135    */
0136    public func ~~> <T: Encodable>(key: String, property: [T]?) -> JSON? {
0137        return Encoder.encodeEncodableArray(key)(property)
0138    }
0139    
0140    /**
0141    Convenience operator for encoding array of enum values to JSON
0142    */
0143    public func ~~> <T: RawRepresentable>(key: String, property: [T]?) -> JSON? {
0144        return Encoder.encodeEnumArray(key)(property)
0145    }
0146    
0147    /**
0148     Convenience operator for encoding dictionary of Encodable objects to JSON
0149     */
0150    public func ~~> <T: Encodable>(key: String, property: [String : T]?) -> JSON? {
0151        return Encoder.encodeEncodableDictionary(key)(property)
0152    }
0153