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