-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCodableSupport.swift
278 lines (210 loc) · 6.33 KB
/
CodableSupport.swift
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//
// JavaScriptKit
// Copyright (c) 2017 - present Alexis Aubry. Licensed under the MIT license.
//
import Foundation
/**
* An encoding container.
*/
enum JSCodingContainer {
/// A single value container associated with a single-value storage.
case singleValue(SingleValueStorage)
/// An unkeyed value container associated with a reference to an array storage.
case unkeyed(ArrayStorage)
/// A keyed value container associated with a reference to a dictionary storage.
case keyed(DictionaryStorage)
}
// MARK: - Single Value Storage
/**
* A storage container for JavaScript encoder/decoder.
*/
enum SingleValueStorage {
/// A `null` value.
case null
/// A String value.
case string(String)
/// A Bool value.
case boolean(Bool)
/// A number value.
case number(NSNumber)
/// A date value.
case date(Date)
/// An empty object.
case emptyObject
/// The stored value.
var storedValue: Any {
switch self {
case .null:
return NSNull()
case .string(let string):
return string
case .boolean(let bool):
return bool
case .number(let number):
return number
case .date(let date):
return date
case .emptyObject:
return [String: Any]()
}
}
/// The type of the stored value.
var storedType: Any.Type {
switch self {
case .null: return NSNull.self
case .string: return String.self
case .boolean: return Bool.self
case .number: return NSNumber.self
case .date: return Date.self
case .emptyObject: return Dictionary<String, Any>.self
}
}
// MARK: Initialization
/// Decodes the stored value.
init(storedValue: Any) throws {
if storedValue is NSNull {
self = .null
} else if storedValue is String {
self = .string(storedValue as! String)
} else if storedValue is Date {
self = .date(storedValue as! Date)
} else if storedValue is NSNumber {
self = .number(storedValue as! NSNumber)
} else {
let context = DecodingError.Context(codingPath: [], debugDescription: "Could not decode \(storedValue) because its type is not supported. Supported types include null, booleans, strings, numbers and dates.")
throw DecodingError.dataCorrupted(context)
}
}
}
// MARK: - Array Storage
/**
* An object that holds a reference to an array. Use this class when you need an Array with
* reference semantics.
*/
class ArrayStorage {
/// The underlying array object.
private var array: [Any]
// MARK: Initialization
/// Creates an empty array storage.
init() {
array = [Any]()
}
/// Creates an array storage by copying the contents of an existing array.
init(_ array: NSArray) {
self.array = array as! [Any]
}
// MARK: Array Interaction
/// The number of elements in the Array.
var count: Int {
return array.count
}
/// Appends an element to the array.
func append(_ element: Any) {
array.append(element)
}
/// Get the value at the given index.
subscript(index: Int) -> Any {
get {
return array[index]
}
set {
array[index] = newValue
}
}
// MARK: Contents
/// An immutable reference to the contents of the array storage.
var body: [Any] {
return array
}
}
// MARK: - Dictionary Storage
/**
* An object that holds a reference to a dictionary. Use this class when you need a Dictionary with
* reference semantics.
*/
class DictionaryStorage {
/// The underlying dictionary.
private var dictionary: [AnyHashable: Any]
// MARK: Initialization
/// Creates an empty dictionary storage.
init() {
dictionary = [AnyHashable: Any]()
}
/// Creates a dictionary storage by copying the contents of an existing dictionary.
init(_ dictionary: NSDictionary) {
self.dictionary = dictionary as! [AnyHashable: Any]
}
// MARK: Dictionary Interaction
/// Access the value of the dictionary for the given key.
subscript(key: String) -> Any? {
get {
return dictionary[key]
}
set {
dictionary[key] = newValue
}
}
// MARK: Contents
/// An immutable reference to the contents of the dictionary storage.
var body: [AnyHashable: Any] {
return dictionary
}
/// The keys indexing the storage contents.
var keys: Dictionary<AnyHashable, Any>.Keys {
return dictionary.keys
}
}
// MARK: - Escaping
extension String {
/// Escapes the JavaScript special characters.
internal var escapingSpecialCharacters: String {
let controlCharactersRange = UnicodeScalar(0x08) ... UnicodeScalar(0x0d)
let escapablePuntuation = "\u{0022}\u{0027}\u{005C}"
var escapableCharacters = CharacterSet(charactersIn: controlCharactersRange)
escapableCharacters.insert(charactersIn: escapablePuntuation)
return unicodeScalars.reduce("") {
current, scalar in
let needsEscaping = escapableCharacters.contains(scalar)
let nextSequence = needsEscaping ? "\\u{\(String(scalar.value, radix: 16))}" : String(scalar)
return current + nextSequence
}
}
}
// MARK: - JSON Key
/// A key for JavaScript objects.
enum JSONKey: CodingKey {
/// A string key.
case string(String)
/// An index key.
case index(Int)
/// A string key for the object's superclass.
case `super`
/// The text value of the key.
var stringValue: String {
switch self {
case .string(let string):
return string
case .index(let index):
return "Index \(index)"
case .super:
return "super"
}
}
/// The integer value of the key?
var intValue: Int? {
switch self {
case .index(let index):
return index
default:
return nil
}
}
/// Creates a JSON key with an integer raw key.
init(intValue: Int) {
self = .index(intValue)
}
/// Creates a JSON key with a String raw key.
init(stringValue: String) {
self = .string(stringValue)
}
}