JSONDecoder.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. import Foundation
  15. internal struct JSONDecoder: Decoder {
  16. internal var scanner: JSONScanner
  17. internal var messageType: any Message.Type
  18. private var fieldCount = 0
  19. private var isMapKey = false
  20. private var fieldNameMap: _NameMap?
  21. internal var options: JSONDecodingOptions {
  22. scanner.options
  23. }
  24. mutating func handleConflictingOneOf() throws {
  25. throw JSONDecodingError.conflictingOneOf
  26. }
  27. internal init(
  28. source: UnsafeRawBufferPointer,
  29. options: JSONDecodingOptions,
  30. messageType: any Message.Type,
  31. extensions: (any ExtensionMap)?
  32. ) {
  33. let scanner = JSONScanner(
  34. source: source,
  35. options: options,
  36. extensions: extensions
  37. )
  38. self.init(scanner: scanner, messageType: messageType)
  39. }
  40. private init(scanner: JSONScanner, messageType: any Message.Type) {
  41. self.scanner = scanner
  42. self.messageType = messageType
  43. }
  44. mutating func nextFieldNumber() throws -> Int? {
  45. if scanner.skipOptionalObjectEnd() {
  46. return nil
  47. }
  48. if fieldCount > 0 {
  49. try scanner.skipRequiredComma()
  50. }
  51. let fieldNumber = try scanner.nextFieldNumber(
  52. names: fieldNameMap!,
  53. messageType: messageType
  54. )
  55. if let fieldNumber = fieldNumber {
  56. fieldCount += 1
  57. return fieldNumber
  58. }
  59. return nil
  60. }
  61. mutating func decodeSingularFloatField(value: inout Float) throws {
  62. if scanner.skipOptionalNull() {
  63. value = 0
  64. return
  65. }
  66. value = try scanner.nextFloat()
  67. }
  68. mutating func decodeSingularFloatField(value: inout Float?) throws {
  69. if scanner.skipOptionalNull() {
  70. value = nil
  71. return
  72. }
  73. value = try scanner.nextFloat()
  74. }
  75. mutating func decodeRepeatedFloatField(value: inout [Float]) throws {
  76. if scanner.skipOptionalNull() {
  77. return
  78. }
  79. try scanner.skipRequiredArrayStart()
  80. if scanner.skipOptionalArrayEnd() {
  81. return
  82. }
  83. while true {
  84. let n = try scanner.nextFloat()
  85. value.append(n)
  86. if scanner.skipOptionalArrayEnd() {
  87. return
  88. }
  89. try scanner.skipRequiredComma()
  90. }
  91. }
  92. mutating func decodeSingularDoubleField(value: inout Double) throws {
  93. if scanner.skipOptionalNull() {
  94. value = 0
  95. return
  96. }
  97. value = try scanner.nextDouble()
  98. }
  99. mutating func decodeSingularDoubleField(value: inout Double?) throws {
  100. if scanner.skipOptionalNull() {
  101. value = nil
  102. return
  103. }
  104. value = try scanner.nextDouble()
  105. }
  106. mutating func decodeRepeatedDoubleField(value: inout [Double]) throws {
  107. if scanner.skipOptionalNull() {
  108. return
  109. }
  110. try scanner.skipRequiredArrayStart()
  111. if scanner.skipOptionalArrayEnd() {
  112. return
  113. }
  114. while true {
  115. let n = try scanner.nextDouble()
  116. value.append(n)
  117. if scanner.skipOptionalArrayEnd() {
  118. return
  119. }
  120. try scanner.skipRequiredComma()
  121. }
  122. }
  123. mutating func decodeSingularInt32Field(value: inout Int32) throws {
  124. if scanner.skipOptionalNull() {
  125. value = 0
  126. return
  127. }
  128. let n = try scanner.nextSInt()
  129. if n > Int64(Int32.max) || n < Int64(Int32.min) {
  130. throw JSONDecodingError.numberRange
  131. }
  132. value = Int32(truncatingIfNeeded: n)
  133. }
  134. mutating func decodeSingularInt32Field(value: inout Int32?) throws {
  135. if scanner.skipOptionalNull() {
  136. value = nil
  137. return
  138. }
  139. let n = try scanner.nextSInt()
  140. if n > Int64(Int32.max) || n < Int64(Int32.min) {
  141. throw JSONDecodingError.numberRange
  142. }
  143. value = Int32(truncatingIfNeeded: n)
  144. }
  145. mutating func decodeRepeatedInt32Field(value: inout [Int32]) throws {
  146. if scanner.skipOptionalNull() {
  147. return
  148. }
  149. try scanner.skipRequiredArrayStart()
  150. if scanner.skipOptionalArrayEnd() {
  151. return
  152. }
  153. while true {
  154. let n = try scanner.nextSInt()
  155. if n > Int64(Int32.max) || n < Int64(Int32.min) {
  156. throw JSONDecodingError.numberRange
  157. }
  158. value.append(Int32(truncatingIfNeeded: n))
  159. if scanner.skipOptionalArrayEnd() {
  160. return
  161. }
  162. try scanner.skipRequiredComma()
  163. }
  164. }
  165. mutating func decodeSingularInt64Field(value: inout Int64) throws {
  166. if scanner.skipOptionalNull() {
  167. value = 0
  168. return
  169. }
  170. value = try scanner.nextSInt()
  171. }
  172. mutating func decodeSingularInt64Field(value: inout Int64?) throws {
  173. if scanner.skipOptionalNull() {
  174. value = nil
  175. return
  176. }
  177. value = try scanner.nextSInt()
  178. }
  179. mutating func decodeRepeatedInt64Field(value: inout [Int64]) throws {
  180. if scanner.skipOptionalNull() {
  181. return
  182. }
  183. try scanner.skipRequiredArrayStart()
  184. if scanner.skipOptionalArrayEnd() {
  185. return
  186. }
  187. while true {
  188. let n = try scanner.nextSInt()
  189. value.append(n)
  190. if scanner.skipOptionalArrayEnd() {
  191. return
  192. }
  193. try scanner.skipRequiredComma()
  194. }
  195. }
  196. mutating func decodeSingularUInt32Field(value: inout UInt32) throws {
  197. if scanner.skipOptionalNull() {
  198. value = 0
  199. return
  200. }
  201. let n = try scanner.nextUInt()
  202. if n > UInt64(UInt32.max) {
  203. throw JSONDecodingError.numberRange
  204. }
  205. value = UInt32(truncatingIfNeeded: n)
  206. }
  207. mutating func decodeSingularUInt32Field(value: inout UInt32?) throws {
  208. if scanner.skipOptionalNull() {
  209. value = nil
  210. return
  211. }
  212. let n = try scanner.nextUInt()
  213. if n > UInt64(UInt32.max) {
  214. throw JSONDecodingError.numberRange
  215. }
  216. value = UInt32(truncatingIfNeeded: n)
  217. }
  218. mutating func decodeRepeatedUInt32Field(value: inout [UInt32]) throws {
  219. if scanner.skipOptionalNull() {
  220. return
  221. }
  222. try scanner.skipRequiredArrayStart()
  223. if scanner.skipOptionalArrayEnd() {
  224. return
  225. }
  226. while true {
  227. let n = try scanner.nextUInt()
  228. if n > UInt64(UInt32.max) {
  229. throw JSONDecodingError.numberRange
  230. }
  231. value.append(UInt32(truncatingIfNeeded: n))
  232. if scanner.skipOptionalArrayEnd() {
  233. return
  234. }
  235. try scanner.skipRequiredComma()
  236. }
  237. }
  238. mutating func decodeSingularUInt64Field(value: inout UInt64) throws {
  239. if scanner.skipOptionalNull() {
  240. value = 0
  241. return
  242. }
  243. value = try scanner.nextUInt()
  244. }
  245. mutating func decodeSingularUInt64Field(value: inout UInt64?) throws {
  246. if scanner.skipOptionalNull() {
  247. value = nil
  248. return
  249. }
  250. value = try scanner.nextUInt()
  251. }
  252. mutating func decodeRepeatedUInt64Field(value: inout [UInt64]) throws {
  253. if scanner.skipOptionalNull() {
  254. return
  255. }
  256. try scanner.skipRequiredArrayStart()
  257. if scanner.skipOptionalArrayEnd() {
  258. return
  259. }
  260. while true {
  261. let n = try scanner.nextUInt()
  262. value.append(n)
  263. if scanner.skipOptionalArrayEnd() {
  264. return
  265. }
  266. try scanner.skipRequiredComma()
  267. }
  268. }
  269. mutating func decodeSingularSInt32Field(value: inout Int32) throws {
  270. try decodeSingularInt32Field(value: &value)
  271. }
  272. mutating func decodeSingularSInt32Field(value: inout Int32?) throws {
  273. try decodeSingularInt32Field(value: &value)
  274. }
  275. mutating func decodeRepeatedSInt32Field(value: inout [Int32]) throws {
  276. try decodeRepeatedInt32Field(value: &value)
  277. }
  278. mutating func decodeSingularSInt64Field(value: inout Int64) throws {
  279. try decodeSingularInt64Field(value: &value)
  280. }
  281. mutating func decodeSingularSInt64Field(value: inout Int64?) throws {
  282. try decodeSingularInt64Field(value: &value)
  283. }
  284. mutating func decodeRepeatedSInt64Field(value: inout [Int64]) throws {
  285. try decodeRepeatedInt64Field(value: &value)
  286. }
  287. mutating func decodeSingularFixed32Field(value: inout UInt32) throws {
  288. try decodeSingularUInt32Field(value: &value)
  289. }
  290. mutating func decodeSingularFixed32Field(value: inout UInt32?) throws {
  291. try decodeSingularUInt32Field(value: &value)
  292. }
  293. mutating func decodeRepeatedFixed32Field(value: inout [UInt32]) throws {
  294. try decodeRepeatedUInt32Field(value: &value)
  295. }
  296. mutating func decodeSingularFixed64Field(value: inout UInt64) throws {
  297. try decodeSingularUInt64Field(value: &value)
  298. }
  299. mutating func decodeSingularFixed64Field(value: inout UInt64?) throws {
  300. try decodeSingularUInt64Field(value: &value)
  301. }
  302. mutating func decodeRepeatedFixed64Field(value: inout [UInt64]) throws {
  303. try decodeRepeatedUInt64Field(value: &value)
  304. }
  305. mutating func decodeSingularSFixed32Field(value: inout Int32) throws {
  306. try decodeSingularInt32Field(value: &value)
  307. }
  308. mutating func decodeSingularSFixed32Field(value: inout Int32?) throws {
  309. try decodeSingularInt32Field(value: &value)
  310. }
  311. mutating func decodeRepeatedSFixed32Field(value: inout [Int32]) throws {
  312. try decodeRepeatedInt32Field(value: &value)
  313. }
  314. mutating func decodeSingularSFixed64Field(value: inout Int64) throws {
  315. try decodeSingularInt64Field(value: &value)
  316. }
  317. mutating func decodeSingularSFixed64Field(value: inout Int64?) throws {
  318. try decodeSingularInt64Field(value: &value)
  319. }
  320. mutating func decodeRepeatedSFixed64Field(value: inout [Int64]) throws {
  321. try decodeRepeatedInt64Field(value: &value)
  322. }
  323. mutating func decodeSingularBoolField(value: inout Bool) throws {
  324. if scanner.skipOptionalNull() {
  325. value = false
  326. return
  327. }
  328. if isMapKey {
  329. value = try scanner.nextQuotedBool()
  330. } else {
  331. value = try scanner.nextBool()
  332. }
  333. }
  334. mutating func decodeSingularBoolField(value: inout Bool?) throws {
  335. if scanner.skipOptionalNull() {
  336. value = nil
  337. return
  338. }
  339. if isMapKey {
  340. value = try scanner.nextQuotedBool()
  341. } else {
  342. value = try scanner.nextBool()
  343. }
  344. }
  345. mutating func decodeRepeatedBoolField(value: inout [Bool]) throws {
  346. if scanner.skipOptionalNull() {
  347. return
  348. }
  349. try scanner.skipRequiredArrayStart()
  350. if scanner.skipOptionalArrayEnd() {
  351. return
  352. }
  353. while true {
  354. let n = try scanner.nextBool()
  355. value.append(n)
  356. if scanner.skipOptionalArrayEnd() {
  357. return
  358. }
  359. try scanner.skipRequiredComma()
  360. }
  361. }
  362. mutating func decodeSingularStringField(value: inout String) throws {
  363. if scanner.skipOptionalNull() {
  364. value = String()
  365. return
  366. }
  367. value = try scanner.nextQuotedString()
  368. }
  369. mutating func decodeSingularStringField(value: inout String?) throws {
  370. if scanner.skipOptionalNull() {
  371. value = nil
  372. return
  373. }
  374. value = try scanner.nextQuotedString()
  375. }
  376. mutating func decodeRepeatedStringField(value: inout [String]) throws {
  377. if scanner.skipOptionalNull() {
  378. return
  379. }
  380. try scanner.skipRequiredArrayStart()
  381. if scanner.skipOptionalArrayEnd() {
  382. return
  383. }
  384. while true {
  385. let n = try scanner.nextQuotedString()
  386. value.append(n)
  387. if scanner.skipOptionalArrayEnd() {
  388. return
  389. }
  390. try scanner.skipRequiredComma()
  391. }
  392. }
  393. mutating func decodeSingularBytesField(value: inout Data) throws {
  394. if scanner.skipOptionalNull() {
  395. value = Data()
  396. return
  397. }
  398. value = try scanner.nextBytesValue()
  399. }
  400. mutating func decodeSingularBytesField(value: inout Data?) throws {
  401. if scanner.skipOptionalNull() {
  402. value = nil
  403. return
  404. }
  405. value = try scanner.nextBytesValue()
  406. }
  407. mutating func decodeRepeatedBytesField(value: inout [Data]) throws {
  408. if scanner.skipOptionalNull() {
  409. return
  410. }
  411. try scanner.skipRequiredArrayStart()
  412. if scanner.skipOptionalArrayEnd() {
  413. return
  414. }
  415. while true {
  416. let n = try scanner.nextBytesValue()
  417. value.append(n)
  418. if scanner.skipOptionalArrayEnd() {
  419. return
  420. }
  421. try scanner.skipRequiredComma()
  422. }
  423. }
  424. mutating func decodeSingularEnumField<E: Enum>(value: inout E?) throws
  425. where E.RawValue == Int {
  426. if scanner.skipOptionalNull() {
  427. if let customDecodable = E.self as? any _CustomJSONCodable.Type {
  428. value = try customDecodable.decodedFromJSONNull() as? E
  429. return
  430. }
  431. value = nil
  432. return
  433. }
  434. // Only change the value if a value was read.
  435. if let e: E = try scanner.nextEnumValue() {
  436. value = e
  437. }
  438. }
  439. mutating func decodeSingularEnumField<E: Enum>(value: inout E) throws
  440. where E.RawValue == Int {
  441. if scanner.skipOptionalNull() {
  442. if let customDecodable = E.self as? any _CustomJSONCodable.Type {
  443. value = try customDecodable.decodedFromJSONNull() as! E
  444. return
  445. }
  446. value = E()
  447. return
  448. }
  449. if let e: E = try scanner.nextEnumValue() {
  450. value = e
  451. }
  452. }
  453. mutating func decodeRepeatedEnumField<E: Enum>(value: inout [E]) throws
  454. where E.RawValue == Int {
  455. if scanner.skipOptionalNull() {
  456. return
  457. }
  458. try scanner.skipRequiredArrayStart()
  459. if scanner.skipOptionalArrayEnd() {
  460. return
  461. }
  462. let maybeCustomDecodable = E.self as? any _CustomJSONCodable.Type
  463. while true {
  464. if scanner.skipOptionalNull() {
  465. if let customDecodable = maybeCustomDecodable {
  466. let e = try customDecodable.decodedFromJSONNull() as! E
  467. value.append(e)
  468. } else {
  469. throw JSONDecodingError.illegalNull
  470. }
  471. } else {
  472. if let e: E = try scanner.nextEnumValue() {
  473. value.append(e)
  474. }
  475. }
  476. if scanner.skipOptionalArrayEnd() {
  477. return
  478. }
  479. try scanner.skipRequiredComma()
  480. }
  481. }
  482. internal mutating func decodeFullObject<M: Message>(message: inout M) throws {
  483. guard let nameProviding = (M.self as? any _ProtoNameProviding.Type) else {
  484. throw JSONDecodingError.missingFieldNames
  485. }
  486. fieldNameMap = nameProviding._protobuf_nameMap
  487. if let m = message as? (any _CustomJSONCodable) {
  488. var customCodable = m
  489. try customCodable.decodeJSON(from: &self)
  490. message = customCodable as! M
  491. } else {
  492. try scanner.skipRequiredObjectStart()
  493. if scanner.skipOptionalObjectEnd() {
  494. return
  495. }
  496. try message.decodeMessage(decoder: &self)
  497. }
  498. }
  499. mutating func decodeSingularMessageField<M: Message>(value: inout M?) throws {
  500. if scanner.skipOptionalNull() {
  501. if M.self is any _CustomJSONCodable.Type {
  502. value =
  503. try (M.self as! any _CustomJSONCodable.Type).decodedFromJSONNull() as? M
  504. return
  505. }
  506. // All other message field types treat 'null' as an unset
  507. value = nil
  508. return
  509. }
  510. if value == nil {
  511. value = M()
  512. }
  513. var subDecoder = JSONDecoder(scanner: scanner, messageType: M.self)
  514. try subDecoder.decodeFullObject(message: &value!)
  515. assert(scanner.recursionBudget == subDecoder.scanner.recursionBudget)
  516. scanner = subDecoder.scanner
  517. }
  518. mutating func decodeRepeatedMessageField<M: Message>(
  519. value: inout [M]
  520. ) throws {
  521. if scanner.skipOptionalNull() {
  522. return
  523. }
  524. try scanner.skipRequiredArrayStart()
  525. if scanner.skipOptionalArrayEnd() {
  526. return
  527. }
  528. while true {
  529. if scanner.skipOptionalNull() {
  530. var appended = false
  531. if M.self is any _CustomJSONCodable.Type {
  532. if let message = try (M.self as! any _CustomJSONCodable.Type)
  533. .decodedFromJSONNull() as? M
  534. {
  535. value.append(message)
  536. appended = true
  537. }
  538. }
  539. if !appended {
  540. throw JSONDecodingError.illegalNull
  541. }
  542. } else {
  543. var message = M()
  544. var subDecoder = JSONDecoder(scanner: scanner, messageType: M.self)
  545. try subDecoder.decodeFullObject(message: &message)
  546. value.append(message)
  547. assert(scanner.recursionBudget == subDecoder.scanner.recursionBudget)
  548. scanner = subDecoder.scanner
  549. }
  550. if scanner.skipOptionalArrayEnd() {
  551. return
  552. }
  553. try scanner.skipRequiredComma()
  554. }
  555. }
  556. mutating func decodeSingularGroupField<G: Message>(value: inout G?) throws {
  557. try decodeSingularMessageField(value: &value)
  558. }
  559. mutating func decodeRepeatedGroupField<G: Message>(value: inout [G]) throws {
  560. try decodeRepeatedMessageField(value: &value)
  561. }
  562. mutating func decodeMapField<KeyType, ValueType: MapValueType>(
  563. fieldType: _ProtobufMap<KeyType, ValueType>.Type,
  564. value: inout _ProtobufMap<KeyType, ValueType>.BaseType
  565. ) throws {
  566. if scanner.skipOptionalNull() {
  567. return
  568. }
  569. try scanner.skipRequiredObjectStart()
  570. if scanner.skipOptionalObjectEnd() {
  571. return
  572. }
  573. while true {
  574. // Next character must be double quote, because
  575. // map keys must always be quoted strings.
  576. let c = try scanner.peekOneCharacter()
  577. if c != "\"" {
  578. throw JSONDecodingError.unquotedMapKey
  579. }
  580. isMapKey = true
  581. var keyField: KeyType.BaseType?
  582. try KeyType.decodeSingular(value: &keyField, from: &self)
  583. isMapKey = false
  584. try scanner.skipRequiredColon()
  585. var valueField: ValueType.BaseType?
  586. try ValueType.decodeSingular(value: &valueField, from: &self)
  587. if let keyField = keyField, let valueField = valueField {
  588. value[keyField] = valueField
  589. } else {
  590. throw JSONDecodingError.malformedMap
  591. }
  592. if scanner.skipOptionalObjectEnd() {
  593. return
  594. }
  595. try scanner.skipRequiredComma()
  596. }
  597. }
  598. mutating func decodeMapField<KeyType, ValueType>(
  599. fieldType: _ProtobufEnumMap<KeyType, ValueType>.Type,
  600. value: inout _ProtobufEnumMap<KeyType, ValueType>.BaseType
  601. ) throws where ValueType.RawValue == Int {
  602. if scanner.skipOptionalNull() {
  603. return
  604. }
  605. try scanner.skipRequiredObjectStart()
  606. if scanner.skipOptionalObjectEnd() {
  607. return
  608. }
  609. while true {
  610. // Next character must be double quote, because
  611. // map keys must always be quoted strings.
  612. let c = try scanner.peekOneCharacter()
  613. if c != "\"" {
  614. throw JSONDecodingError.unquotedMapKey
  615. }
  616. isMapKey = true
  617. var keyFieldOpt: KeyType.BaseType?
  618. try KeyType.decodeSingular(value: &keyFieldOpt, from: &self)
  619. guard let keyField = keyFieldOpt else {
  620. throw JSONDecodingError.malformedMap
  621. }
  622. isMapKey = false
  623. try scanner.skipRequiredColon()
  624. var valueField: ValueType?
  625. try decodeSingularEnumField(value: &valueField)
  626. if let valueField = valueField {
  627. value[keyField] = valueField
  628. } else {
  629. // Nothing, the only way ``decodeSingularEnumField(value:)`` leaves
  630. // it as nil is if ignoreUnknownFields option is enabled which also
  631. // means to ignore unknown enum values.
  632. }
  633. if scanner.skipOptionalObjectEnd() {
  634. return
  635. }
  636. try scanner.skipRequiredComma()
  637. }
  638. }
  639. mutating func decodeMapField<KeyType, ValueType>(
  640. fieldType: _ProtobufMessageMap<KeyType, ValueType>.Type,
  641. value: inout _ProtobufMessageMap<KeyType, ValueType>.BaseType
  642. ) throws {
  643. if scanner.skipOptionalNull() {
  644. return
  645. }
  646. try scanner.skipRequiredObjectStart()
  647. if scanner.skipOptionalObjectEnd() {
  648. return
  649. }
  650. while true {
  651. // Next character must be double quote, because
  652. // map keys must always be quoted strings.
  653. let c = try scanner.peekOneCharacter()
  654. if c != "\"" {
  655. throw JSONDecodingError.unquotedMapKey
  656. }
  657. isMapKey = true
  658. var keyField: KeyType.BaseType?
  659. try KeyType.decodeSingular(value: &keyField, from: &self)
  660. isMapKey = false
  661. try scanner.skipRequiredColon()
  662. var valueField: ValueType?
  663. try decodeSingularMessageField(value: &valueField)
  664. if let keyField = keyField, let valueField = valueField {
  665. value[keyField] = valueField
  666. } else {
  667. throw JSONDecodingError.malformedMap
  668. }
  669. if scanner.skipOptionalObjectEnd() {
  670. return
  671. }
  672. try scanner.skipRequiredComma()
  673. }
  674. }
  675. mutating func decodeExtensionField(
  676. values: inout ExtensionFieldValueSet,
  677. messageType: any Message.Type,
  678. fieldNumber: Int
  679. ) throws {
  680. // Force-unwrap: we can only get here if the extension exists.
  681. let ext = scanner.extensions[messageType, fieldNumber]!
  682. try values.modify(index: fieldNumber) { fieldValue in
  683. if fieldValue != nil {
  684. try fieldValue!.decodeExtensionField(decoder: &self)
  685. } else {
  686. fieldValue = try ext._protobuf_newField(decoder: &self)
  687. }
  688. }
  689. }
  690. }