Google_Protobuf_ListValue+Extensions.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Sources/SwiftProtobuf/Google_Protobuf_ListValue+Extensions.swift - ListValue extensions
  2. //
  3. // Copyright (c) 2014 - 2017 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. /// ListValue is a well-known message type that can be used to parse or encode
  12. /// arbitrary JSON arrays without a predefined schema.
  13. ///
  14. // -----------------------------------------------------------------------------
  15. extension Google_Protobuf_ListValue: ExpressibleByArrayLiteral {
  16. // TODO: Give this a direct array interface by proxying the interesting
  17. // bits down to values
  18. public typealias Element = Google_Protobuf_Value
  19. /// Creates a new `Google_Protobuf_ListValue` from an array literal containing
  20. /// `Google_Protobuf_Value` elements.
  21. public init(arrayLiteral elements: Element...) {
  22. self.init(values: elements)
  23. }
  24. }
  25. extension Google_Protobuf_ListValue: _CustomJSONCodable {
  26. internal func encodedJSONString(options: JSONEncodingOptions) throws -> String {
  27. var jsonEncoder = JSONEncoder()
  28. jsonEncoder.append(text: "[")
  29. var separator: StaticString = ""
  30. for v in values {
  31. jsonEncoder.append(staticText: separator)
  32. try v.serializeJSONValue(to: &jsonEncoder, options: options)
  33. separator = ","
  34. }
  35. jsonEncoder.append(text: "]")
  36. return jsonEncoder.stringResult
  37. }
  38. internal mutating func decodeJSON(from decoder: inout JSONDecoder) throws {
  39. if decoder.scanner.skipOptionalNull() {
  40. return
  41. }
  42. try decoder.scanner.skipRequiredArrayStart()
  43. // Since we override the JSON decoding, we can't rely
  44. // on the default recursion depth tracking.
  45. try decoder.scanner.incrementRecursionDepth()
  46. if decoder.scanner.skipOptionalArrayEnd() {
  47. decoder.scanner.decrementRecursionDepth()
  48. return
  49. }
  50. while true {
  51. var v = Google_Protobuf_Value()
  52. try v.decodeJSON(from: &decoder)
  53. values.append(v)
  54. if decoder.scanner.skipOptionalArrayEnd() {
  55. decoder.scanner.decrementRecursionDepth()
  56. return
  57. }
  58. try decoder.scanner.skipRequiredComma()
  59. }
  60. }
  61. }
  62. extension Google_Protobuf_ListValue {
  63. /// Creates a new `Google_Protobuf_ListValue` from the given array of
  64. /// `Google_Protobuf_Value` elements.
  65. ///
  66. /// - Parameter values: The list of `Google_Protobuf_Value` messages from
  67. /// which to create the `Google_Protobuf_ListValue`.
  68. public init(values: [Google_Protobuf_Value]) {
  69. self.init()
  70. self.values = values
  71. }
  72. /// Accesses the `Google_Protobuf_Value` at the specified position.
  73. ///
  74. /// - Parameter index: The position of the element to access.
  75. public subscript(index: Int) -> Google_Protobuf_Value {
  76. get { values[index] }
  77. set(newValue) { values[index] = newValue }
  78. }
  79. }