forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONEncoder.swift
1249 lines (1049 loc) · 48.7 KB
/
JSONEncoder.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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@_implementationOnly import CoreFoundation
/// A marker protocol used to determine whether a value is a `String`-keyed `Dictionary`
/// containing `Encodable` values (in which case it should be exempt from key conversion strategies).
///
fileprivate protocol _JSONStringDictionaryEncodableMarker { }
extension Dictionary: _JSONStringDictionaryEncodableMarker where Key == String, Value: Encodable { }
//===----------------------------------------------------------------------===//
// JSON Encoder
//===----------------------------------------------------------------------===//
/// `JSONEncoder` facilitates the encoding of `Encodable` values into JSON.
open class JSONEncoder {
// MARK: Options
/// The formatting of the output JSON data.
public struct OutputFormatting: OptionSet {
/// The format's default value.
public let rawValue: UInt
/// Creates an OutputFormatting value with the given raw value.
public init(rawValue: UInt) {
self.rawValue = rawValue
}
/// Produce human-readable JSON with indented output.
public static let prettyPrinted = OutputFormatting(rawValue: 1 << 0)
/// Produce JSON with dictionary keys sorted in lexicographic order.
@available(macOS 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)
public static let sortedKeys = OutputFormatting(rawValue: 1 << 1)
/// By default slashes get escaped ("/" → "\/", "http://apple.com/" → "http:\/\/apple.com\/")
/// for security reasons, allowing outputted JSON to be safely embedded within HTML/XML.
/// In contexts where this escaping is unnecessary, the JSON is known to not be embedded,
/// or is intended only for display, this option avoids this escaping.
public static let withoutEscapingSlashes = OutputFormatting(rawValue: 1 << 3)
}
/// The strategy to use for encoding `Date` values.
public enum DateEncodingStrategy {
/// Defer to `Date` for choosing an encoding. This is the default strategy.
case deferredToDate
/// Encode the `Date` as a UNIX timestamp (as a JSON number).
case secondsSince1970
/// Encode the `Date` as UNIX millisecond timestamp (as a JSON number).
case millisecondsSince1970
/// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
case iso8601
/// Encode the `Date` as a string formatted by the given formatter.
case formatted(DateFormatter)
/// Encode the `Date` as a custom value encoded by the given closure.
///
/// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
case custom((Date, Encoder) throws -> Void)
}
/// The strategy to use for encoding `Data` values.
public enum DataEncodingStrategy {
/// Defer to `Data` for choosing an encoding.
case deferredToData
/// Encoded the `Data` as a Base64-encoded string. This is the default strategy.
case base64
/// Encode the `Data` as a custom value encoded by the given closure.
///
/// If the closure fails to encode a value into the given encoder, the encoder will encode an empty automatic container in its place.
case custom((Data, Encoder) throws -> Void)
}
/// The strategy to use for non-JSON-conforming floating-point values (IEEE 754 infinity and NaN).
public enum NonConformingFloatEncodingStrategy {
/// Throw upon encountering non-conforming values. This is the default strategy.
case `throw`
/// Encode the values using the given representation strings.
case convertToString(positiveInfinity: String, negativeInfinity: String, nan: String)
}
/// The strategy to use for automatically changing the value of keys before encoding.
public enum KeyEncodingStrategy {
/// Use the keys specified by each type. This is the default strategy.
case useDefaultKeys
/// Convert from "camelCaseKeys" to "snake_case_keys" before writing a key to JSON payload.
///
/// Capital characters are determined by testing membership in `CharacterSet.uppercaseLetters` and `CharacterSet.lowercaseLetters` (Unicode General Categories Lu and Lt).
/// The conversion to lower case uses `Locale.system`, also known as the ICU "root" locale. This means the result is consistent regardless of the current user's locale and language preferences.
///
/// Converting from camel case to snake case:
/// 1. Splits words at the boundary of lower-case to upper-case
/// 2. Inserts `_` between words
/// 3. Lowercases the entire string
/// 4. Preserves starting and ending `_`.
///
/// For example, `oneTwoThree` becomes `one_two_three`. `_oneTwoThree_` becomes `_one_two_three_`.
///
/// - Note: Using a key encoding strategy has a nominal performance cost, as each string key has to be converted.
case convertToSnakeCase
/// Provide a custom conversion to the key in the encoded JSON from the keys specified by the encoded types.
/// The full path to the current encoding position is provided for context (in case you need to locate this key within the payload). The returned key is used in place of the last component in the coding path before encoding.
/// If the result of the conversion is a duplicate key, then only one value will be present in the result.
case custom((_ codingPath: [CodingKey]) -> CodingKey)
fileprivate static func _convertToSnakeCase(_ stringKey: String) -> String {
guard !stringKey.isEmpty else { return stringKey }
var words: [Range<String.Index>] = []
// The general idea of this algorithm is to split words on transition from lower to upper case, then on transition of >1 upper case characters to lowercase
//
// myProperty -> my_property
// myURLProperty -> my_url_property
//
// We assume, per Swift naming conventions, that the first character of the key is lowercase.
var wordStart = stringKey.startIndex
var searchRange = stringKey.index(after: wordStart)..<stringKey.endIndex
// Find next uppercase character
while let upperCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.uppercaseLetters, options: [], range: searchRange) {
let untilUpperCase = wordStart..<upperCaseRange.lowerBound
words.append(untilUpperCase)
// Find next lowercase character
searchRange = upperCaseRange.lowerBound..<searchRange.upperBound
guard let lowerCaseRange = stringKey.rangeOfCharacter(from: CharacterSet.lowercaseLetters, options: [], range: searchRange) else {
// There are no more lower case letters. Just end here.
wordStart = searchRange.lowerBound
break
}
// Is the next lowercase letter more than 1 after the uppercase? If so, we encountered a group of uppercase letters that we should treat as its own word
let nextCharacterAfterCapital = stringKey.index(after: upperCaseRange.lowerBound)
if lowerCaseRange.lowerBound == nextCharacterAfterCapital {
// The next character after capital is a lower case character and therefore not a word boundary.
// Continue searching for the next upper case for the boundary.
wordStart = upperCaseRange.lowerBound
} else {
// There was a range of >1 capital letters. Turn those into a word, stopping at the capital before the lower case character.
let beforeLowerIndex = stringKey.index(before: lowerCaseRange.lowerBound)
words.append(upperCaseRange.lowerBound..<beforeLowerIndex)
// Next word starts at the capital before the lowercase we just found
wordStart = beforeLowerIndex
}
searchRange = lowerCaseRange.upperBound..<searchRange.upperBound
}
words.append(wordStart..<searchRange.upperBound)
let result = words.map({ (range) in
return stringKey[range].lowercased()
}).joined(separator: "_")
return result
}
}
/// The output format to produce. Defaults to `[]`.
open var outputFormatting: OutputFormatting = []
/// The strategy to use in encoding dates. Defaults to `.deferredToDate`.
open var dateEncodingStrategy: DateEncodingStrategy = .deferredToDate
/// The strategy to use in encoding binary data. Defaults to `.base64`.
open var dataEncodingStrategy: DataEncodingStrategy = .base64
/// The strategy to use in encoding non-conforming numbers. Defaults to `.throw`.
open var nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy = .throw
/// The strategy to use for encoding keys. Defaults to `.useDefaultKeys`.
open var keyEncodingStrategy: KeyEncodingStrategy = .useDefaultKeys
/// Contextual user-provided information for use during encoding.
open var userInfo: [CodingUserInfoKey: Any] = [:]
/// Options set on the top-level encoder to pass down the encoding hierarchy.
fileprivate struct _Options {
let dateEncodingStrategy: DateEncodingStrategy
let dataEncodingStrategy: DataEncodingStrategy
let nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy
let keyEncodingStrategy: KeyEncodingStrategy
let userInfo: [CodingUserInfoKey: Any]
}
/// The options set on the top-level encoder.
fileprivate var options: _Options {
return _Options(dateEncodingStrategy: dateEncodingStrategy,
dataEncodingStrategy: dataEncodingStrategy,
nonConformingFloatEncodingStrategy: nonConformingFloatEncodingStrategy,
keyEncodingStrategy: keyEncodingStrategy,
userInfo: userInfo)
}
// MARK: - Constructing a JSON Encoder
/// Initializes `self` with default strategies.
public init() {}
// MARK: - Encoding Values
/// Encodes the given top-level value and returns its JSON representation.
///
/// - parameter value: The value to encode.
/// - returns: A new `Data` value containing the encoded JSON data.
/// - throws: `EncodingError.invalidValue` if a non-conforming floating-point value is encountered during encoding, and the encoding strategy is `.throw`.
/// - throws: An error if any value throws an error during encoding.
open func encode<T: Encodable>(_ value: T) throws -> Data {
let value: JSONValue = try encodeAsJSONValue(value)
let writer = JSONValue.Writer(options: self.outputFormatting)
let bytes = writer.writeValue(value)
return Data(bytes)
}
func encodeAsJSONValue<T: Encodable>(_ value: T) throws -> JSONValue {
let encoder = JSONEncoderImpl(options: self.options, codingPath: [])
guard let topLevel = try encoder.wrapEncodable(value, for: nil) else {
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Top-level \(T.self) did not encode any values."))
}
return topLevel
}
}
// MARK: - _JSONEncoder
private enum JSONFuture {
case value(JSONValue)
case encoder(JSONEncoderImpl)
case nestedArray(RefArray)
case nestedObject(RefObject)
class RefArray {
private(set) var array: [JSONFuture] = []
init() {
self.array.reserveCapacity(10)
}
@inline(__always) func append(_ element: JSONValue) {
self.array.append(.value(element))
}
@inline(__always) func append(_ encoder: JSONEncoderImpl) {
self.array.append(.encoder(encoder))
}
@inline(__always) func appendArray() -> RefArray {
let array = RefArray()
self.array.append(.nestedArray(array))
return array
}
@inline(__always) func appendObject() -> RefObject {
let object = RefObject()
self.array.append(.nestedObject(object))
return object
}
var values: [JSONValue] {
self.array.map { (future) -> JSONValue in
switch future {
case .value(let value):
return value
case .nestedArray(let array):
return .array(array.values)
case .nestedObject(let object):
return .object(object.values)
case .encoder(let encoder):
return encoder.value ?? .object([:])
}
}
}
}
class RefObject {
private(set) var dict: [String: JSONFuture] = [:]
init() {
self.dict.reserveCapacity(20)
}
@inline(__always) func set(_ value: JSONValue, for key: String) {
self.dict[key] = .value(value)
}
@inline(__always) func setArray(for key: String) -> RefArray {
switch self.dict[key] {
case .encoder:
preconditionFailure("For key \"\(key)\" an encoder has already been created.")
case .nestedObject:
preconditionFailure("For key \"\(key)\" a keyed container has already been created.")
case .nestedArray(let array):
return array
case .none, .value:
let array = RefArray()
dict[key] = .nestedArray(array)
return array
}
}
@inline(__always) func setObject(for key: String) -> RefObject {
switch self.dict[key] {
case .encoder:
preconditionFailure("For key \"\(key)\" an encoder has already been created.")
case .nestedObject(let object):
return object
case .nestedArray:
preconditionFailure("For key \"\(key)\" a unkeyed container has already been created.")
case .none, .value:
let object = RefObject()
dict[key] = .nestedObject(object)
return object
}
}
@inline(__always) func set(_ encoder: JSONEncoderImpl, for key: String) {
switch self.dict[key] {
case .encoder:
preconditionFailure("For key \"\(key)\" an encoder has already been created.")
case .nestedObject:
preconditionFailure("For key \"\(key)\" a keyed container has already been created.")
case .nestedArray:
preconditionFailure("For key \"\(key)\" a unkeyed container has already been created.")
case .none, .value:
dict[key] = .encoder(encoder)
}
}
var values: [String: JSONValue] {
self.dict.mapValues { (future) -> JSONValue in
switch future {
case .value(let value):
return value
case .nestedArray(let array):
return .array(array.values)
case .nestedObject(let object):
return .object(object.values)
case .encoder(let encoder):
return encoder.value ?? .object([:])
}
}
}
}
}
private class JSONEncoderImpl {
let options: JSONEncoder._Options
let codingPath: [CodingKey]
var userInfo: [CodingUserInfoKey: Any] {
options.userInfo
}
var singleValue: JSONValue?
var array: JSONFuture.RefArray?
var object: JSONFuture.RefObject?
var value: JSONValue? {
if let object = self.object {
return .object(object.values)
}
if let array = self.array {
return .array(array.values)
}
return self.singleValue
}
init(options: JSONEncoder._Options, codingPath: [CodingKey]) {
self.options = options
self.codingPath = codingPath
}
}
extension JSONEncoderImpl: Encoder {
func container<Key>(keyedBy _: Key.Type) -> KeyedEncodingContainer<Key> where Key: CodingKey {
if let _ = object {
let container = JSONKeyedEncodingContainer<Key>(impl: self, codingPath: codingPath)
return KeyedEncodingContainer(container)
}
guard self.singleValue == nil, self.array == nil else {
preconditionFailure()
}
self.object = JSONFuture.RefObject()
let container = JSONKeyedEncodingContainer<Key>(impl: self, codingPath: codingPath)
return KeyedEncodingContainer(container)
}
func unkeyedContainer() -> UnkeyedEncodingContainer {
if let _ = array {
return JSONUnkeyedEncodingContainer(impl: self, codingPath: self.codingPath)
}
guard self.singleValue == nil, self.object == nil else {
preconditionFailure()
}
self.array = JSONFuture.RefArray()
return JSONUnkeyedEncodingContainer(impl: self, codingPath: self.codingPath)
}
func singleValueContainer() -> SingleValueEncodingContainer {
guard self.object == nil, self.array == nil else {
preconditionFailure()
}
return JSONSingleValueEncodingContainer(impl: self, codingPath: self.codingPath)
}
}
// this is a private protocol to implement convenience methods directly on the EncodingContainers
extension JSONEncoderImpl: _SpecialTreatmentEncoder {
var impl: JSONEncoderImpl {
return self
}
// untyped escape hatch. needed for `wrapObject`
func wrapUntyped(_ encodable: Encodable) throws -> JSONValue {
switch encodable {
case let date as Date:
return try self.wrapDate(date, for: nil)
case let data as Data:
return try self.wrapData(data, for: nil)
case let url as URL:
return .string(url.absoluteString)
case let decimal as Decimal:
return .number(decimal.description)
case let object as [String: Encodable]: // this emits a warning, but it works perfectly
return try self.wrapObject(object, for: nil)
default:
try encodable.encode(to: self)
return self.value ?? .object([:])
}
}
}
private protocol _SpecialTreatmentEncoder {
var codingPath: [CodingKey] { get }
var options: JSONEncoder._Options { get }
var impl: JSONEncoderImpl { get }
}
extension _SpecialTreatmentEncoder {
@inline(__always) fileprivate func wrapFloat<F: FloatingPoint & CustomStringConvertible>(_ float: F, for additionalKey: CodingKey?) throws -> JSONValue {
guard !float.isNaN, !float.isInfinite else {
if case .convertToString(let posInfString, let negInfString, let nanString) = self.options.nonConformingFloatEncodingStrategy {
switch float {
case F.infinity:
return .string(posInfString)
case -F.infinity:
return .string(negInfString)
default:
// must be nan in this case
return .string(nanString)
}
}
var path = self.codingPath
if let additionalKey = additionalKey {
path.append(additionalKey)
}
throw EncodingError.invalidValue(float, .init(
codingPath: path,
debugDescription: "Unable to encode \(F.self).\(float) directly in JSON."
))
}
var string = float.description
if string.hasSuffix(".0") {
string.removeLast(2)
}
return .number(string)
}
fileprivate func wrapEncodable<E: Encodable>(_ encodable: E, for additionalKey: CodingKey?) throws -> JSONValue? {
switch encodable {
case let date as Date:
return try self.wrapDate(date, for: additionalKey)
case let data as Data:
return try self.wrapData(data, for: additionalKey)
case let url as URL:
return .string(url.absoluteString)
case let decimal as Decimal:
return .number(decimal.description)
case let object as _JSONStringDictionaryEncodableMarker:
return try self.wrapObject(object as! [String: Encodable], for: additionalKey)
default:
let encoder = self.getEncoder(for: additionalKey)
try encodable.encode(to: encoder)
return encoder.value
}
}
func wrapDate(_ date: Date, for additionalKey: CodingKey?) throws -> JSONValue {
switch self.options.dateEncodingStrategy {
case .deferredToDate:
let encoder = self.getEncoder(for: additionalKey)
try date.encode(to: encoder)
return encoder.value ?? .null
case .secondsSince1970:
return .number(date.timeIntervalSince1970.description)
case .millisecondsSince1970:
return .number((date.timeIntervalSince1970 * 1000).description)
case .iso8601:
if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) {
return .string(_iso8601Formatter.string(from: date))
} else {
fatalError("ISO8601DateFormatter is unavailable on this platform.")
}
case .formatted(let formatter):
return .string(formatter.string(from: date))
case .custom(let closure):
let encoder = self.getEncoder(for: additionalKey)
try closure(date, encoder)
// The closure didn't encode anything. Return the default keyed container.
return encoder.value ?? .object([:])
}
}
func wrapData(_ data: Data, for additionalKey: CodingKey?) throws -> JSONValue {
switch self.options.dataEncodingStrategy {
case .deferredToData:
let encoder = self.getEncoder(for: additionalKey)
try data.encode(to: encoder)
return encoder.value ?? .null
case .base64:
let base64 = data.base64EncodedString()
return .string(base64)
case .custom(let closure):
let encoder = self.getEncoder(for: additionalKey)
try closure(data, encoder)
// The closure didn't encode anything. Return the default keyed container.
return encoder.value ?? .object([:])
}
}
func wrapObject(_ object: [String: Encodable], for additionalKey: CodingKey?) throws -> JSONValue {
var baseCodingPath = self.codingPath
if let additionalKey = additionalKey {
baseCodingPath.append(additionalKey)
}
var result = [String: JSONValue]()
result.reserveCapacity(object.count)
try object.forEach { (key, value) in
var elemCodingPath = baseCodingPath
elemCodingPath.append(_JSONKey(stringValue: key, intValue: nil))
let encoder = JSONEncoderImpl(options: self.options, codingPath: elemCodingPath)
result[key] = try encoder.wrapUntyped(value)
}
return .object(result)
}
fileprivate func getEncoder(for additionalKey: CodingKey?) -> JSONEncoderImpl {
if let additionalKey = additionalKey {
var newCodingPath = self.codingPath
newCodingPath.append(additionalKey)
return JSONEncoderImpl(options: self.options, codingPath: newCodingPath)
}
return self.impl
}
}
private struct JSONKeyedEncodingContainer<K: CodingKey>: KeyedEncodingContainerProtocol, _SpecialTreatmentEncoder {
typealias Key = K
let impl: JSONEncoderImpl
let object: JSONFuture.RefObject
let codingPath: [CodingKey]
private var firstValueWritten: Bool = false
fileprivate var options: JSONEncoder._Options {
return self.impl.options
}
init(impl: JSONEncoderImpl, codingPath: [CodingKey]) {
self.impl = impl
self.object = impl.object!
self.codingPath = codingPath
}
// used for nested containers
init(impl: JSONEncoderImpl, object: JSONFuture.RefObject, codingPath: [CodingKey]) {
self.impl = impl
self.object = object
self.codingPath = codingPath
}
private func _converted(_ key: Key) -> CodingKey {
switch self.options.keyEncodingStrategy {
case .useDefaultKeys:
return key
case .convertToSnakeCase:
let newKeyString = JSONEncoder.KeyEncodingStrategy._convertToSnakeCase(key.stringValue)
return _JSONKey(stringValue: newKeyString, intValue: key.intValue)
case .custom(let converter):
return converter(codingPath + [key])
}
}
mutating func encodeNil(forKey key: Self.Key) throws {
self.object.set(.null, for: self._converted(key).stringValue)
}
mutating func encode(_ value: Bool, forKey key: Self.Key) throws {
self.object.set(.bool(value), for: self._converted(key).stringValue)
}
mutating func encode(_ value: String, forKey key: Self.Key) throws {
self.object.set(.string(value), for: self._converted(key).stringValue)
}
mutating func encode(_ value: Double, forKey key: Self.Key) throws {
try encodeFloatingPoint(value, key: self._converted(key))
}
mutating func encode(_ value: Float, forKey key: Self.Key) throws {
try encodeFloatingPoint(value, key: self._converted(key))
}
mutating func encode(_ value: Int, forKey key: Self.Key) throws {
try encodeFixedWidthInteger(value, key: self._converted(key))
}
mutating func encode(_ value: Int8, forKey key: Self.Key) throws {
try encodeFixedWidthInteger(value, key: self._converted(key))
}
mutating func encode(_ value: Int16, forKey key: Self.Key) throws {
try encodeFixedWidthInteger(value, key: self._converted(key))
}
mutating func encode(_ value: Int32, forKey key: Self.Key) throws {
try encodeFixedWidthInteger(value, key: self._converted(key))
}
mutating func encode(_ value: Int64, forKey key: Self.Key) throws {
try encodeFixedWidthInteger(value, key: self._converted(key))
}
mutating func encode(_ value: UInt, forKey key: Self.Key) throws {
try encodeFixedWidthInteger(value, key: self._converted(key))
}
mutating func encode(_ value: UInt8, forKey key: Self.Key) throws {
try encodeFixedWidthInteger(value, key: self._converted(key))
}
mutating func encode(_ value: UInt16, forKey key: Self.Key) throws {
try encodeFixedWidthInteger(value, key: self._converted(key))
}
mutating func encode(_ value: UInt32, forKey key: Self.Key) throws {
try encodeFixedWidthInteger(value, key: self._converted(key))
}
mutating func encode(_ value: UInt64, forKey key: Self.Key) throws {
try encodeFixedWidthInteger(value, key: self._converted(key))
}
mutating func encode<T>(_ value: T, forKey key: Self.Key) throws where T: Encodable {
let convertedKey = self._converted(key)
let encoded = try self.wrapEncodable(value, for: convertedKey)
self.object.set(encoded ?? .object([:]), for: convertedKey.stringValue)
}
mutating func nestedContainer<NestedKey>(keyedBy _: NestedKey.Type, forKey key: Self.Key) ->
KeyedEncodingContainer<NestedKey> where NestedKey: CodingKey
{
let convertedKey = self._converted(key)
let newPath = self.codingPath + [convertedKey]
let object = self.object.setObject(for: convertedKey.stringValue)
let nestedContainer = JSONKeyedEncodingContainer<NestedKey>(impl: impl, object: object, codingPath: newPath)
return KeyedEncodingContainer(nestedContainer)
}
mutating func nestedUnkeyedContainer(forKey key: Self.Key) -> UnkeyedEncodingContainer {
let convertedKey = self._converted(key)
let newPath = self.codingPath + [convertedKey]
let array = self.object.setArray(for: convertedKey.stringValue)
let nestedContainer = JSONUnkeyedEncodingContainer(impl: impl, array: array, codingPath: newPath)
return nestedContainer
}
mutating func superEncoder() -> Encoder {
let newEncoder = self.getEncoder(for: _JSONKey.super)
self.object.set(newEncoder, for: _JSONKey.super.stringValue)
return newEncoder
}
mutating func superEncoder(forKey key: Self.Key) -> Encoder {
let convertedKey = self._converted(key)
let newEncoder = self.getEncoder(for: convertedKey)
self.object.set(newEncoder, for: convertedKey.stringValue)
return newEncoder
}
}
extension JSONKeyedEncodingContainer {
@inline(__always) private mutating func encodeFloatingPoint<F: FloatingPoint & CustomStringConvertible>(_ float: F, key: CodingKey) throws {
let value = try self.wrapFloat(float, for: key)
self.object.set(value, for: key.stringValue)
}
@inline(__always) private mutating func encodeFixedWidthInteger<N: FixedWidthInteger>(_ value: N, key: CodingKey) throws {
self.object.set(.number(value.description), for: key.stringValue)
}
}
private struct JSONUnkeyedEncodingContainer: UnkeyedEncodingContainer, _SpecialTreatmentEncoder {
let impl: JSONEncoderImpl
let array: JSONFuture.RefArray
let codingPath: [CodingKey]
var count: Int {
self.array.array.count
}
private var firstValueWritten: Bool = false
fileprivate var options: JSONEncoder._Options {
return self.impl.options
}
init(impl: JSONEncoderImpl, codingPath: [CodingKey]) {
self.impl = impl
self.array = impl.array!
self.codingPath = codingPath
}
// used for nested containers
init(impl: JSONEncoderImpl, array: JSONFuture.RefArray, codingPath: [CodingKey]) {
self.impl = impl
self.array = array
self.codingPath = codingPath
}
mutating func encodeNil() throws {
self.array.append(.null)
}
mutating func encode(_ value: Bool) throws {
self.array.append(.bool(value))
}
mutating func encode(_ value: String) throws {
self.array.append(.string(value))
}
mutating func encode(_ value: Double) throws {
try encodeFloatingPoint(value)
}
mutating func encode(_ value: Float) throws {
try encodeFloatingPoint(value)
}
mutating func encode(_ value: Int) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: Int8) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: Int16) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: Int32) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: Int64) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: UInt) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: UInt8) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: UInt16) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: UInt32) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: UInt64) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode<T>(_ value: T) throws where T: Encodable {
let key = _JSONKey(stringValue: "Index \(self.count)", intValue: self.count)
let encoded = try self.wrapEncodable(value, for: key)
self.array.append(encoded ?? .object([:]))
}
mutating func nestedContainer<NestedKey>(keyedBy _: NestedKey.Type) ->
KeyedEncodingContainer<NestedKey> where NestedKey: CodingKey
{
let newPath = self.codingPath + [_JSONKey(index: self.count)]
let object = self.array.appendObject()
let nestedContainer = JSONKeyedEncodingContainer<NestedKey>(impl: impl, object: object, codingPath: newPath)
return KeyedEncodingContainer(nestedContainer)
}
mutating func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
let newPath = self.codingPath + [_JSONKey(index: self.count)]
let array = self.array.appendArray()
let nestedContainer = JSONUnkeyedEncodingContainer(impl: impl, array: array, codingPath: newPath)
return nestedContainer
}
mutating func superEncoder() -> Encoder {
let encoder = self.getEncoder(for: _JSONKey(index: self.count))
self.array.append(encoder)
return encoder
}
}
extension JSONUnkeyedEncodingContainer {
@inline(__always) private mutating func encodeFixedWidthInteger<N: FixedWidthInteger>(_ value: N) throws {
self.array.append(.number(value.description))
}
@inline(__always) private mutating func encodeFloatingPoint<F: FloatingPoint & CustomStringConvertible>(_ float: F) throws {
let value = try self.wrapFloat(float, for: _JSONKey(index: self.count))
self.array.append(value)
}
}
private struct JSONSingleValueEncodingContainer: SingleValueEncodingContainer, _SpecialTreatmentEncoder {
let impl: JSONEncoderImpl
let codingPath: [CodingKey]
private var firstValueWritten: Bool = false
fileprivate var options: JSONEncoder._Options {
return self.impl.options
}
init(impl: JSONEncoderImpl, codingPath: [CodingKey]) {
self.impl = impl
self.codingPath = codingPath
}
mutating func encodeNil() throws {
self.preconditionCanEncodeNewValue()
self.impl.singleValue = .null
}
mutating func encode(_ value: Bool) throws {
self.preconditionCanEncodeNewValue()
self.impl.singleValue = .bool(value)
}
mutating func encode(_ value: Int) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: Int8) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: Int16) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: Int32) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: Int64) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: UInt) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: UInt8) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: UInt16) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: UInt32) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: UInt64) throws {
try encodeFixedWidthInteger(value)
}
mutating func encode(_ value: Float) throws {
try encodeFloatingPoint(value)
}
mutating func encode(_ value: Double) throws {
try encodeFloatingPoint(value)
}
mutating func encode(_ value: String) throws {
self.preconditionCanEncodeNewValue()
self.impl.singleValue = .string(value)
}
mutating func encode<T: Encodable>(_ value: T) throws {
self.preconditionCanEncodeNewValue()
self.impl.singleValue = try self.wrapEncodable(value, for: nil)
}
func preconditionCanEncodeNewValue() {
precondition(self.impl.singleValue == nil, "Attempt to encode value through single value container when previously value already encoded.")
}
}
extension JSONSingleValueEncodingContainer {
@inline(__always) private mutating func encodeFixedWidthInteger<N: FixedWidthInteger>(_ value: N) throws {
self.preconditionCanEncodeNewValue()
self.impl.singleValue = .number(value.description)
}
@inline(__always) private mutating func encodeFloatingPoint<F: FloatingPoint & CustomStringConvertible>(_ float: F) throws {
self.preconditionCanEncodeNewValue()
let value = try self.wrapFloat(float, for: nil)
self.impl.singleValue = value
}
}
extension JSONValue {
fileprivate struct Writer {
let options: JSONEncoder.OutputFormatting
init(options: JSONEncoder.OutputFormatting) {
self.options = options
}
func writeValue(_ value: JSONValue) -> [UInt8] {
var bytes = [UInt8]()
if self.options.contains(.prettyPrinted) {
self.writeValuePretty(value, into: &bytes)
}
else {
self.writeValue(value, into: &bytes)
}
return bytes
}
private func writeValue(_ value: JSONValue, into bytes: inout [UInt8]) {
switch value {
case .null:
bytes.append(contentsOf: [UInt8]._null)
case .bool(true):
bytes.append(contentsOf: [UInt8]._true)
case .bool(false):
bytes.append(contentsOf: [UInt8]._false)
case .string(let string):
self.encodeString(string, to: &bytes)
case .number(let string):
bytes.append(contentsOf: string.utf8)
case .array(let array):