-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathObjectiveC.swift
More file actions
233 lines (209 loc) · 8.54 KB
/
ObjectiveC.swift
File metadata and controls
233 lines (209 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
// ObjectiveC.swift
// PMJSON
//
// Created by Kevin Ballard on 10/9/15.
// Copyright © 2016 Postmates.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
#if os(iOS) || os(OSX) || os(watchOS) || os(tvOS)
import Foundation
extension JSON {
/// Decodes a `Data` as JSON.
/// - Note: Invalid UTF8 sequences in the data are replaced with U+FFFD.
/// - Parameter strict: If `true`, trailing commas in arrays/objects are treated as errors. Default is `false`.
/// - Returns: A `JSON` value.
/// - Throws: `JSONParserError` if the data does not contain valid JSON.
public static func decode(_ data: Data, strict: Bool = false) throws -> JSON {
return try JSON.decode(UTF8Decoder(data: data), strict: strict)
}
/// Encodes a `JSON` to a `Data`.
/// - Parameter json: The `JSON` to encode.
/// - Parameter pretty: If `true`, include extra whitespace for formatting. Default is `false`.
/// - Returns: An `NSData` with the JSON representation of *json*.
public static func encodeAsData(_ json: JSON, pretty: Bool = false) -> Data {
struct Output: TextOutputStream {
// NB: We use NSMutableData instead of Data because it's significantly faster as of Xcode 8b3
var data = NSMutableData()
mutating func write(_ string: String) {
let oldLen = data.length
data.increaseLength(by: string.utf8.count)
let ptr = data.mutableBytes.assumingMemoryBound(to: UInt8.self) + oldLen
for (i, x) in string.utf8.enumerated() {
ptr[i] = x
}
}
}
var output = Output()
JSON.encode(json, toStream: &output, pretty: pretty)
return output.data as Data
}
}
extension JSON {
/// Converts a JSON-compatible Foundation object into a `JSON` value.
/// - Throws: `JSONFoundationError` if the object is not JSON-compatible.
public init(ns: Any) throws {
let object = ns as AnyObject
if object === kCFBooleanTrue {
self = .bool(true)
return
} else if object === kCFBooleanFalse {
self = .bool(false)
return
}
switch object {
case is NSNull:
self = .null
case let n as NSNumber:
let typeChar: UnicodeScalar
let objCType = n.objCType
if objCType[0] == 0 || objCType[1] != 0 {
typeChar = "?"
} else {
typeChar = UnicodeScalar(UInt8(bitPattern: objCType[0]))
}
switch typeChar {
case "c", "i", "s", "l", "q", "C", "I", "S", "L", "B":
self = .int64(n.int64Value)
case "Q": // unsigned long long
let val = n.uint64Value
if val > UInt64(Int64.max) {
fallthrough
}
self = .int64(Int64(val))
default:
self = .double(n.doubleValue)
}
case let s as String:
self = .string(s)
case let dict as NSDictionary:
var obj: [String: JSON] = Dictionary(minimumCapacity: dict.count)
for (key, value) in dict {
guard let key = key as? String else { throw JSONFoundationError.nonStringKey }
obj[key] = try JSON(ns: value)
}
self = .object(JSONObject(obj))
case let array as NSArray:
var ary: JSONArray = []
ary.reserveCapacity(array.count)
for elt in array {
ary.append(try JSON(ns: elt))
}
self = .array(ary)
default:
throw JSONFoundationError.incompatibleType
}
}
/// Returns the JSON as a JSON-compatible Foundation object.
public var ns: Any {
switch self {
case .null: return NSNull()
case .bool(let b): return NSNumber(value: b)
case .string(let s): return s
case .int64(let i): return NSNumber(value: i)
case .double(let d): return d
case .object(let obj): return obj.ns
case .array(let ary):
return ary.map({$0.ns})
}
}
/// Returns the JSON as a JSON-compatible Foundation object, discarding any nulls.
public var nsNoNull: Any? {
switch self {
case .null: return nil
case .bool(let b): return NSNumber(value: b)
case .string(let s): return s
case .int64(let i): return NSNumber(value: i)
case .double(let d): return d
case .object(let obj): return obj.nsNoNull
case .array(let ary):
return ary.flatMap({$0.nsNoNull})
}
}
}
extension JSONObject {
/// Returns the JSON as a JSON-compatible dictionary.
public var ns: [AnyHashable: Any] {
var dict: [AnyHashable: Any] = Dictionary(minimumCapacity: count)
for (key, value) in self {
dict[key] = value.ns
}
return dict
}
/// Returns the JSON as a JSON-compatible dictionary, discarding any nulls.
public var nsNoNull: [AnyHashable: Any] {
var dict: [AnyHashable: Any] = Dictionary(minimumCapacity: count)
for (key, value) in self {
if let value = value.nsNoNull {
dict[key] = value
}
}
return dict
}
}
// MARK: - Errors
/// An error that is thrown when converting from `AnyObject` to `JSON`.
/// - SeeAlso: `JSON.init(ns:)`
public enum JSONFoundationError: Error {
/// Thrown when a non-JSON-compatible type is found.
case incompatibleType
/// Thrown when a dictionary has a key that is not a string.
case nonStringKey
}
extension JSONError: LocalizedError {
public var errorDescription: String? {
return String(describing: self)
}
}
extension JSONParserError: CustomNSError {
public static let errorDomain: String = "PMJSON.JSONParserError"
public var errorCode: Int {
return code.rawValue
}
public var errorUserInfo: [String: Any] {
return [NSLocalizedDescriptionKey: String(describing: self)]
}
}
extension JSONDecoderError: LocalizedError {
public var failureReason: String? {
switch self {
case .streamEnded: return "The JSON event stream ended."
case .unexpectedToken: return "The JSON event stream contained more than one top-level value."
}
}
}
// MARK: -
private struct UTF8Decoder: Sequence {
init(data: Data) {
self.data = data as NSData
}
func makeIterator() -> Iterator {
return Iterator(data: data)
}
private let data: NSData
fileprivate struct Iterator: IteratorProtocol {
init(data: NSData) {
// NB: We use NSData instead of using Data's iterator because it's significantly faster as of Xcode 8b3
self.data = data
let ptr = UnsafeBufferPointer(start: data.bytes.assumingMemoryBound(to: UInt8.self), count: data.length)
iter = ptr.makeIterator()
utf8 = UTF8()
}
mutating func next() -> UnicodeScalar? {
switch utf8.decode(&iter) {
case .scalarValue(let scalar): return scalar
case .error: return "\u{FFFD}"
case .emptyInput: return nil
}
}
private let data: NSData
private var iter: UnsafeBufferPointerIterator<UInt8>
private var utf8: UTF8
}
}
#endif // os(iOS) || os(OSX) || os(watchOS) || os(tvOS)