0001 // 0002 // JSONEncodable.swift 0003 // Freddy 0004 // 0005 // Created by Matthew Mathias on 1/4/16. 0006 // Copyright © 2016 Big Nerd Ranch. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 /// A protocol to facilitate encoding and decoding of `JSON`. 0012 public protocol JSONEncodable{ 0013 /// Converts an instance of a conforming type to `JSON`. 0014 /// - returns: An instance of `JSON`. 0015 /// - Note: If conforming to `JSONEncodable` with a custom type of your own, you should return an instance of 0016 /// `JSON.Dictionary`. 0017 func toJSON
JSONEncodable.swift:45 extension Int: JSONEncodable {JSONEncodable.swift:53 extension Double: JSONEncodable {JSONEncodable.swift:61 extension String: JSONEncodable {JSONEncodable.swift:69 extension Bool: JSONEncodable {() -> JSON 0018 } 0019 0020 extension Array where Element: JSONEncodable { 0021 /// Converts an instance of `Array` whose elements conform to `JSONEncodable` to `JSON`. 0022 /// - returns: An instance of `JSON` where the enum case is `.Array`. 0023 public func toJSON() -> JSON { 0024 let arrayOfJSON = self.map { $0.toJSON() } 0025 return .Array(arrayOfJSON) 0026 } 0027 } 0028 0029 extension Dictionary where Value: JSONEncodable { 0030 /// Converts an instance of `Dictionary` whose values conform to `JSONEncodable` to `JSON`. The keys in the resulting 0031 /// `JSON.Dictionary` will be of type `String`. 0032 /// - returns: An instance of `JSON` where the enum case is `.Dictionary`. 0033 public func toJSON() -> JSON { 0034 var jsonDictionary = [String: JSON]() 0035 0036 for (k, v) in self { 0037 let key = String(k) 0038 jsonDictionary[key] = v.toJSON() 0039 } 0040 0041 return .Dictionary(jsonDictionary) 0042 } 0043 } 0044 0045 extension Int: JSONEncodable { 0046 /// Converts an instance of a conforming type to `JSON`. 0047 /// - returns: An instance of `JSON` where the enum case is `.Int`. 0048 public func toJSON() -> JSON { 0049 return .Int(self) 0050 } 0051 } 0052 0053 extension Double: JSONEncodable { 0054 /// Converts an instance of a conforming type to `JSON`. 0055 /// - returns: An instance of `JSON` where the enum case is `.Double`. 0056 public func toJSON() -> JSON { 0057 return .Double(self) 0058 } 0059 } 0060 0061 extension String: JSONEncodable { 0062 /// Converts an instance of a conforming type to `JSON`. 0063 /// - returns: An instance of `JSON` where the enum case is `.String`. 0064 public func toJSON() -> JSON { 0065 return .String(self) 0066 } 0067 } 0068 0069 extension Bool: JSONEncodable { 0070 /// Converts an instance of a conforming type to `JSON`. 0071 /// - returns: An instance of `JSON` where the enum case is `.Bool`. 0072 public func toJSON() -> JSON { 0073 return .Bool(self) 0074 } 0075 } 0076 0077 extension RawRepresentable where RawValue: JSONEncodable { 0078 /// Converts an instance of a conforming type to `JSON`. 0079 /// - returns: An instance of `JSON` where the enum case is whatever the underlying `RawValue` converts to. 0080 public func toJSON() -> JSON { 0081 return rawValue.toJSON() 0082 } 0083 } 0084
JSONEncodable.swift:24 let arrayOfJSON = self.map { $0.toJSON() }JSONEncodable.swift:38 jsonDictionary[key] = v.toJSON()JSONEncodable.swift:81 return rawValue.toJSON()