/src/swift-protobuf/Sources/SwiftProtobuf/JSONDecoder.swift
Line | Count | Source (jump to first uncovered line) |
1 | | // Sources/SwiftProtobuf/JSONDecoder.swift - JSON format decoding |
2 | | // |
3 | | // Copyright (c) 2014 - 2016 Apple Inc. and the project authors |
4 | | // Licensed under Apache License v2.0 with Runtime Library Exception |
5 | | // |
6 | | // See LICENSE.txt for license information: |
7 | | // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt |
8 | | // |
9 | | // ----------------------------------------------------------------------------- |
10 | | /// |
11 | | /// JSON format decoding engine. |
12 | | /// |
13 | | // ----------------------------------------------------------------------------- |
14 | | |
15 | | import Foundation |
16 | | |
17 | | internal struct JSONDecoder: Decoder { |
18 | | internal var scanner: JSONScanner |
19 | | internal var messageType: any Message.Type |
20 | 8.15M | private var fieldCount = 0 |
21 | 8.15M | private var isMapKey = false |
22 | | private var fieldNameMap: _NameMap? |
23 | | |
24 | 2.12k | internal var options: JSONDecodingOptions { |
25 | 2.12k | scanner.options |
26 | 2.12k | } |
27 | | |
28 | 28 | mutating func handleConflictingOneOf() throws { |
29 | 28 | throw JSONDecodingError.conflictingOneOf |
30 | 28 | } |
31 | | |
32 | | internal init( |
33 | | source: UnsafeRawBufferPointer, |
34 | | options: JSONDecodingOptions, |
35 | | messageType: any Message.Type, |
36 | | extensions: (any ExtensionMap)? |
37 | 70.6k | ) { |
38 | 70.6k | let scanner = JSONScanner( |
39 | 70.6k | source: source, |
40 | 70.6k | options: options, |
41 | 70.6k | extensions: extensions |
42 | 70.6k | ) |
43 | 70.6k | self.init(scanner: scanner, messageType: messageType) |
44 | 70.6k | } |
45 | | |
46 | 8.15M | private init(scanner: JSONScanner, messageType: any Message.Type) { |
47 | 8.15M | self.scanner = scanner |
48 | 8.15M | self.messageType = messageType |
49 | 8.15M | } |
50 | | |
51 | 5.85M | mutating func nextFieldNumber() throws -> Int? { |
52 | 5.85M | if scanner.skipOptionalObjectEnd() { |
53 | 1.35M | return nil |
54 | 4.50M | } |
55 | 4.50M | if fieldCount > 0 { |
56 | 2.26M | try scanner.skipRequiredComma() |
57 | 4.50M | } |
58 | 4.50M | let fieldNumber = try scanner.nextFieldNumber( |
59 | 4.50M | names: fieldNameMap!, |
60 | 4.50M | messageType: messageType |
61 | 4.50M | ) |
62 | 4.50M | if let fieldNumber = fieldNumber { |
63 | 3.65M | fieldCount += 1 |
64 | 3.65M | return fieldNumber |
65 | 3.65M | } |
66 | 847k | return nil |
67 | 4.50M | } |
68 | | |
69 | 0 | mutating func decodeSingularFloatField(value: inout Float) throws { |
70 | 0 | if scanner.skipOptionalNull() { |
71 | 0 | value = 0 |
72 | 0 | return |
73 | 0 | } |
74 | 0 | value = try scanner.nextFloat() |
75 | 0 | } |
76 | | |
77 | 59.0k | mutating func decodeSingularFloatField(value: inout Float?) throws { |
78 | 59.0k | if scanner.skipOptionalNull() { |
79 | 152 | value = nil |
80 | 152 | return |
81 | 58.8k | } |
82 | 58.8k | value = try scanner.nextFloat() |
83 | 58.8k | } |
84 | | |
85 | 73.9k | mutating func decodeRepeatedFloatField(value: inout [Float]) throws { |
86 | 73.9k | if scanner.skipOptionalNull() { |
87 | 1.45k | return |
88 | 72.5k | } |
89 | 72.5k | try scanner.skipRequiredArrayStart() |
90 | 72.5k | if scanner.skipOptionalArrayEnd() { |
91 | 39 | return |
92 | 72.4k | } |
93 | 138k | while true { |
94 | 138k | let n = try scanner.nextFloat() |
95 | 138k | value.append(n) |
96 | 138k | if scanner.skipOptionalArrayEnd() { |
97 | 72.3k | return |
98 | 72.3k | } |
99 | 66.0k | try scanner.skipRequiredComma() |
100 | 66.0k | } |
101 | 170 | } |
102 | | |
103 | 0 | mutating func decodeSingularDoubleField(value: inout Double) throws { |
104 | 0 | if scanner.skipOptionalNull() { |
105 | 0 | value = 0 |
106 | 0 | return |
107 | 0 | } |
108 | 0 | value = try scanner.nextDouble() |
109 | 0 | } |
110 | | |
111 | 131k | mutating func decodeSingularDoubleField(value: inout Double?) throws { |
112 | 131k | if scanner.skipOptionalNull() { |
113 | 8.14k | value = nil |
114 | 8.14k | return |
115 | 123k | } |
116 | 123k | value = try scanner.nextDouble() |
117 | 123k | } |
118 | | |
119 | 23.0k | mutating func decodeRepeatedDoubleField(value: inout [Double]) throws { |
120 | 23.0k | if scanner.skipOptionalNull() { |
121 | 252 | return |
122 | 22.7k | } |
123 | 22.7k | try scanner.skipRequiredArrayStart() |
124 | 22.7k | if scanner.skipOptionalArrayEnd() { |
125 | 15 | return |
126 | 22.7k | } |
127 | 65.5k | while true { |
128 | 65.3k | let n = try scanner.nextDouble() |
129 | 65.3k | value.append(n) |
130 | 65.3k | if scanner.skipOptionalArrayEnd() { |
131 | 22.6k | return |
132 | 42.7k | } |
133 | 42.7k | try scanner.skipRequiredComma() |
134 | 42.7k | } |
135 | 132 | } |
136 | | |
137 | 0 | mutating func decodeSingularInt32Field(value: inout Int32) throws { |
138 | 0 | if scanner.skipOptionalNull() { |
139 | 0 | value = 0 |
140 | 0 | return |
141 | 0 | } |
142 | 0 | let n = try scanner.nextSInt() |
143 | 0 | if n > Int64(Int32.max) || n < Int64(Int32.min) { |
144 | 0 | throw JSONDecodingError.numberRange |
145 | 0 | } |
146 | 0 | value = Int32(truncatingIfNeeded: n) |
147 | 0 | } |
148 | | |
149 | 338k | mutating func decodeSingularInt32Field(value: inout Int32?) throws { |
150 | 338k | if scanner.skipOptionalNull() { |
151 | 4.91k | value = nil |
152 | 4.91k | return |
153 | 334k | } |
154 | 334k | let n = try scanner.nextSInt() |
155 | 372k | if n > Int64(Int32.max) || n < Int64(Int32.min) { |
156 | 1.12k | throw JSONDecodingError.numberRange |
157 | 332k | } |
158 | 332k | value = Int32(truncatingIfNeeded: n) |
159 | 332k | } |
160 | | |
161 | 55.1k | mutating func decodeRepeatedInt32Field(value: inout [Int32]) throws { |
162 | 55.1k | if scanner.skipOptionalNull() { |
163 | 1.22k | return |
164 | 53.9k | } |
165 | 53.9k | try scanner.skipRequiredArrayStart() |
166 | 53.9k | if scanner.skipOptionalArrayEnd() { |
167 | 104 | return |
168 | 53.8k | } |
169 | 454k | while true { |
170 | 454k | let n = try scanner.nextSInt() |
171 | 459k | if n > Int64(Int32.max) || n < Int64(Int32.min) { |
172 | 57 | throw JSONDecodingError.numberRange |
173 | 454k | } |
174 | 454k | value.append(Int32(truncatingIfNeeded: n)) |
175 | 454k | if scanner.skipOptionalArrayEnd() { |
176 | 53.7k | return |
177 | 400k | } |
178 | 400k | try scanner.skipRequiredComma() |
179 | 400k | } |
180 | 87 | } |
181 | | |
182 | 0 | mutating func decodeSingularInt64Field(value: inout Int64) throws { |
183 | 0 | if scanner.skipOptionalNull() { |
184 | 0 | value = 0 |
185 | 0 | return |
186 | 0 | } |
187 | 0 | value = try scanner.nextSInt() |
188 | 0 | } |
189 | | |
190 | 1.44M | mutating func decodeSingularInt64Field(value: inout Int64?) throws { |
191 | 1.44M | if scanner.skipOptionalNull() { |
192 | 8.96k | value = nil |
193 | 8.96k | return |
194 | 1.43M | } |
195 | 1.43M | value = try scanner.nextSInt() |
196 | 1.43M | } |
197 | | |
198 | 70.5k | mutating func decodeRepeatedInt64Field(value: inout [Int64]) throws { |
199 | 70.5k | if scanner.skipOptionalNull() { |
200 | 1.40k | return |
201 | 69.1k | } |
202 | 69.1k | try scanner.skipRequiredArrayStart() |
203 | 69.1k | if scanner.skipOptionalArrayEnd() { |
204 | 1.05k | return |
205 | 68.0k | } |
206 | 671k | while true { |
207 | 670k | let n = try scanner.nextSInt() |
208 | 670k | value.append(n) |
209 | 670k | if scanner.skipOptionalArrayEnd() { |
210 | 67.9k | return |
211 | 602k | } |
212 | 602k | try scanner.skipRequiredComma() |
213 | 602k | } |
214 | 120 | } |
215 | | |
216 | 0 | mutating func decodeSingularUInt32Field(value: inout UInt32) throws { |
217 | 0 | if scanner.skipOptionalNull() { |
218 | 0 | value = 0 |
219 | 0 | return |
220 | 0 | } |
221 | 0 | let n = try scanner.nextUInt() |
222 | 0 | if n > UInt64(UInt32.max) { |
223 | 0 | throw JSONDecodingError.numberRange |
224 | 0 | } |
225 | 0 | value = UInt32(truncatingIfNeeded: n) |
226 | 0 | } |
227 | | |
228 | 881k | mutating func decodeSingularUInt32Field(value: inout UInt32?) throws { |
229 | 881k | if scanner.skipOptionalNull() { |
230 | 1.16k | value = nil |
231 | 1.16k | return |
232 | 880k | } |
233 | 880k | let n = try scanner.nextUInt() |
234 | 880k | if n > UInt64(UInt32.max) { |
235 | 332 | throw JSONDecodingError.numberRange |
236 | 880k | } |
237 | 880k | value = UInt32(truncatingIfNeeded: n) |
238 | 880k | } |
239 | | |
240 | 27.2k | mutating func decodeRepeatedUInt32Field(value: inout [UInt32]) throws { |
241 | 27.2k | if scanner.skipOptionalNull() { |
242 | 488 | return |
243 | 26.7k | } |
244 | 26.7k | try scanner.skipRequiredArrayStart() |
245 | 26.7k | if scanner.skipOptionalArrayEnd() { |
246 | 922 | return |
247 | 25.7k | } |
248 | 38.8k | while true { |
249 | 38.7k | let n = try scanner.nextUInt() |
250 | 38.7k | if n > UInt64(UInt32.max) { |
251 | 1 | throw JSONDecodingError.numberRange |
252 | 38.7k | } |
253 | 38.7k | value.append(UInt32(truncatingIfNeeded: n)) |
254 | 38.7k | if scanner.skipOptionalArrayEnd() { |
255 | 25.7k | return |
256 | 25.7k | } |
257 | 13.0k | try scanner.skipRequiredComma() |
258 | 13.0k | } |
259 | 77 | } |
260 | | |
261 | 0 | mutating func decodeSingularUInt64Field(value: inout UInt64) throws { |
262 | 0 | if scanner.skipOptionalNull() { |
263 | 0 | value = 0 |
264 | 0 | return |
265 | 0 | } |
266 | 0 | value = try scanner.nextUInt() |
267 | 0 | } |
268 | | |
269 | 963k | mutating func decodeSingularUInt64Field(value: inout UInt64?) throws { |
270 | 963k | if scanner.skipOptionalNull() { |
271 | 2.59k | value = nil |
272 | 2.59k | return |
273 | 960k | } |
274 | 960k | value = try scanner.nextUInt() |
275 | 960k | } |
276 | | |
277 | 70.7k | mutating func decodeRepeatedUInt64Field(value: inout [UInt64]) throws { |
278 | 70.7k | if scanner.skipOptionalNull() { |
279 | 1.39k | return |
280 | 69.3k | } |
281 | 69.3k | try scanner.skipRequiredArrayStart() |
282 | 69.3k | if scanner.skipOptionalArrayEnd() { |
283 | 94 | return |
284 | 69.2k | } |
285 | 85.7k | while true { |
286 | 85.6k | let n = try scanner.nextUInt() |
287 | 85.6k | value.append(n) |
288 | 85.6k | if scanner.skipOptionalArrayEnd() { |
289 | 69.2k | return |
290 | 69.2k | } |
291 | 16.4k | try scanner.skipRequiredComma() |
292 | 16.4k | } |
293 | 59 | } |
294 | | |
295 | 0 | mutating func decodeSingularSInt32Field(value: inout Int32) throws { |
296 | 0 | try decodeSingularInt32Field(value: &value) |
297 | 0 | } |
298 | | |
299 | 28.1k | mutating func decodeSingularSInt32Field(value: inout Int32?) throws { |
300 | 28.1k | try decodeSingularInt32Field(value: &value) |
301 | 28.1k | } |
302 | | |
303 | 11.7k | mutating func decodeRepeatedSInt32Field(value: inout [Int32]) throws { |
304 | 11.7k | try decodeRepeatedInt32Field(value: &value) |
305 | 11.7k | } |
306 | | |
307 | 0 | mutating func decodeSingularSInt64Field(value: inout Int64) throws { |
308 | 0 | try decodeSingularInt64Field(value: &value) |
309 | 0 | } |
310 | | |
311 | 33.7k | mutating func decodeSingularSInt64Field(value: inout Int64?) throws { |
312 | 33.7k | try decodeSingularInt64Field(value: &value) |
313 | 33.7k | } |
314 | | |
315 | 17.0k | mutating func decodeRepeatedSInt64Field(value: inout [Int64]) throws { |
316 | 17.0k | try decodeRepeatedInt64Field(value: &value) |
317 | 17.0k | } |
318 | | |
319 | 0 | mutating func decodeSingularFixed32Field(value: inout UInt32) throws { |
320 | 0 | try decodeSingularUInt32Field(value: &value) |
321 | 0 | } |
322 | | |
323 | 190k | mutating func decodeSingularFixed32Field(value: inout UInt32?) throws { |
324 | 190k | try decodeSingularUInt32Field(value: &value) |
325 | 190k | } |
326 | | |
327 | 13.2k | mutating func decodeRepeatedFixed32Field(value: inout [UInt32]) throws { |
328 | 13.2k | try decodeRepeatedUInt32Field(value: &value) |
329 | 13.2k | } |
330 | | |
331 | 0 | mutating func decodeSingularFixed64Field(value: inout UInt64) throws { |
332 | 0 | try decodeSingularUInt64Field(value: &value) |
333 | 0 | } |
334 | | |
335 | 201k | mutating func decodeSingularFixed64Field(value: inout UInt64?) throws { |
336 | 201k | try decodeSingularUInt64Field(value: &value) |
337 | 201k | } |
338 | | |
339 | 48.9k | mutating func decodeRepeatedFixed64Field(value: inout [UInt64]) throws { |
340 | 48.9k | try decodeRepeatedUInt64Field(value: &value) |
341 | 48.9k | } |
342 | | |
343 | 0 | mutating func decodeSingularSFixed32Field(value: inout Int32) throws { |
344 | 0 | try decodeSingularInt32Field(value: &value) |
345 | 0 | } |
346 | | |
347 | 14.4k | mutating func decodeSingularSFixed32Field(value: inout Int32?) throws { |
348 | 14.4k | try decodeSingularInt32Field(value: &value) |
349 | 14.4k | } |
350 | | |
351 | 26.4k | mutating func decodeRepeatedSFixed32Field(value: inout [Int32]) throws { |
352 | 26.4k | try decodeRepeatedInt32Field(value: &value) |
353 | 26.4k | } |
354 | | |
355 | 0 | mutating func decodeSingularSFixed64Field(value: inout Int64) throws { |
356 | 0 | try decodeSingularInt64Field(value: &value) |
357 | 0 | } |
358 | | |
359 | 21.2k | mutating func decodeSingularSFixed64Field(value: inout Int64?) throws { |
360 | 21.2k | try decodeSingularInt64Field(value: &value) |
361 | 21.2k | } |
362 | | |
363 | 47.8k | mutating func decodeRepeatedSFixed64Field(value: inout [Int64]) throws { |
364 | 47.8k | try decodeRepeatedInt64Field(value: &value) |
365 | 47.8k | } |
366 | | |
367 | 0 | mutating func decodeSingularBoolField(value: inout Bool) throws { |
368 | 0 | if scanner.skipOptionalNull() { |
369 | 0 | value = false |
370 | 0 | return |
371 | 0 | } |
372 | 0 | if isMapKey { |
373 | 0 | value = try scanner.nextQuotedBool() |
374 | 0 | } else { |
375 | 0 | value = try scanner.nextBool() |
376 | 0 | } |
377 | 0 | } |
378 | | |
379 | 20.3k | mutating func decodeSingularBoolField(value: inout Bool?) throws { |
380 | 20.3k | if scanner.skipOptionalNull() { |
381 | 2.49k | value = nil |
382 | 2.49k | return |
383 | 17.8k | } |
384 | 17.8k | if isMapKey { |
385 | 10.7k | value = try scanner.nextQuotedBool() |
386 | 17.8k | } else { |
387 | 7.10k | value = try scanner.nextBool() |
388 | 17.8k | } |
389 | 17.8k | } |
390 | | |
391 | 12.6k | mutating func decodeRepeatedBoolField(value: inout [Bool]) throws { |
392 | 12.6k | if scanner.skipOptionalNull() { |
393 | 53 | return |
394 | 12.5k | } |
395 | 12.5k | try scanner.skipRequiredArrayStart() |
396 | 12.5k | if scanner.skipOptionalArrayEnd() { |
397 | 237 | return |
398 | 12.3k | } |
399 | 16.0k | while true { |
400 | 16.0k | let n = try scanner.nextBool() |
401 | 16.0k | value.append(n) |
402 | 16.0k | if scanner.skipOptionalArrayEnd() { |
403 | 12.2k | return |
404 | 12.2k | } |
405 | 3.72k | try scanner.skipRequiredComma() |
406 | 3.72k | } |
407 | 63 | } |
408 | | |
409 | 0 | mutating func decodeSingularStringField(value: inout String) throws { |
410 | 0 | if scanner.skipOptionalNull() { |
411 | 0 | value = String() |
412 | 0 | return |
413 | 0 | } |
414 | 0 | value = try scanner.nextQuotedString() |
415 | 0 | } |
416 | | |
417 | 596k | mutating func decodeSingularStringField(value: inout String?) throws { |
418 | 596k | if scanner.skipOptionalNull() { |
419 | 776 | value = nil |
420 | 776 | return |
421 | 595k | } |
422 | 595k | value = try scanner.nextQuotedString() |
423 | 595k | } |
424 | | |
425 | 9.51k | mutating func decodeRepeatedStringField(value: inout [String]) throws { |
426 | 9.51k | if scanner.skipOptionalNull() { |
427 | 23 | return |
428 | 9.48k | } |
429 | 9.48k | try scanner.skipRequiredArrayStart() |
430 | 9.48k | if scanner.skipOptionalArrayEnd() { |
431 | 401 | return |
432 | 9.08k | } |
433 | 18.2k | while true { |
434 | 18.2k | let n = try scanner.nextQuotedString() |
435 | 18.2k | value.append(n) |
436 | 18.2k | if scanner.skipOptionalArrayEnd() { |
437 | 9.03k | return |
438 | 9.17k | } |
439 | 9.17k | try scanner.skipRequiredComma() |
440 | 9.17k | } |
441 | 57 | } |
442 | | |
443 | 0 | mutating func decodeSingularBytesField(value: inout Data) throws { |
444 | 0 | if scanner.skipOptionalNull() { |
445 | 0 | value = Data() |
446 | 0 | return |
447 | 0 | } |
448 | 0 | value = try scanner.nextBytesValue() |
449 | 0 | } |
450 | | |
451 | 27.9k | mutating func decodeSingularBytesField(value: inout Data?) throws { |
452 | 27.9k | if scanner.skipOptionalNull() { |
453 | 1.90k | value = nil |
454 | 1.90k | return |
455 | 26.0k | } |
456 | 26.0k | value = try scanner.nextBytesValue() |
457 | 26.0k | } |
458 | | |
459 | 8.31k | mutating func decodeRepeatedBytesField(value: inout [Data]) throws { |
460 | 8.31k | if scanner.skipOptionalNull() { |
461 | 194 | return |
462 | 8.11k | } |
463 | 8.11k | try scanner.skipRequiredArrayStart() |
464 | 8.11k | if scanner.skipOptionalArrayEnd() { |
465 | 29 | return |
466 | 8.08k | } |
467 | 19.7k | while true { |
468 | 19.6k | let n = try scanner.nextBytesValue() |
469 | 19.6k | value.append(n) |
470 | 19.6k | if scanner.skipOptionalArrayEnd() { |
471 | 7.97k | return |
472 | 11.6k | } |
473 | 11.6k | try scanner.skipRequiredComma() |
474 | 11.6k | } |
475 | 111 | } |
476 | | |
477 | | mutating func decodeSingularEnumField<E: Enum>(value: inout E?) throws |
478 | 24.2k | where E.RawValue == Int { |
479 | 24.2k | if scanner.skipOptionalNull() { |
480 | 611 | if let customDecodable = E.self as? any _CustomJSONCodable.Type { |
481 | 0 | value = try customDecodable.decodedFromJSONNull() as? E |
482 | 0 | return |
483 | 611 | } |
484 | 611 | value = nil |
485 | 611 | return |
486 | 23.6k | } |
487 | 23.6k | // Only change the value if a value was read. |
488 | 23.6k | if let e: E = try scanner.nextEnumValue() { |
489 | 19.8k | value = e |
490 | 23.6k | } |
491 | 23.6k | } |
492 | | |
493 | | mutating func decodeSingularEnumField<E: Enum>(value: inout E) throws |
494 | 0 | where E.RawValue == Int { |
495 | 0 | if scanner.skipOptionalNull() { |
496 | 0 | if let customDecodable = E.self as? any _CustomJSONCodable.Type { |
497 | 0 | value = try customDecodable.decodedFromJSONNull() as! E |
498 | 0 | return |
499 | 0 | } |
500 | 0 | value = E() |
501 | 0 | return |
502 | 0 | } |
503 | 0 | if let e: E = try scanner.nextEnumValue() { |
504 | 0 | value = e |
505 | 0 | } |
506 | 0 |
|
507 | 0 | } |
508 | | |
509 | | mutating func decodeRepeatedEnumField<E: Enum>(value: inout [E]) throws |
510 | 90.7k | where E.RawValue == Int { |
511 | 90.7k | if scanner.skipOptionalNull() { |
512 | 579 | return |
513 | 90.2k | } |
514 | 90.2k | try scanner.skipRequiredArrayStart() |
515 | 90.2k | if scanner.skipOptionalArrayEnd() { |
516 | 223 | return |
517 | 89.9k | } |
518 | 89.9k | let maybeCustomDecodable = E.self as? any _CustomJSONCodable.Type |
519 | 97.6k | while true { |
520 | 97.5k | if scanner.skipOptionalNull() { |
521 | 1 | if let customDecodable = maybeCustomDecodable { |
522 | 0 | let e = try customDecodable.decodedFromJSONNull() as! E |
523 | 0 | value.append(e) |
524 | 1 | } else { |
525 | 1 | throw JSONDecodingError.illegalNull |
526 | 1 | } |
527 | 97.5k | } else { |
528 | 97.5k | if let e: E = try scanner.nextEnumValue() { |
529 | 53.0k | value.append(e) |
530 | 97.5k | } |
531 | 97.5k | } |
532 | 97.5k | if scanner.skipOptionalArrayEnd() { |
533 | 89.8k | return |
534 | 89.8k | } |
535 | 7.69k | try scanner.skipRequiredComma() |
536 | 7.69k | } |
537 | 103 | } |
538 | | |
539 | 862k | internal mutating func decodeFullObject<M: Message>(message: inout M) throws { |
540 | 862k | guard let nameProviding = (M.self as? any _ProtoNameProviding.Type) else { |
541 | 0 | throw JSONDecodingError.missingFieldNames |
542 | 862k | } |
543 | 862k | fieldNameMap = nameProviding._protobuf_nameMap |
544 | 862k | if let m = message as? (any _CustomJSONCodable) { |
545 | 167k | var customCodable = m |
546 | 167k | try customCodable.decodeJSON(from: &self) |
547 | 167k | message = customCodable as! M |
548 | 862k | } else { |
549 | 695k | try scanner.skipRequiredObjectStart() |
550 | 695k | if scanner.skipOptionalObjectEnd() { |
551 | 134k | return |
552 | 560k | } |
553 | 560k | try message.decodeMessage(decoder: &self) |
554 | 727k | } |
555 | 727k | } |
556 | | |
557 | 279k | mutating func decodeSingularMessageField<M: Message>(value: inout M?) throws { |
558 | 279k | if scanner.skipOptionalNull() { |
559 | 4.26k | if M.self is any _CustomJSONCodable.Type { |
560 | 2.07k | value = |
561 | 2.07k | try (M.self as! any _CustomJSONCodable.Type).decodedFromJSONNull() as? M |
562 | 2.07k | return |
563 | 2.19k | } |
564 | 2.19k | // All other message field types treat 'null' as an unset |
565 | 2.19k | value = nil |
566 | 2.19k | return |
567 | 275k | } |
568 | 275k | if value == nil { |
569 | 240k | value = M() |
570 | 275k | } |
571 | 275k | var subDecoder = JSONDecoder(scanner: scanner, messageType: M.self) |
572 | 275k | try subDecoder.decodeFullObject(message: &value!) |
573 | 275k | assert(scanner.recursionBudget == subDecoder.scanner.recursionBudget) |
574 | 275k | scanner = subDecoder.scanner |
575 | 275k | } |
576 | | |
577 | | mutating func decodeRepeatedMessageField<M: Message>( |
578 | | value: inout [M] |
579 | 12.0k | ) throws { |
580 | 12.0k | if scanner.skipOptionalNull() { |
581 | 1.16k | return |
582 | 10.9k | } |
583 | 10.9k | try scanner.skipRequiredArrayStart() |
584 | 10.9k | if scanner.skipOptionalArrayEnd() { |
585 | 776 | return |
586 | 10.1k | } |
587 | 570k | while true { |
588 | 569k | if scanner.skipOptionalNull() { |
589 | 1 | var appended = false |
590 | 1 | if M.self is any _CustomJSONCodable.Type { |
591 | 0 | if let message = try (M.self as! any _CustomJSONCodable.Type) |
592 | 0 | .decodedFromJSONNull() as? M |
593 | 0 | { |
594 | 0 | value.append(message) |
595 | 0 | appended = true |
596 | 0 | } |
597 | 1 | } |
598 | 1 | if !appended { |
599 | 1 | throw JSONDecodingError.illegalNull |
600 | 1 | } |
601 | 569k | } else { |
602 | 569k | var message = M() |
603 | 569k | var subDecoder = JSONDecoder(scanner: scanner, messageType: M.self) |
604 | 569k | try subDecoder.decodeFullObject(message: &message) |
605 | 569k | value.append(message) |
606 | 569k | assert(scanner.recursionBudget == subDecoder.scanner.recursionBudget) |
607 | 569k | scanner = subDecoder.scanner |
608 | 569k | } |
609 | 569k | if scanner.skipOptionalArrayEnd() { |
610 | 8.80k | return |
611 | 560k | } |
612 | 560k | try scanner.skipRequiredComma() |
613 | 560k | } |
614 | 1.31k | } |
615 | | |
616 | 3.91k | mutating func decodeSingularGroupField<G: Message>(value: inout G?) throws { |
617 | 3.91k | try decodeSingularMessageField(value: &value) |
618 | 3.91k | } |
619 | | |
620 | 1.59k | mutating func decodeRepeatedGroupField<G: Message>(value: inout [G]) throws { |
621 | 1.59k | try decodeRepeatedMessageField(value: &value) |
622 | 1.59k | } |
623 | | |
624 | | mutating func decodeMapField<KeyType, ValueType: MapValueType>( |
625 | | fieldType: _ProtobufMap<KeyType, ValueType>.Type, |
626 | | value: inout _ProtobufMap<KeyType, ValueType>.BaseType |
627 | 99.4k | ) throws { |
628 | 99.4k | if scanner.skipOptionalNull() { |
629 | 3.29k | return |
630 | 96.1k | } |
631 | 96.1k | try scanner.skipRequiredObjectStart() |
632 | 96.1k | if scanner.skipOptionalObjectEnd() { |
633 | 12.7k | return |
634 | 83.4k | } |
635 | 415k | while true { |
636 | 415k | // Next character must be double quote, because |
637 | 415k | // map keys must always be quoted strings. |
638 | 415k | let c = try scanner.peekOneCharacter() |
639 | 415k | if c != "\"" { |
640 | 24 | throw JSONDecodingError.unquotedMapKey |
641 | 415k | } |
642 | 415k | isMapKey = true |
643 | 415k | var keyField: KeyType.BaseType? |
644 | 415k | try KeyType.decodeSingular(value: &keyField, from: &self) |
645 | 415k | isMapKey = false |
646 | 415k | try scanner.skipRequiredColon() |
647 | 415k | var valueField: ValueType.BaseType? |
648 | 415k | try ValueType.decodeSingular(value: &valueField, from: &self) |
649 | 415k | if let keyField = keyField, let valueField = valueField { |
650 | 414k | value[keyField] = valueField |
651 | 415k | } else { |
652 | 247 | throw JSONDecodingError.malformedMap |
653 | 414k | } |
654 | 414k | if scanner.skipOptionalObjectEnd() { |
655 | 82.7k | return |
656 | 332k | } |
657 | 332k | try scanner.skipRequiredComma() |
658 | 332k | } |
659 | 454 | } |
660 | | |
661 | | mutating func decodeMapField<KeyType, ValueType>( |
662 | | fieldType: _ProtobufEnumMap<KeyType, ValueType>.Type, |
663 | | value: inout _ProtobufEnumMap<KeyType, ValueType>.BaseType |
664 | 8.30k | ) throws where ValueType.RawValue == Int { |
665 | 8.30k | if scanner.skipOptionalNull() { |
666 | 150 | return |
667 | 8.15k | } |
668 | 8.15k | try scanner.skipRequiredObjectStart() |
669 | 8.15k | if scanner.skipOptionalObjectEnd() { |
670 | 1.06k | return |
671 | 7.09k | } |
672 | 12.3k | while true { |
673 | 12.2k | // Next character must be double quote, because |
674 | 12.2k | // map keys must always be quoted strings. |
675 | 12.2k | let c = try scanner.peekOneCharacter() |
676 | 12.2k | if c != "\"" { |
677 | 15 | throw JSONDecodingError.unquotedMapKey |
678 | 12.2k | } |
679 | 12.2k | isMapKey = true |
680 | 12.2k | var keyFieldOpt: KeyType.BaseType? |
681 | 12.2k | try KeyType.decodeSingular(value: &keyFieldOpt, from: &self) |
682 | 12.2k | guard let keyField = keyFieldOpt else { |
683 | 0 | throw JSONDecodingError.malformedMap |
684 | 12.2k | } |
685 | 12.2k | isMapKey = false |
686 | 12.2k | try scanner.skipRequiredColon() |
687 | 12.2k | var valueField: ValueType? |
688 | 12.2k | try decodeSingularEnumField(value: &valueField) |
689 | 12.2k | if let valueField = valueField { |
690 | 9.19k | value[keyField] = valueField |
691 | 12.2k | } else { |
692 | 3.06k | // Nothing, the only way ``decodeSingularEnumField(value:)`` leaves |
693 | 3.06k | // it as nil is if ignoreUnknownFields option is enabled which also |
694 | 3.06k | // means to ignore unknown enum values. |
695 | 12.2k | } |
696 | 12.2k | if scanner.skipOptionalObjectEnd() { |
697 | 6.98k | return |
698 | 6.98k | } |
699 | 5.26k | try scanner.skipRequiredComma() |
700 | 5.26k | } |
701 | 91 | } |
702 | | |
703 | | mutating func decodeMapField<KeyType, ValueType>( |
704 | | fieldType: _ProtobufMessageMap<KeyType, ValueType>.Type, |
705 | | value: inout _ProtobufMessageMap<KeyType, ValueType>.BaseType |
706 | 74.4k | ) throws { |
707 | 74.4k | if scanner.skipOptionalNull() { |
708 | 1.09k | return |
709 | 73.3k | } |
710 | 73.3k | try scanner.skipRequiredObjectStart() |
711 | 73.3k | if scanner.skipOptionalObjectEnd() { |
712 | 663 | return |
713 | 72.6k | } |
714 | 81.4k | while true { |
715 | 81.3k | // Next character must be double quote, because |
716 | 81.3k | // map keys must always be quoted strings. |
717 | 81.3k | let c = try scanner.peekOneCharacter() |
718 | 81.3k | if c != "\"" { |
719 | 16 | throw JSONDecodingError.unquotedMapKey |
720 | 81.3k | } |
721 | 81.3k | isMapKey = true |
722 | 81.3k | var keyField: KeyType.BaseType? |
723 | 81.3k | try KeyType.decodeSingular(value: &keyField, from: &self) |
724 | 81.3k | isMapKey = false |
725 | 81.3k | try scanner.skipRequiredColon() |
726 | 81.3k | var valueField: ValueType? |
727 | 81.3k | try decodeSingularMessageField(value: &valueField) |
728 | 81.3k | if let keyField = keyField, let valueField = valueField { |
729 | 80.7k | value[keyField] = valueField |
730 | 81.3k | } else { |
731 | 589 | throw JSONDecodingError.malformedMap |
732 | 80.7k | } |
733 | 80.7k | if scanner.skipOptionalObjectEnd() { |
734 | 72.0k | return |
735 | 72.0k | } |
736 | 8.77k | try scanner.skipRequiredComma() |
737 | 8.77k | } |
738 | 74 | } |
739 | | |
740 | | mutating func decodeExtensionField( |
741 | | values: inout ExtensionFieldValueSet, |
742 | | messageType: any Message.Type, |
743 | | fieldNumber: Int |
744 | 889k | ) throws { |
745 | 889k | // Force-unwrap: we can only get here if the extension exists. |
746 | 889k | let ext = scanner.extensions[messageType, fieldNumber]! |
747 | 889k | |
748 | 941k | try values.modify(index: fieldNumber) { fieldValue in |
749 | 941k | if fieldValue != nil { |
750 | 332k | try fieldValue!.decodeExtensionField(decoder: &self) |
751 | 941k | } else { |
752 | 609k | fieldValue = try ext._protobuf_newField(decoder: &self) |
753 | 941k | } |
754 | 941k | } |
755 | 889k | } |
756 | | } |