Message+JSONArrayAdditions_Data.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Sources/SwiftProtobuf/Message+JSONArrayAdditions_Data.swift - JSON format primitive types
  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. /// Extensions to `Array` to support JSON encoding/decoding.
  12. ///
  13. // -----------------------------------------------------------------------------
  14. import Foundation
  15. /// JSON encoding and decoding methods for arrays of messages.
  16. extension Message {
  17. /// Creates a new array of messages by decoding the given `Data`
  18. /// containing a serialized array of messages in JSON format, interpreting the data as
  19. /// UTF-8 encoded text.
  20. ///
  21. /// - Parameter jsonUTF8Data: The JSON-formatted data to decode, represented
  22. /// as UTF-8 encoded text.
  23. /// - Parameter options: The JSONDecodingOptions to use.
  24. /// - Throws: ``SwiftProtobufError`` or ``JSONDecodingError`` if decoding fails.
  25. public static func array(
  26. fromJSONUTF8Data jsonUTF8Data: Data,
  27. options: JSONDecodingOptions = JSONDecodingOptions()
  28. ) throws -> [Self] {
  29. try self.array(
  30. fromJSONUTF8Bytes: jsonUTF8Data,
  31. extensions: SimpleExtensionMap(),
  32. options: options
  33. )
  34. }
  35. /// Creates a new array of messages by decoding the given `Data`
  36. /// containing a serialized array of messages in JSON format, interpreting the data as
  37. /// UTF-8 encoded text.
  38. ///
  39. /// - Parameter jsonUTF8Data: The JSON-formatted data to decode, represented
  40. /// as UTF-8 encoded text.
  41. /// - Parameter extensions: The extension map to use with this decode
  42. /// - Parameter options: The JSONDecodingOptions to use.
  43. /// - Throws: ``SwiftProtobufError`` or ``JSONDecodingError`` if decoding fails.
  44. public static func array(
  45. fromJSONUTF8Data jsonUTF8Data: Data,
  46. extensions: any ExtensionMap = SimpleExtensionMap(),
  47. options: JSONDecodingOptions = JSONDecodingOptions()
  48. ) throws -> [Self] {
  49. try array(
  50. fromJSONUTF8Bytes: jsonUTF8Data,
  51. extensions: extensions,
  52. options: options
  53. )
  54. }
  55. /// Returns a Data containing the UTF-8 JSON serialization of the messages.
  56. ///
  57. /// Unlike binary encoding, presence of required fields is not enforced when
  58. /// serializing to JSON.
  59. ///
  60. /// - Returns: A Data containing the JSON serialization of the messages.
  61. /// - Parameters:
  62. /// - collection: The list of messages to encode.
  63. /// - options: The JSONEncodingOptions to use.
  64. /// - Throws: ``SwiftProtobufError`` or ``JSONEncodingError`` if encoding fails.
  65. public static func jsonUTF8Data<C: Collection>(
  66. from collection: C,
  67. options: JSONEncodingOptions = JSONEncodingOptions()
  68. ) throws -> Data where C.Iterator.Element == Self {
  69. try jsonUTF8Bytes(from: collection, options: options)
  70. }
  71. }