0001 // 0002 // JSONDecodable.swift 0003 // Freddy 0004 // 0005 // Created by Matthew D. Mathias on 3/24/15. 0006 // Copyright © 2015 Big Nerd Ranch. Licensed under MIT. 0007 // 0008 0009 /// A protocol to provide functionality for creating a model object with a `JSON` 0010 /// value. 0011 public protocol JSONDecodable{ 0012 0013 /// Creates an instance of the model with a `JSON` instance. 0014 /// - parameter json: An instance of a `JSON` value from which to 0015 /// construct an instance of the implementing type. 0016 /// - throws: Any `JSON.Error` for errors derived from inspecting the 0017 /// `JSON` value, or any other error involved in decoding. 0018 init
JSONDecodable.swift:22 extension Double: JSONDecodable {JSONDecodable.swift:42 extension Int: JSONDecodable {JSONDecodable.swift:62 extension String: JSONDecodable {JSONDecodable.swift:78 extension Bool: JSONDecodable {JSONDecodable.swift:145 static func getArrayOf<Decoded: JSONDecodable>(json: JSON) throws -> [Decoded] {JSONSubscripting.swift:143 public func decode<Decoded: JSONDecodable>(path: JSONPathType..., type: Decoded.Type = Decoded.self) throws -> Decoded {JSONSubscripting.swift:202 public func arrayOf<Decoded: JSONDecodable>(path: JSONPathType..., type: Decoded.Type = Decoded.self) throws -> [Decoded] {JSONSubscripting.swift:253 public func decode<Decoded: JSONDecodable>(path: JSONPathType..., ifNotFound: Swift.Bool, type: Decoded.Type = Decoded.self) throws -> Decoded? {JSONSubscripting.swift:364 public func arrayOf<Decoded: JSONDecodable>(path: JSONPathType..., ifNotFound: Swift.Bool) throws -> [Decoded]? {JSONSubscripting.swift:407 public func decode<Decoded: JSONDecodable>(path: JSONPathType..., @autoclosure or fallback: () -> Decoded) throws -> Decoded {JSONSubscripting.swift:504 public func arrayOf<Decoded: JSONDecodable>(path: JSONPathType..., @autoclosure or fallback: () -> [Decoded]) throws -> [Decoded] {JSONSubscripting.swift:562 public func decode<Decoded: JSONDecodable>(path: JSONPathType..., ifNull: Swift.Bool, type: Decoded.Type = Decoded.self) throws -> Decoded? {JSONSubscripting.swift:673 public func arrayOf<Decoded: JSONDecodable>(path: JSONPathType..., ifNull: Swift.Bool) throws -> [Decoded]? {(json: JSON) throws 0019 0020 } 0021 0022 extension Double: JSONDecodable { 0023 0024 /// An initializer to create an instance of `Double` from a `JSON` value. 0025 /// - parameter json: An instance of `JSON`. 0026 /// - throws: The initializer will throw an instance of `JSON.Error` if 0027 /// an instance of `Double` cannot be created from the `JSON` value that was 0028 /// passed to this initializer. 0029 public init
JSONDecodable.swift:148 return try getArray(json).map(Decoded.init)JSONSubscripting.swift:144 return try Decoded(json: valueAtPath(path))JSONSubscripting.swift:254 return try optionalAtPath(path, ifNotFound: ifNotFound).map(Decoded.init)JSONSubscripting.swift:408 return try mapOptionalAtPath(path, fallback: fallback, transform: Decoded.init)JSONSubscripting.swift:563 return try mapOptionalAtPath(path, ifNull: ifNull, transform: Decoded.init)(json: JSON) throws { 0030 switch json { 0031 case let .Double(double): 0032 self = double 0033 case let .Int(int): 0034 self = Swift.Double(int) 0035 default: 0036 throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Double) 0037 } 0038 } 0039 0040 } 0041 0042 extension Int: JSONDecodable { 0043 0044 /// An initializer to create an instance of `Int` from a `JSON` value. 0045 /// - parameter json: An instance of `JSON`. 0046 /// - throws: The initializer will throw an instance of `JSON.Error` if 0047 /// an instance of `Int` cannot be created from the `JSON` value that was 0048 /// passed to this initializer. 0049 public init
JSONSubscripting.swift:153 return try Swift.Double(json: valueAtPath(path))JSONSubscripting.swift:272 return try optionalAtPath(path, ifNotFound: ifNotFound).map(Swift.Double.init)JSONSubscripting.swift:418 return try mapOptionalAtPath(path, fallback: fallback, transform: Swift.Double.init)JSONSubscripting.swift:581 return try mapOptionalAtPath(path, ifNull: ifNull, transform: Swift.Double.init)(json: JSON) throws { 0050 switch json { 0051 case let .Double(double): 0052 self = Swift.Int(double) 0053 case let .Int(int): 0054 self = int 0055 default: 0056 throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Int) 0057 } 0058 } 0059 0060 } 0061 0062 extension String: JSONDecodable { 0063 0064 /// An initializer to create an instance of `String` from a `JSON` value. 0065 /// - parameter json: An instance of `JSON`. 0066 /// - throws: The initializer will throw an instance of `JSON.Error` if 0067 /// an instance of `String` cannot be created from the `JSON` value that was 0068 /// passed to this initializer. 0069 public init
JSONSubscripting.swift:162 return try Swift.Int(json: valueAtPath(path))JSONSubscripting.swift:290 return try optionalAtPath(path, ifNotFound: ifNotFound).map(Swift.Int.init)JSONSubscripting.swift:435 return try mapOptionalAtPath(path, fallback: fallback, transform: Swift.Int.init)JSONSubscripting.swift:599 return try mapOptionalAtPath(path, ifNull: ifNull, transform: Swift.Int.init)(json: JSON) throws { 0070 guard case let .String(string) = json else { 0071 throw JSON.Error.ValueNotConvertible(value: json, to: Swift.String) 0072 } 0073 self = string 0074 } 0075 0076 } 0077 0078 extension Bool: JSONDecodable { 0079 0080 /// An initializer to create an instance of `Bool` from a `JSON` value. 0081 /// - parameter json: An instance of `JSON`. 0082 /// - throws: The initializer will throw an instance of `JSON.Error` if 0083 /// an instance of `Bool` cannot be created from the `JSON` value that was 0084 /// passed to this initializer. 0085 public init
JSONSubscripting.swift:171 return try Swift.String(json: valueAtPath(path))JSONSubscripting.swift:308 return try optionalAtPath(path, ifNotFound: ifNotFound).map(Swift.String.init)JSONSubscripting.swift:452 return try mapOptionalAtPath(path, fallback: fallback, transform: Swift.String.init)JSONSubscripting.swift:617 return try mapOptionalAtPath(path, ifNull: ifNull, transform: Swift.String.init)(json: JSON) throws { 0086 guard case let .Bool(bool) = json else { 0087 throw JSON.Error.ValueNotConvertible(value: json, to: Swift.Bool) 0088 } 0089 self = bool 0090 } 0091 0092 } 0093 0094 extension RawRepresentable where RawValue: JSONDecodable { 0095 0096 /// An initializer to create an instance of `RawRepresentable` from a `JSON` value. 0097 /// - parameter json: An instance of `JSON`. 0098 /// - throws: The initializer will throw an instance of `JSON.Error` if 0099 /// an instance of `RawRepresentable` cannot be created from the `JSON` value that was 0100 /// passed to this initializer. 0101 public init(json: JSON) throws { 0102 let raw = try json.decode(type: RawValue.self) 0103 guard let value = Self(rawValue: raw) else { 0104 throw JSON.Error.ValueNotConvertible(value: json, to: Self.self) 0105 } 0106 self = value 0107 } 0108 } 0109 0110 internal extension JSON { 0111 0112 /// Retrieves a `[JSON]` from the JSON. 0113 /// - returns: An `Array` of `JSON` elements 0114 /// - throws: Any of the `JSON.Error` cases thrown by `decode(type:)`. 0115 /// - seealso: `JSON.decode(_:type:)` 0116 static func getArray
JSONSubscripting.swift:180 return try Swift.Bool(json: valueAtPath(path))JSONSubscripting.swift:326 return try optionalAtPath(path, ifNotFound: ifNotFound).map(Swift.Bool.init)JSONSubscripting.swift:469 return try mapOptionalAtPath(path, fallback: fallback, transform: Swift.Bool.init)JSONSubscripting.swift:635 return try mapOptionalAtPath(path, ifNull: ifNull, transform: Swift.Bool.init)(json: JSON) throws -> [JSON] { 0117 // Ideally should be expressed as a conditional protocol implementation on Swift.Array. 0118 guard case let .Array(array) = json else { 0119 throw Error.ValueNotConvertible(value: json, to: Swift.Array<JSON>) 0120 } 0121 return array 0122 } 0123 0124 /// Retrieves a `[String: JSON]` from the JSON. 0125 /// - returns: An `Dictionary` of `String` mapping to `JSON` elements 0126 /// - throws: Any of the `JSON.Error` cases thrown by `decode(type:)`. 0127 /// - seealso: `JSON.decode(_:type:)` 0128 static func getDictionary
JSONDecodable.swift:148 return try getArray(json).map(Decoded.init)JSONSubscripting.swift:189 return try JSON.getArray(valueAtPath(path))JSONSubscripting.swift:345 return try optionalAtPath(path, ifNotFound: ifNotFound).map(JSON.getArray)JSONSubscripting.swift:486 return try mapOptionalAtPath(path, fallback: fallback, transform: JSON.getArray)JSONSubscripting.swift:654 return try mapOptionalAtPath(path, ifNull: ifNull, transform: JSON.getArray)(json: JSON) throws -> [Swift.String: JSON] { 0129 // Ideally should be expressed as a conditional protocol implementation on Swift.Dictionary. 0130 guard case let .Dictionary(dictionary) = json else { 0131 throw Error.ValueNotConvertible(value: json, to: Swift.Dictionary<Swift.String, JSON>) 0132 } 0133 return dictionary 0134 } 0135 0136 /// Attempts to decode many values from a descendant JSON array at a path 0137 /// into JSON. 0138 /// - parameter type: If the context this method is called from does not 0139 /// make the return type clear, pass a type implementing `JSONDecodable` 0140 /// to disambiguate the type to decode with. 0141 /// - returns: An `Array` of decoded elements 0142 /// - throws: Any of the `JSON.Error` cases thrown by `decode(type:)`, as 0143 /// well as any error that arises from decoding the contained values. 0144 /// - seealso: `JSON.decode(_:type:)` 0145 static func getArrayOf
JSONSubscripting.swift:212 return try JSON.getDictionary(valueAtPath(path))JSONSubscripting.swift:384 return try optionalAtPath(path, ifNotFound: ifNotFound).map(JSON.getDictionary)JSONSubscripting.swift:523 return try mapOptionalAtPath(path, fallback: fallback, transform: JSON.getDictionary)JSONSubscripting.swift:694 return try mapOptionalAtPath(path, ifNull: ifNull, transform: JSON.getDictionary)<Decoded: JSONDecodable>(json: JSON) throws -> [Decoded] { 0146 // Ideally should be expressed as a conditional protocol implementation on Swift.Dictionary. 0147 // This implementation also doesn't do the `type = Type.self` trick. 0148 return try getArray(json).map(Decoded.init) 0149 } 0150 0151 } 0152
JSONSubscripting.swift:203 return try JSON.getArrayOf(valueAtPath(path))JSONSubscripting.swift:365 return try optionalAtPath(path, ifNotFound: ifNotFound).map(JSON.getArrayOf)JSONSubscripting.swift:505 return try mapOptionalAtPath(path, fallback: fallback, transform: JSON.getArrayOf)JSONSubscripting.swift:674 return try mapOptionalAtPath(path, ifNull: ifNull, transform: JSON.getArrayOf)