forked from PerfectlySoft/Perfect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSON.swift
593 lines (516 loc) · 14.3 KB
/
JSON.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//
// JSON.swift
// PerfectLib
//
// Created by Kyle Jessup on 7/14/15.
// Copyright (C) 2015 PerfectlySoft, Inc.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Perfect.org open source project
//
// Copyright (c) 2015 - 2016 PerfectlySoft Inc. and the Perfect project authors
// Licensed under Apache License v2.0
//
// See http://perfect.org/licensing.html for license information
//
//===----------------------------------------------------------------------===//
//
#if os(Linux)
import SwiftGlibc
#else
import Darwin
#endif
private let jsonOpenObject = UnicodeScalar(UInt32(123))
private let jsonOpenArray = UnicodeScalar(UInt32(91))
private let jsonCloseObject = UnicodeScalar(UInt32(125))
private let jsonCloseArray = UnicodeScalar(UInt32(93))
private let jsonQuoteDouble = UnicodeScalar(UInt32(34))
private let jsonWhiteSpace = UnicodeScalar(UInt32(32))
private let jsonColon = UnicodeScalar(UInt32(58))
private let jsonComma = UnicodeScalar(UInt32(44))
private let jsonBackSlash = UnicodeScalar(UInt32(92))
private let jsonBackSpace = UnicodeScalar(UInt32(8))
private let jsonFormFeed = UnicodeScalar(UInt32(12))
private let jsonLF = UnicodeScalar(UInt32(10))
private let jsonCR = UnicodeScalar(UInt32(13))
private let jsonTab = UnicodeScalar(UInt32(9))
public typealias JSONKey = String
public typealias JSONValue = Any
/// An exception enum type which represents JSON encoding and decoding errors
public enum JSONError: ErrorType {
/// A data type was used which is not JSON encodable.
case UnhandledType(String)
/// The JSON data was malformed.
case SyntaxError(String)
}
class KeyPair {
let key: JSONKey
var value: JSONValue?
init(key: JSONKey) {
self.key = key
}
}
/// This class encodes Arrays and Dictionaries into JSON text strings
///
/// Top-level values which may be encoded include
/// - `Array<JSONValue>`
/// - `Dictionary<String, JSONValue>`
/// - `JSONArray`
/// - `JSONDictionary`
///
/// A value in any of the container types may consist of
/// - `Int`
/// - `Double`
/// - `String`
/// - `Bool`
/// - `JSONNull`
/// - `JSONArray`
/// - `JSONDictionary`
/// - `Array<JSONValue>`
/// - `Dictionary<String, JSONValue>`
public class JSONEncoder {
/// Empty public initializer
public init() {
}
/// Encode a `JSONArrayType` into a JSON string
/// - throws: A `JSONError.UnhandledType` exception
public func encode(a: JSONArrayType) throws -> String {
return try encode(a.array)
}
/// Encode an Array of objects into a JSON string
/// - throws: A `JSONError.UnhandledType` exception
public func encode(a: [JSONValue]) throws -> String {
var s = "["
var c = false
for value in a {
if c {
s.appendContentsOf(",")
} else {
c = true
}
s.appendContentsOf(try encodeValue(value))
}
s.appendContentsOf("]")
return s
}
/// Encode a `JSONDictionaryType` into a JSON string
/// - throws: A `JSONError.UnhandledType` exception
public func encode(a: JSONDictionaryType) throws -> String {
return try encode(a.dictionary)
}
/// Encode a Dictionary into a JSON string
/// - throws: A `JSONError.UnhandledType` exception
public func encode(d: [String:JSONValue]) throws -> String {
var s = "{"
var c = false
for (key, value) in d {
if c {
s.appendContentsOf(",")
} else {
c = true
}
s.appendContentsOf(encodeString(key))
s.appendContentsOf(":")
s.appendContentsOf(try encodeValue(value))
}
s.appendContentsOf("}")
return s
}
/// Encode a `JSONValue` into a JSON string
/// - throws: A `JSONError.UnhandledType` exception
func encodeValue(value: JSONValue) throws -> String {
switch(value) {
case let b as Bool:
return b ? "true" : "false"
case let i as Int:
return encodeInt(i)
case let d as Double:
return encodeDouble(d)
case let s as String:
return encodeString(s)
case let ja as JSONArrayType:
return try encodeArray(ja.array)
case let a as Array<JSONValue>:
return try encodeArray(a)
case let jd as JSONDictionaryType:
return try encodeDictionary(jd.dictionary)
case let d as Dictionary<String, JSONValue>:
return try encodeDictionary(d)
case _ as JSONNull:
return "null"
default:
throw JSONError.UnhandledType("The type \(value.dynamicType) was not handled")
}
}
/// Encode a `String` into a JSON string
func encodeString(src: String) -> String {
var s = "\""
for uchar in src.unicodeScalars {
switch(uchar) {
case jsonBackSlash:
s.appendContentsOf("\\\\")
case jsonQuoteDouble:
s.appendContentsOf("\\\"")
case jsonBackSpace:
s.appendContentsOf("\\b")
case jsonFormFeed:
s.appendContentsOf("\\f")
case jsonLF:
s.appendContentsOf("\\n")
case jsonCR:
s.appendContentsOf("\\r")
case jsonTab:
s.appendContentsOf("\\t")
default:
s.append(uchar)
}
}
s.appendContentsOf("\"")
return s
}
/// Encode an `Int` into a JSON string
func encodeInt(i: Int) -> String {
return String(i)
}
/// Encode a `Double` into a JSON string
func encodeDouble(d: Double) -> String {
return String(d)
}
/// Encode an `Array` into a JSON string
/// - throws: A `JSONError.UnhandledType` exception
func encodeArray(a: Array<JSONValue>) throws -> String {
return try encode(a)
}
/// Encode a `Dictionary<String, JSONValue>` intot a JSON string
/// - throws: A `JSONError.UnhandledType` exception
func encodeDictionary(d: Dictionary<String, JSONValue>) throws -> String {
return try encode(d)
}
}
/// This class is used to represent a JSON null
/// An instance of this class can be used in an Array of Dictionary which is to be encoded.
/// An instance of this class may be found in any decoded Array or Dictionary.
public class JSONNull {
/// Empty public initializer
public init() {
}
}
/// This class is a reference based wrapper around `Array<JSONValue>`
/// JSON data which is being decoded will have these object as part of the contents of the resulting data.
public class JSONArrayType {
/// Provides access to the underlying array
public var array = Array<JSONValue>()
public subscript (index: Int) -> Array<JSONValue>.Element {
return array[index]
}
/// Pass-through function which appends to the array.
public func append(a: JSONValue) {
array.append(a)
}
}
/// This class is a referenced based wrapper around `Dictionary<String, JSONValue>`
/// JSON data which is being decoded will have these object as part of the contents of the resulting data.
public class JSONDictionaryType {
public typealias DictionaryType = Dictionary<JSONKey, JSONValue>
public typealias Key = DictionaryType.Key
public typealias Value = DictionaryType.Value
/// Provides access to the underlying Dictionary.
public var dictionary = DictionaryType()
public subscript (key: Key) -> Value? {
get {
return dictionary[key]
}
set(newValue) {
dictionary[key] = newValue
}
}
}
private let malformedJSONString = "Malformed JSON string"
/// This class decodes JSON string data and returns the resulting value(s)
///
/// A resulting value may consist of
/// - `Int`
/// - `Double`
/// - `String`
/// - `Bool`
/// - `JSONNull`
/// - `JSONArray`
/// - `JSONDictionary`
public class JSONDecoder {
var stack = Array<JSONValue>()
var exit: JSONValue?
var g = String().unicodeScalars.generate()
var pushBack: UnicodeScalar?
/// Empty public initializer
public init() {
}
/// Decode a JSON string and return the result
/// - parameter s: The JSON string data
/// - throws: `JSONError.SyntaxError`
/// - returns: The resulting object which may be one of `Int`, `Double`, `String`, `Bool`, `JSONNull`, `JSONArray` or `JSONDictionary`
public func decode(s: String) throws -> JSONValue {
let scalars = s.unicodeScalars
g = scalars.generate()
try readObjects()
guard stack.count == 0 && exit != nil else {
throw JSONError.SyntaxError("Unterminated JSON string")
}
return exit!
}
func readObjects() throws {
var next = self.next()
while let c = next {
switch(c) {
case jsonOpenArray:
stack.append(JSONArrayType())
case jsonOpenObject:
stack.append(JSONDictionaryType())
case jsonCloseArray:
try handlePop()
case jsonCloseObject:
try handlePop()
case jsonColon:
guard stack.count > 0 && stack.last! is KeyPair && (stack.last! as! KeyPair).value == nil else {
throw JSONError.SyntaxError(malformedJSONString)
}
case jsonComma:
guard stack.count > 0 && !(stack.last! is KeyPair) else {
throw JSONError.SyntaxError(malformedJSONString)
}
case jsonQuoteDouble:
try handlePop(try readString())
default:
if c.isWhiteSpace() {
// nothing
} else if c.isDigit() || c == "-" || c == "+" {
try handlePop(try readNumber(c))
} else if c == "t" || c == "T" {
try handlePop(try readTrue())
} else if c == "f" || c == "F" {
try handlePop(try readFalse())
} else if c == "n" || c == "N" {
try handlePop(try readNull())
}
}
next = self.next()
}
}
func pop() throws -> JSONValue {
guard stack.count > 0 else {
throw JSONError.SyntaxError(malformedJSONString)
}
return stack.removeLast()
}
func handleNested(top: JSONValue, obj: JSONValue) throws -> JSONValue {
// top must be array or dictionary or KeyPair with value of nil
// if top is dictionary, obj must be KeyPair
switch top {
case let a as JSONArrayType:
a.append(obj)
return a // ?
case let d as JSONDictionaryType:
switch obj {
case let keyPair as KeyPair:
guard keyPair.value != nil else {
throw JSONError.SyntaxError(malformedJSONString)
}
d[keyPair.key] = keyPair.value!
return d
case let s as String:
let ky = KeyPair(key: s)
stack.append(ky)
return ky
default:
throw JSONError.SyntaxError(malformedJSONString)
}
case let pair as KeyPair:
guard pair.value == nil else {
throw JSONError.SyntaxError(malformedJSONString)
}
pair.value = obj
try pop()
return try handlePop(pair)
default:
throw JSONError.SyntaxError(malformedJSONString)
}
}
func handlePop(a: JSONValue) throws -> JSONValue {
if stack.count > 0 {
return try handleNested(stack.last!, obj: a)
}
exit = a // done
return a
}
func handlePop() throws -> JSONValue {
guard stack.count > 0 else {
throw JSONError.SyntaxError(malformedJSONString)
}
return try handlePop(try pop())
}
// the opening quote has been read
func readString() throws -> String {
var next = self.next()
var esc = false
var s = ""
while let c = next {
if esc {
switch(c) {
case jsonBackSlash:
s.append(jsonBackSlash)
case jsonQuoteDouble:
s.append(jsonQuoteDouble)
case "b":
s.append(jsonBackSpace)
case "f":
s.append(jsonFormFeed)
case "n":
s.append(jsonLF)
case "r":
s.append(jsonCR)
case "t":
s.append(jsonTab)
case "u":
var hexStr = ""
for _ in 1...4 {
next = self.next()
guard let hexC = next else {
throw JSONError.SyntaxError("Malformed hex sequence")
}
guard hexC.isHexDigit() else {
throw JSONError.SyntaxError("Malformed hex sequence")
}
hexStr.append(hexC)
}
let result = UnicodeScalar(UInt32(strtoul(hexStr, nil, 16)))
s.append(result)
default:
s.append(c)
}
esc = false
} else if c == jsonBackSlash {
esc = true
} else if c == jsonQuoteDouble {
return s
} else {
s.append(c)
}
next = self.next()
}
throw JSONError.SyntaxError("Unterminated string literal")
}
func readNumber(firstChar: UnicodeScalar) throws -> JSONValue {
var s = ""
var needPeriod = true, needExp = true
s.append(firstChar)
if firstChar == "." {
needPeriod = false
}
var next = self.next()
var last = firstChar
while let c = next {
if c.isDigit() {
s.append(c)
} else if c == "." && !needPeriod {
break
} else if (c == "e" || c == "E") && !needExp {
break
} else if c == "." {
needPeriod = false
s.append(c)
} else if c == "e" || c == "E" {
needExp = false
s.append(c)
next = self.next()
if next != nil && (next! == "-" || next! == "+") {
s.append(next!)
} else {
pushBack = next!
}
} else if last.isDigit() {
pushBack = c
if needPeriod && needExp {
return Int(s)!
}
return Double(s)!
} else {
break
}
last = c
next = self.next()
}
throw JSONError.SyntaxError("Malformed numeric literal")
}
func readTrue() throws -> Bool {
var next = self.next()
if next != "r" && next != "R" {
throw JSONError.SyntaxError("Malformed boolean literal")
}
next = self.next()
if next != "u" && next != "U" {
throw JSONError.SyntaxError("Malformed boolean literal")
}
next = self.next()
if next != "e" && next != "E" {
throw JSONError.SyntaxError("Malformed boolean literal")
}
next = self.next()
guard next != nil && !next!.isAlphaNum() else {
throw JSONError.SyntaxError("Malformed boolean literal")
}
pushBack = next!
return true
}
func readFalse() throws -> Bool {
var next = self.next()
if next != "a" && next != "A" {
throw JSONError.SyntaxError("Malformed boolean literal")
}
next = self.next()
if next != "l" && next != "L" {
throw JSONError.SyntaxError("Malformed boolean literal")
}
next = self.next()
if next != "s" && next != "S" {
throw JSONError.SyntaxError("Malformed boolean literal")
}
next = self.next()
if next != "e" && next != "E" {
throw JSONError.SyntaxError("Malformed boolean literal")
}
next = self.next()
guard next != nil && !next!.isAlphaNum() else {
throw JSONError.SyntaxError("Malformed boolean literal")
}
pushBack = next!
return false
}
func readNull() throws -> JSONNull {
var next = self.next()
if next != "u" && next != "U" {
throw JSONError.SyntaxError("Malformed null literal")
}
next = self.next()
if next != "l" && next != "L" {
throw JSONError.SyntaxError("Malformed null literal")
}
next = self.next()
if next != "l" && next != "L" {
throw JSONError.SyntaxError("Malformed null literal")
}
next = self.next()
guard next != nil && !next!.isAlphaNum() else {
throw JSONError.SyntaxError("Malformed null literal")
}
pushBack = next!
return JSONNull()
}
func next() -> UnicodeScalar? {
if pushBack != nil {
let c = pushBack!
pushBack = nil
return c
}
return g.next()
}
}