forked from swiftlang/swift-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewJSONDecoder.swift
More file actions
351 lines (314 loc) · 14.6 KB
/
NewJSONDecoder.swift
File metadata and controls
351 lines (314 loc) · 14.6 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
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 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
//
//===----------------------------------------------------------------------===//
#if canImport(FoundationEssentials)
import FoundationEssentials
#elseif FOUNDATION_FRAMEWORK
import Foundation
#endif
#if canImport(Darwin)
import Darwin
#elseif canImport(Bionic)
import Bionic
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(ucrt)
import ucrt
#elseif canImport(WASILibc)
import WASILibc
#endif
public struct NewJSONDecoder {
@usableFromInline
internal var options: Options
public struct Options {
/// The strategy to use for decoding `Date` values.
public struct DateDecodingStrategy : Sendable {
internal enum _Storage: @unchecked Sendable {
case deferredToDate
case secondsSince1970
case millisecondsSince1970
case iso8601
case formatted(any ParseStrategy)
}
internal let storage: _Storage
/// Defer to `Date` for decoding. This is the default strategy.
public static var deferredToDate: Self {
.init(storage: .deferredToDate)
}
/// Decode the `Date` as a UNIX timestamp from a JSON number.
public static var secondsSince1970: Self {
.init(storage: .secondsSince1970)
}
/// Decode the `Date` as UNIX millisecond timestamp from a JSON number.
public static var millisecondsSince1970: Self {
.init(storage: .millisecondsSince1970)
}
/// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format).
public static var iso8601: Self {
.init(storage: .iso8601)
}
/// Decode the `Date` as a string parsed by the given strategy.
public static func formatted<S: ParseStrategy & Sendable>(_ style: S) -> Self where S.ParseInput == String, S.ParseOutput == Date {
.init(storage: .formatted(style))
}
}
/// The strategy to use for decoding `Data` values.
public enum DataDecodingStrategy : Sendable {
/// Defer to `Data` for decoding.
case deferredToData
/// Decode the `Data` from a Base64-encoded string. This is the default strategy.
case base64
}
/// The strategy to use for non-JSON-conforming floating-point values (IEEE 754 infinity and NaN).
public enum NonConformingFloatDecodingStrategy : Sendable {
/// Throw upon encountering non-conforming values. This is the default strategy.
case `throw`
/// Decode the values using the given representation strings.
case convertFromString(positiveInfinity: String, negativeInfinity: String, nan: String)
}
var assumesTopLevelDictionary = false // TODO: Unimplemented
internal var dataDecodingStrategy: DataDecodingStrategy
internal var dateDecodingStrategy: DateDecodingStrategy
internal var nonConformingFloatDecodingStrategy: NonConformingFloatDecodingStrategy
public init(assumesTopLevelDictionary: Bool = false,
dataDecodingStrategy: DataDecodingStrategy = .base64,
dateDecodingStrategy: DateDecodingStrategy = .deferredToDate,
nonConformingFloatDecodingStrategy: NonConformingFloatDecodingStrategy = .throw) {
self.assumesTopLevelDictionary = assumesTopLevelDictionary
self.dataDecodingStrategy = dataDecodingStrategy
self.dateDecodingStrategy = dateDecodingStrategy
self.nonConformingFloatDecodingStrategy = nonConformingFloatDecodingStrategy
}
}
public init(options: Options = .init()) {
self.options = options
}
// TODO: RawSpan-taking functions need to detect Unicode encoding (including BOM) and convert to UTF-8, if necessary.
@usableFromInline
internal func _decode<T: JSONDecodable & ~Copyable>(_ type: T.Type, from bytes: RawSpan, closure: (inout JSONParserDecoder) throws(CodingError.Decoding) -> T) throws(CodingError.Decoding) -> T {
var node = JSONParserDecoder.CodingPathNode.root
return try withUnsafeMutablePointer(to: &node) { ptr throws(CodingError.Decoding) in
let localOptions = self.options
let parserState = JSONParserDecoder.ParserState(unvalidatedUTF8Span: bytes, options: localOptions&, topCodingPathNode: ptr)
var inner = JSONParserDecoder(state: parserState)
let result = try closure(&inner)
try inner._finishDecode()
return result
}
}
@inlinable
public func decode<T: JSONDecodable & ~Copyable>(_ type: T.Type, from bytes: RawSpan) throws(CodingError.Decoding) -> T {
try _decode(type, from: bytes) { inner throws(CodingError.Decoding) in
try inner.decode(type)
}
}
@usableFromInline
internal func _decode<T: CommonDecodable>(_ type: T.Type, from bytes: RawSpan, closure: (inout JSONParserDecoder) throws(CodingError.Decoding) -> T) throws(CodingError.Decoding) -> T {
var node = JSONParserDecoder.CodingPathNode.root
return try withUnsafeMutablePointer(to: &node) { ptr throws(CodingError.Decoding) in
let localOptions = self.options
let parserState = JSONParserDecoder.ParserState(unvalidatedUTF8Span: bytes, options: localOptions&, topCodingPathNode: ptr)
var inner = JSONParserDecoder(state: parserState)
let result = try closure(&inner)
try inner._finishDecode()
return result
}
}
@_disfavoredOverload
@inlinable
public func decode<T: CommonDecodable>(_ type: T.Type, from bytes: RawSpan) throws(CodingError.Decoding) -> T {
try _decode(type, from: bytes) { parserDecoder throws(CodingError.Decoding) in
try parserDecoder.decode(type)
}
}
@usableFromInline
internal func _decode<T: JSONDecodable & ~Copyable>(_ type: T.Type, from utf8: UTF8Span, closure: (inout JSONParserDecoder) throws(CodingError.Decoding) -> T) throws(CodingError.Decoding) -> T {
var node = JSONParserDecoder.CodingPathNode.root
return try withUnsafeMutablePointer(to: &node) { ptr throws(CodingError.Decoding) in
let localOptions = self.options
let parserState = JSONParserDecoder.ParserState(utf8: utf8, options: localOptions&, topCodingPathNode: ptr)
var inner = JSONParserDecoder(state: parserState)
let result = try closure(&inner)
try inner._finishDecode()
return result
}
}
@inlinable
public func decode<T: JSONDecodable & ~Copyable>(_ type: T.Type, from utf8: UTF8Span) throws(CodingError.Decoding) -> T {
try _decode(type, from: utf8) { inner throws(CodingError.Decoding) in
try inner.decode(type)
}
}
@usableFromInline
internal func _decode<T: CommonDecodable>(_ type: T.Type, from utf8: UTF8Span, closure: (inout JSONParserDecoder) throws(CodingError.Decoding) -> T) throws(CodingError.Decoding) -> T {
var node = JSONParserDecoder.CodingPathNode.root
return try withUnsafeMutablePointer(to: &node) { ptr throws(CodingError.Decoding) in
let localOptions = self.options
let parserState = JSONParserDecoder.ParserState(utf8: utf8, options: localOptions&, topCodingPathNode: ptr)
var inner = JSONParserDecoder(state: parserState)
let result = try closure(&inner)
try inner._finishDecode()
return result
}
}
@_disfavoredOverload
@inlinable
public func decode<T: CommonDecodable>(_ type: T.Type, from utf8: UTF8Span) throws(CodingError.Decoding) -> T {
try _decode(type, from: utf8) { parserDecoder throws(CodingError.Decoding) in
try parserDecoder.decode(type)
}
}
}
// TODO: Move to Foundation + JSON cross-module import.
extension NewJSONDecoder {
@_alwaysEmitIntoClient
public func decode<T: JSONDecodable & ~Copyable>(_ type: T.Type, from data: Data) throws(CodingError.Decoding) -> T {
try self.decode(type, from: data.bytes)
}
@_disfavoredOverload
@_alwaysEmitIntoClient
public func decode<T: CommonDecodable>(_ type: T.Type, from data: Data) throws(CodingError.Decoding) -> T {
try self.decode(type, from: data.bytes)
}
}
@usableFromInline
protocol PrevalidatedJSONNumberBufferConvertible {
init?(prevalidatedBuffer buffer: borrowing RawSpan)
}
extension Double : PrevalidatedJSONNumberBufferConvertible {
@usableFromInline
init?(prevalidatedBuffer buffer: borrowing RawSpan) {
let decodedValue = buffer.withUnsafeBytes { buff -> Double? in
// TODO: Trying to diagnose why floating point parsing is failing w
// if let str = String._tryFromUTF8(buff.bindMemory(to: UInt8.self)) {
// print("Parsing: \(str) from baseAddress \(buff.baseAddress!)")
// }
var endPtr: UnsafeMutablePointer<CChar>? = nil
// TODO: strtoX_l everywhere.
let decodedValue = strtod(buff.baseAddress!, &endPtr)
if let endPtr {
if buff.baseAddress!.advanced(by: buff.count) == endPtr {
return decodedValue
}
// else {
// print("Got value: \(decodedValue) but failing prevalidatedBuffer because of bad pointer. Expected \(buff.baseAddress!.advanced(by: buff.count)) vs \(endPtr)")
// }
return nil
} else {
return nil
}
}
guard let decodedValue else { return nil }
self = decodedValue
}
}
extension Float : PrevalidatedJSONNumberBufferConvertible {
@usableFromInline
init?(prevalidatedBuffer buffer: RawSpan) {
let decodedValue = buffer.withUnsafeBytes { buff -> Float? in
var endPtr: UnsafeMutablePointer<CChar>? = nil
let decodedValue = strtof(buff.baseAddress!, &endPtr)
if let endPtr, buff.baseAddress!.advanced(by: buff.count) == endPtr {
return decodedValue
} else {
return nil
}
}
guard let decodedValue else { return nil }
self = decodedValue
}
}
extension NewJSONDecoder.Options.DateDecodingStrategy {
// @_specialize(where D == JSONParserDecoder)
// @_specialize(where D == JSONPrimitiveDecoder)
func decodeDate<D: JSONDecoderProtocol & ~Escapable>(from decoder: inout D) throws(CodingError.Decoding) -> Date {
switch self.storage {
case .deferredToDate:
let timeInterval = try decoder.decode(TimeInterval.self)
return .init(timeIntervalSinceReferenceDate: timeInterval)
case .secondsSince1970:
let timeInterval = try decoder.decode(TimeInterval.self)
return .init(timeIntervalSince1970: timeInterval)
case .millisecondsSince1970:
let timeInterval = try decoder.decode(TimeInterval.self)
return .init(timeIntervalSince1970: timeInterval / 1000.0)
case .iso8601:
return try decoder.decodeString(JSONParserDecoder.DateStringVistior.iso8601)
case .formatted(let strategy):
return try decoder.decodeString(JSONParserDecoder.DateStringVistior.formatted(strategy))
}
}
}
extension NewJSONDecoder.Options.DataDecodingStrategy {
// @_specialize(where D == JSONParserDecoder)
// @_specialize(where D == JSONPrimitiveDecoder)
func decodeData<D: JSONDecoderProtocol & ~Escapable>(from decoder: inout D) throws(CodingError.Decoding) -> Data {
switch self {
case .deferredToData:
let array = try decoder.decode([UInt8].self)
return Data(array)
case .base64:
return try decoder.decodeString(JSONParserDecoder.Base64Visitor())
}
}
}
extension NewJSONDecoder.Options.DateDecodingStrategy {
func decode(from decoder: AdaptorDecoder<JSONPrimitive>) throws(CodingError.Decoding) -> Date {
do {
switch storage {
case .deferredToDate:
return try Date(from: decoder)
case .secondsSince1970:
let timeInterval = try decoder.decode(TimeInterval.self)
return try JSONParserDecoder.DateNumberVisitor.secondsSince1970.visit(timeInterval)
case .millisecondsSince1970:
let timeIntervalMS = try decoder.decode(TimeInterval.self)
return try JSONParserDecoder.DateNumberVisitor.msSince1970.visit(timeIntervalMS)
case .iso8601:
let string = try decoder.decode(String.self)
return try JSONParserDecoder.DateStringVistior.iso8601.visitString(string)
case .formatted(let strategy):
let string = try decoder.decode(String.self)
return try JSONParserDecoder.DateStringVistior.formatted(strategy).visitString(string)
}
} catch {
fatalError("TODO: Better error handling/translation")
}
}
}
extension NewJSONEncoder.Options.DateEncodingStrategy {
func jsonValue(for date: Date, context: AdaptorEncodableValueContext<JSONPrimitive>) throws(CodingError.Encoding) -> JSONPrimitive {
do {
switch self.storage {
case .millisecondsSince1970:
let value = 1000.0 * date.timeIntervalSince1970
return try .init(value, context: context)
case .secondsSince1970:
return try .init(date.timeIntervalSince1970, context: context)
case .iso8601:
let string = date.formatted(.iso8601)
return .string(string)
case .formatted(let style):
let string = date.formatted(style)
return.string(string)
case .deferredToDate:
return try context.withEncoder {
try date.encode(to: $0)
return $0.encodedValue
}
}
} catch {
fatalError("TODO: better error handling/translation")
}
}
}