0001    //
0002    //  Vapor+Json.swift
0003    //  Vapor
0004    //
0005    //  Created by Logan Wright on 2/19/16.
0006    //  Copyright © 2016 Tanner Nelson. All rights reserved.
0007    //
0008    
0009    import Foundation
0010    
0011    /// To allow users to opt into preferred case style
0012    public typealias JSON = Json
0013    
0014    // MARK: Response
0015    
0016    extension Json: ResponseConvertible {
0017        public func response() -> Response {
0018            let js = serialize()
0019            let data = Array(js.utf8)
0020            return Response(status: .OK, data: data, contentType: .Json)
0021        }
0022    }
0023    
0024    // MARK: Request Json
0025    
0026    extension Request {
0027    
0028        /**
0029         If the body can be serialized as Json, the value will be returned here
0030         */
0031        public var json: Json? {
0032            return try? Json.deserialize(body)
0033        }
0034    }
0035    
0036    // MARK: Json Convertible 
0037    
0038    public enum JsonError
Vapor+JSON.swift:151
            throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)")
Vapor+JSON.swift:166
            throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)")
Vapor+JSON.swift:189
            throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)")
Vapor+JSON.swift:212
            throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)")
Vapor+JSON.swift:246
            throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)")
: ErrorType { 0039 0040 /** 0041 * When converting to a value from Json, if there is a type conflict, this will throw an error 0042 * 0043 * @param Json the json that was unable to map 0044 * @param String a string description of the type that was attempting to map 0045 */ 0046 case UnableToConvert
Vapor+JSON.swift:151
            throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)")
Vapor+JSON.swift:166
            throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)")
Vapor+JSON.swift:189
            throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)")
Vapor+JSON.swift:212
            throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)")
Vapor+JSON.swift:246
            throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)")
(json: Json, toType: String) 0047 } 0048 0049 /** 0050 * An umbrella protocol used to define behavior to and from Json 0051 */ 0052 public protocol JsonConvertible
Vapor+JSON.swift:88
    public init<T: JsonConvertible>(_ any: T) throws {
Vapor+JSON.swift:92
    public init<T: JsonConvertible>(_ any: [T]) throws {
Vapor+JSON.swift:97
    public init<T: JsonConvertible>(_ any: [[T]]) throws {
Vapor+JSON.swift:102
    public init<T: JsonConvertible>(_ any: Set<T>) throws {
Vapor+JSON.swift:107
    public init<T: JsonConvertible>(_ any: [String : T]) throws {
Vapor+JSON.swift:115
    public init<T: JsonConvertible>(_ any: [String : [T]]) throws {
Vapor+JSON.swift:123
    public init<T: JsonConvertible>(_ any: [String : [String : T]]) throws {
Vapor+JSON.swift:132
extension Json : JsonConvertible {
Vapor+JSON.swift:144
extension String : JsonConvertible {
Vapor+JSON.swift:159
extension Bool : JsonConvertible {
Vapor+JSON.swift:175
extension UInt : JsonConvertible {}
Vapor+JSON.swift:176
extension UInt8 : JsonConvertible {}
Vapor+JSON.swift:177
extension UInt16 : JsonConvertible {}
Vapor+JSON.swift:178
extension UInt32 : JsonConvertible {}
Vapor+JSON.swift:179
extension UInt64 : JsonConvertible {}
Vapor+JSON.swift:198
extension Int : JsonConvertible {}
Vapor+JSON.swift:199
extension Int8 : JsonConvertible {}
Vapor+JSON.swift:200
extension Int16 : JsonConvertible {}
Vapor+JSON.swift:201
extension Int32 : JsonConvertible {}
Vapor+JSON.swift:202
extension Int64 : JsonConvertible {}
Vapor+JSON.swift:234
public protocol JsonConvertibleFloatingPointType : JsonConvertible {
{ 0053 0054 /** 0055 This function will be used to create an instance of the type from Json 0056 0057 - parameter json: the json to use in initialization 0058 0059 - throws: a potential error. ie: invalid json type 0060 0061 - returns: an initialized object 0062 */ 0063 static func newInstance(json: Json) throws -> Self 0064 0065 /** 0066 Used to convert the object back to its Json representation 0067 0068 - throws: a potential conversion error 0069 0070 - returns: the object as a Json representation 0071 */ 0072 func jsonRepresentation
Vapor+JSON.swift:89
        self = try any.jsonRepresentation()
() throws -> Json 0073 } 0074 0075 // MARK: Json Convertible Initializers 0076 0077 extension Json { 0078 0079 /** 0080 Create Json from any convertible type 0081 0082 - parameter any: the convertible type 0083 0084 - throws: a potential conversion error 0085 0086 - returns: initialized Json 0087 */ 0088 public init
Vapor+JSON.swift:93
        let mapped = try any.map(Json.init)
Vapor+JSON.swift:103
        let mapped = try any.map(Json.init)
Vapor+JSON.swift:110
            mapped[key] = try Json(val)
<T: JsonConvertible>(_ any: T) throws { 0089 self = try any.jsonRepresentation() 0090 } 0091 0092 public init
Vapor+JSON.swift:98
        let mapped = try any.map(Json.init)
Vapor+JSON.swift:118
            mapped[key] = try Json(val)
<T: JsonConvertible>(_ any: [T]) throws { 0093 let mapped = try any.map(Json.init) 0094 self.init(mapped) 0095 } 0096 0097 public init<T: JsonConvertible>(_ any: [[T]]) throws { 0098 let mapped = try any.map(Json.init) 0099 self.init(mapped) 0100 } 0101 0102 public init<T: JsonConvertible>(_ any: Set<T>) throws { 0103 let mapped = try any.map(Json.init) 0104 self.init(mapped) 0105 } 0106 0107 public init
Vapor+JSON.swift:126
            mapped[key] = try Json(val)
<T: JsonConvertible>(_ any: [String : T]) throws { 0108 var mapped: [String : Json] = [:] 0109 try any.forEach { key, val in 0110 mapped[key] = try Json(val) 0111 } 0112 self.init(mapped) 0113 } 0114 0115 public init<T: JsonConvertible>(_ any: [String : [T]]) throws { 0116 var mapped: [String : Json] = [:] 0117 try any.forEach { key, val in 0118 mapped[key] = try Json(val) 0119 } 0120 self.init(mapped) 0121 } 0122 0123 public init<T: JsonConvertible>(_ any: [String : [String : T]]) throws { 0124 var mapped: [String : Json] = [:] 0125 try any.forEach { key, val in 0126 mapped[key] = try Json(val) 0127 } 0128 self.init(mapped) 0129 } 0130 } 0131 0132 extension Json : JsonConvertible { 0133 public static func newInstance(json: Json) -> Json { 0134 return json 0135 } 0136 0137 public func jsonRepresentation() -> Json { 0138 return self 0139 } 0140 } 0141 0142 // MARK: String 0143 0144 extension String : JsonConvertible { 0145 public func jsonRepresentation() throws -> Json { 0146 return Json(self) 0147 } 0148 0149 public static func newInstance(json: Json) throws -> String { 0150 guard let string = json.stringValue else { 0151 throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)") 0152 } 0153 return string 0154 } 0155 } 0156 0157 // MARK: Boolean 0158 0159 extension Bool : JsonConvertible { 0160 public func jsonRepresentation() throws -> Json { 0161 return Json(self) 0162 } 0163 0164 public static func newInstance(json: Json) throws -> Bool { 0165 guard let bool = json.boolValue else { 0166 throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)") 0167 } 0168 return bool 0169 } 0170 } 0171 0172 0173 // MARK: UnsignedIntegerType 0174 0175 extension UInt : JsonConvertible {} 0176 extension UInt8 : JsonConvertible {} 0177 extension UInt16 : JsonConvertible {} 0178 extension UInt32 : JsonConvertible {} 0179 extension UInt64 : JsonConvertible {} 0180 0181 extension UnsignedIntegerType { 0182 public func jsonRepresentation() throws -> Json { 0183 let double = Double(UIntMax(self.toUIntMax())) 0184 return .from(double) 0185 } 0186 0187 public static func newInstance(json: Json) throws -> Self { 0188 guard let int = json.uintValue else { 0189 throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)") 0190 } 0191 0192 return self.init(int.toUIntMax()) 0193 } 0194 } 0195 0196 // MARK: SignedIntegerType 0197 0198 extension Int : JsonConvertible {} 0199 extension Int8 : JsonConvertible {} 0200 extension Int16 : JsonConvertible {} 0201 extension Int32 : JsonConvertible {} 0202 extension Int64 : JsonConvertible {} 0203 0204 extension SignedIntegerType { 0205 public func jsonRepresentation() throws -> Json { 0206 let double = Double(IntMax(self.toIntMax())) 0207 return .from(double) 0208 } 0209 0210 public static func newInstance(json: Json) throws -> Self { 0211 guard let int = json.intValue else { 0212 throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)") 0213 } 0214 0215 return self.init(int.toIntMax()) 0216 } 0217 } 0218 0219 0220 // MARK: FloatingPointType 0221 0222 extension Float : JsonConvertibleFloatingPointType { 0223 public var doubleValue: Double { 0224 return Double(self) 0225 } 0226 } 0227 0228 extension Double : JsonConvertibleFloatingPointType { 0229 public var doubleValue: Double { 0230 return Double(self) 0231 } 0232 } 0233 0234 public protocol JsonConvertibleFloatingPointType
Vapor+JSON.swift:222
extension Float : JsonConvertibleFloatingPointType {
Vapor+JSON.swift:228
extension Double : JsonConvertibleFloatingPointType {
Vapor+JSON.swift:239
extension JsonConvertibleFloatingPointType {
: JsonConvertible { 0235 var doubleValue
Vapor+JSON.swift:241
        return .from(doubleValue)
: Double { get } 0236 init
Vapor+JSON.swift:248
        return self.init(double)
(_ other: Double) 0237 } 0238 0239 extension JsonConvertibleFloatingPointType { 0240 public func jsonRepresentation() throws -> Json { 0241 return .from(doubleValue) 0242 } 0243 0244 public static func newInstance(json: Json) throws -> Self { 0245 guard let double = json.doubleValue else { 0246 throw JsonError.UnableToConvert(json: json, toType: "\(self.dynamicType)") 0247 } 0248 return self.init(double) 0249 } 0250 } 0251