0001 // 0002 // JSONObject.swift 0003 // PMJSON 0004 // 0005 // Created by Kevin Ballard on 11/10/15. 0006 // Copyright © 2016 Postmates. 0007 // 0008 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or 0009 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license 0010 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your 0011 // option. This file may not be copied, modified, or distributed 0012 // except according to those terms. 0013 // 0014 0015 /// A collection of key-value pairs that maps `String` to `JSON`. 0016 /// 0017 /// This collection abstracts away the underlying representation and allows for JSON-specific 0018 /// methods to be added. 0019 public struct JSONObject{ 0020 /// Creates an empty object. 0021 public init() { 0022 dictionary = [:] 0023 } 0024 0025 /// Creates an object from a sequence of `(String,JSON)` pairs. 0026 public init
Accessors.swift:128 var object: JSONObject? {DecimalNumber.swift:193 public extension JSONObject {Decoder.swift:84 case .ObjectEnd: return .Object(JSONObject(dict))Encoder.swift:100 private static func encodeObject<Target: OutputStreamType>(object: JSONObject, inout toStream stream: Target, indent: Int?) {JSON.swift:30 case Object(JSONObject)JSON.swift:117 self = .Object(JSONObject(elements))JSONError.swift:198 func getObject() throws -> JSONObject {JSONError.swift:206 func getObjectOrNil() throws -> JSONObject? {JSONError.swift:482 func getObject(key: Swift.String) throws -> JSONObject {JSONError.swift:493 func getObjectOrNil(key: Swift.String) throws -> JSONObject? {JSONError.swift:503 func getObject<T>(key: Swift.String, @noescape _ transform: JSONObject throws -> T) throws -> T {JSONError.swift:513 func getObjectOrNil<T>(key: Swift.String, @noescape _ transform: JSONObject throws -> T?) throws -> T? {JSONError.swift:777 func getObject(index: Int) throws -> JSONObject {JSONError.swift:788 func getObjectOrNil(index: Int) throws -> JSONObject? {JSONError.swift:798 func getObject<T>(index: Int, @noescape _ f: JSONObject throws -> T) throws -> T {JSONError.swift:810 func getObjectOrNil<T>(index: Int, @noescape _ f: JSONObject throws -> T?) throws -> T? {JSONError.swift:968 public extension JSONObject {JSONError.swift:1068 func getObject(key: Swift.String) throws -> JSONObject {JSONError.swift:1079 func getObjectOrNil(key: Swift.String) throws -> JSONObject? {JSONError.swift:1089 func getObject<T>(key: Swift.String, @noescape _ f: JSONObject throws -> T) throws -> T {JSONError.swift:1099 func getObjectOrNil<T>(key: Swift.String, @noescape _ f: JSONObject throws -> T?) throws -> T? {JSONError.swift:1147 public extension JSONObject {JSONError.swift:1293 internal func getRequired(dict: JSONObject, key: String, type: JSONError.JSONType) throws -> JSON {JSONObject.swift:47 extension JSONObject: CollectionType {JSONObject.swift:113 extension JSONObject {JSONObject.swift:118 public var keys: LazyMapCollection<JSONObject, String> {JSONObject.swift:125 public var values: LazyMapCollection<JSONObject, JSON> {JSONObject.swift:168 extension JSONObject: DictionaryLiteralConvertible {JSONObject.swift:180 extension JSONObject: CustomStringConvertible, CustomDebugStringConvertible {JSONObject.swift:190 extension JSONObject: Equatable {}JSONObject.swift:192 public func ==(lhs: JSONObject, rhs: JSONObject) -> Bool {JSONObject.swift:192 public func ==(lhs: JSONObject, rhs: JSONObject) -> Bool {JSONObject.swift:196 public func ==(lhs: JSONObject.Index, rhs: JSONObject.Index) -> Bool {JSONObject.swift:196 public func ==(lhs: JSONObject.Index, rhs: JSONObject.Index) -> Bool {JSONObject.swift:200 public func <(lhs: JSONObject.Index, rhs: JSONObject.Index) -> Bool {JSONObject.swift:200 public func <(lhs: JSONObject.Index, rhs: JSONObject.Index) -> Bool {ObjectiveC.swift:101 self = .Object(JSONObject(obj))ObjectiveC.swift:157 extension JSONObject {<S: SequenceType where S.Generator.Element == (String,JSON)>(_ seq: S) { 0027 // optimize for the case where the sequence doesn't contain duplicate keys 0028 dictionary = Dictionary(minimumCapacity: seq.underestimateCount()) 0029 for (key,value) in seq { 0030 dictionary[key] = value 0031 } 0032 } 0033 0034 /// The JSON object represented as a `[String: JSON]`. 0035 public private(set) var dictionary
JSON.swift:117 self = .Object(JSONObject(elements))JSONObject.swift:176 self.init(elements): [String: JSON] 0036 0037 public subscript
JSONObject.swift:22 dictionary = [:]JSONObject.swift:28 dictionary = Dictionary(minimumCapacity: seq.underestimateCount())JSONObject.swift:30 dictionary[key] = valueJSONObject.swift:39 return dictionary[key]JSONObject.swift:42 dictionary[key] = newValueJSONObject.swift:52 return Index(dictionary.startIndex)JSONObject.swift:60 return Index(dictionary.endIndex)JSONObject.swift:65 return dictionary.isEmptyJSONObject.swift:70 return dictionary.countJSONObject.swift:74 return Generator(dictionary.generate())JSONObject.swift:78 return dictionary[position.base]JSONObject.swift:131 return dictionary.indexForKey(key).map(Index.init)JSONObject.swift:139 return dictionary.updateValue(value, forKey: key)JSONObject.swift:146 return dictionary.removeAtIndex(index.base)JSONObject.swift:152 return dictionary.removeValueForKey(key)JSONObject.swift:159 dictionary.removeAll()JSONObject.swift:164 return dictionary.popFirst()JSONObject.swift:171 self.dictionary = dictionaryJSONObject.swift:182 return dictionary.descriptionJSONObject.swift:186 return dictionary.debugDescriptionJSONObject.swift:193 return lhs.dictionary == rhs.dictionaryJSONObject.swift:193 return lhs.dictionary == rhs.dictionary(key: String) -> JSON? { 0038 @inline(__always) get { 0039 return dictionary[key] 0040 } 0041 @inline(__always) set { 0042 dictionary[key] = newValue 0043 } 0044 } 0045 } 0046 0047 extension JSONObject: CollectionType { 0048 /// The position of the first element in a non-empty object. 0049 /// 0050 /// Identical to `endIndex` in an empty object. 0051 public var startIndex: Index { 0052 return Index(dictionary.startIndex) 0053 } 0054 0055 /// The collection's "past the end" position. 0056 /// 0057 /// `endIndex` is not a valid argument to `subscript`, and is always reachable from `startIndex` 0058 /// by zero or more applications of `successor()`. 0059 public var endIndex: Index { 0060 return Index(dictionary.endIndex) 0061 } 0062 0063 /// Returns `true` iff `self` is empty. 0064 public var isEmpty: Bool { 0065 return dictionary.isEmpty 0066 } 0067 0068 /// The number of entries in the object. 0069 public var count
Accessors.swift:181 return object?[key]DecimalNumber.swift:115 guard let value = dict[key] else { return nil }DecimalNumber.swift:138 guard let value = dict[key] else { return nil }DecimalNumber.swift:210 guard let value = self[key] else { return nil }DecimalNumber.swift:231 guard let value = self[key] else { return nil }JSONError.swift:384 guard let value = dict[key] else { return nil }JSONError.swift:405 guard let value = dict[key] else { return nil }JSONError.swift:426 guard let value = dict[key] else { return nil }JSONError.swift:449 guard let value = dict[key] else { return nil }JSONError.swift:470 guard let value = dict[key] else { return nil }JSONError.swift:559 guard let value = dict[key] else { return nil }JSONError.swift:584 guard let value = dict[key] else { return nil }JSONError.swift:608 guard let value = dict[key] else { return nil }JSONError.swift:632 guard let value = dict[key] else { return nil }JSONError.swift:655 guard let value = dict[key] else { return nil }JSONError.swift:983 guard let value = self[key] else { return nil }JSONError.swift:1001 guard let value = self[key] else { return nil }JSONError.swift:1019 guard let value = self[key] else { return nil }JSONError.swift:1039 guard let value = self[key] else { return nil }JSONError.swift:1057 guard let value = self[key] else { return nil }JSONError.swift:1100 guard let value = self[key] else { return nil }JSONError.swift:1142 guard let value = self[key] else { return nil }JSONError.swift:1165 guard let value = self[key] else { return nil }JSONError.swift:1187 guard let value = self[key] else { return nil }JSONError.swift:1209 guard let value = self[key] else { return nil }JSONError.swift:1230 guard let value = self[key] else { return nil }JSONError.swift:1294 guard let value = dict[key] else { throw JSONError.MissingOrInvalidType(path: key, expected: .Required(type), actual: nil) }: Int { 0070 return dictionary.count 0071 } 0072 0073 public func generate() -> Generator { 0074 return Generator(dictionary.generate()) 0075 } 0076 0077 public subscript(position: Index) -> (String,JSON) { 0078 return dictionary[position.base] 0079 } 0080 0081 /// Represents a position in a `JSONObject`. 0082 public struct Index
ObjectiveC.swift:167 var dict: [NSObject: AnyObject] = Dictionary(minimumCapacity: count)ObjectiveC.swift:183 var dict: [NSObject: AnyObject] = Dictionary(minimumCapacity: count): ForwardIndexType, Comparable { 0083 private let base
JSONObject.swift:52 return Index(dictionary.startIndex)JSONObject.swift:51 public var startIndex: Index {JSONObject.swift:60 return Index(dictionary.endIndex)JSONObject.swift:59 public var endIndex: Index {JSONObject.swift:92 public func successor() -> Index {JSONObject.swift:93 return Index(base.successor())JSONObject.swift:130 public func indexForKey(key: String) -> Index? {JSONObject.swift:131 return dictionary.indexForKey(key).map(Index.init)JSONObject.swift:145 public mutating func removeAtIndex(index: Index) -> (String,JSON)? {JSONObject.swift:196 public func ==(lhs: JSONObject.Index, rhs: JSONObject.Index) -> Bool {JSONObject.swift:196 public func ==(lhs: JSONObject.Index, rhs: JSONObject.Index) -> Bool {JSONObject.swift:200 public func <(lhs: JSONObject.Index, rhs: JSONObject.Index) -> Bool {JSONObject.swift:200 public func <(lhs: JSONObject.Index, rhs: JSONObject.Index) -> Bool {: Dictionary<String,JSON>.Index 0084 0085 private init
JSONObject.swift:78 return dictionary[position.base]JSONObject.swift:86 self.base = baseJSONObject.swift:93 return Index(base.successor())JSONObject.swift:146 return dictionary.removeAtIndex(index.base)JSONObject.swift:197 return lhs.base == rhs.baseJSONObject.swift:197 return lhs.base == rhs.baseJSONObject.swift:201 return lhs.base < rhs.baseJSONObject.swift:201 return lhs.base < rhs.base(_ base: Dictionary<String,JSON>.Index) { 0086 self.base = base 0087 } 0088 0089 /// Returns the next consecutive value after `self`. 0090 /// 0091 /// - Requires: The next value is representable. 0092 public func successor() -> Index { 0093 return Index(base.successor()) 0094 } 0095 } 0096 0097 public struct Generator
JSONObject.swift:52 return Index(dictionary.startIndex)JSONObject.swift:60 return Index(dictionary.endIndex)JSONObject.swift:93 return Index(base.successor())JSONObject.swift:131 return dictionary.indexForKey(key).map(Index.init): GeneratorType { 0098 private var base
JSONObject.swift:73 public func generate() -> Generator {JSONObject.swift:74 return Generator(dictionary.generate()): Dictionary<String,JSON>.Generator 0099 0100 private init
JSONObject.swift:101 self.base = baseJSONObject.swift:108 return base.next()(_ base: Dictionary<String,JSON>.Generator) { 0101 self.base = base 0102 } 0103 0104 /// Advance to the next element and return it, or `nil` if no next element exists. 0105 /// 0106 /// - Requires: No preceding call to `self.next()` has returned `nil`. 0107 public mutating func next() -> (String,JSON)? { 0108 return base.next() 0109 } 0110 } 0111 } 0112 0113 extension JSONObject { 0114 /// A collection containing just the keys of `self`. 0115 /// 0116 /// Keys appear in the same order as they occur as the `.0` member of key-value pairs in `self`. 0117 /// Each key in the result has a unique value. 0118 public var keys: LazyMapCollection<JSONObject, String> { 0119 return lazy.map({ $0.0 }) 0120 } 0121 0122 /// A collection containing just the values of `self`. 0123 /// 0124 /// Values appear in the same order as they occur as the `.1` member of key-value pairs in `self`. 0125 public var values: LazyMapCollection<JSONObject, JSON> { 0126 return lazy.map({ $0.1 }) 0127 } 0128 0129 /// Returns the `Index` for the given key, or `nil` if the key is not present in the object. 0130 public func indexForKey(key: String) -> Index? { 0131 return dictionary.indexForKey(key).map(Index.init) 0132 } 0133 0134 /// Update the value stored in the object for the given key, or, if the key does not exist, 0135 /// add a new key-value pair to the object. 0136 /// 0137 /// Returns the value that was replaced, or `nil` if a new key-value pair was added. 0138 public mutating func updateValue(value: JSON, forKey key: String) -> JSON? { 0139 return dictionary.updateValue(value, forKey: key) 0140 } 0141 0142 /// Remove the key-value pair at `index`. 0143 /// 0144 /// Invalidates all indices with respect to `self`. 0145 public mutating func removeAtIndex(index: Index) -> (String,JSON)? { 0146 return dictionary.removeAtIndex(index.base) 0147 } 0148 0149 /// Remove a given key and the associated value from the object. 0150 /// Returns the value that was removed, or `nil` if the key was not present in the object. 0151 public mutating func removeValueForKey(key: String) -> JSON? { 0152 return dictionary.removeValueForKey(key) 0153 } 0154 0155 /// Remove all elements. 0156 /// 0157 /// Invalidates all indices with respect to `self`. 0158 public mutating func removeAll() { 0159 dictionary.removeAll() 0160 } 0161 0162 /// If `!self.isEmpty`, return the first key-value pair in the sequence of elements, otherwise return `nil`. 0163 public mutating func popFirst() -> (String,JSON)? { 0164 return dictionary.popFirst() 0165 } 0166 } 0167 0168 extension JSONObject: DictionaryLiteralConvertible { 0169 /// Creates an object from a dictionary. 0170 public init
JSONObject.swift:74 return Generator(dictionary.generate())(_ dictionary: [String: JSON]) { 0171 self.dictionary = dictionary 0172 } 0173 0174 /// Creates an object initialized with `elements`. 0175 public init(dictionaryLiteral elements: (String, JSON)...) { 0176 self.init(elements) 0177 } 0178 } 0179 0180 extension JSONObject: CustomStringConvertible, CustomDebugStringConvertible { 0181 public var description: String { 0182 return dictionary.description 0183 } 0184 0185 public var debugDescription: String { 0186 return dictionary.debugDescription 0187 } 0188 } 0189 0190 extension JSONObject: Equatable {} 0191 0192 public func ==(lhs: JSONObject, rhs: JSONObject) -> Bool { 0193 return lhs.dictionary == rhs.dictionary 0194 } 0195 0196 public func ==(lhs: JSONObject.Index, rhs: JSONObject.Index) -> Bool { 0197 return lhs.base == rhs.base 0198 } 0199 0200 public func <(lhs: JSONObject.Index, rhs: JSONObject.Index) -> Bool { 0201 return lhs.base < rhs.base 0202 } 0203
Decoder.swift:84 case .ObjectEnd: return .Object(JSONObject(dict))ObjectiveC.swift:101 self = .Object(JSONObject(obj))