|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2022 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +// |
| 17 | +// This source file is part of the SwiftNIO open source project |
| 18 | +// |
| 19 | +// Copyright (c) 2019-2021 Apple Inc. and the SwiftNIO project authors |
| 20 | +// Licensed under Apache License v2.0 |
| 21 | +// |
| 22 | +// See LICENSE.txt for license information |
| 23 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 24 | +// |
| 25 | +// SPDX-License-Identifier: Apache-2.0 |
| 26 | +// |
| 27 | +//===----------------------------------------------------------------------===// |
| 28 | + |
| 29 | +import NIOCore |
| 30 | + |
| 31 | +#if canImport(FoundationEssentials) |
| 32 | +import FoundationEssentials |
| 33 | +#else |
| 34 | +import Foundation |
| 35 | +#endif |
| 36 | + |
| 37 | +extension ByteBuffer { |
| 38 | + /// Attempts to decode the `length` bytes from `index` using the `JSONDecoder` `decoder` as `T`. |
| 39 | + /// |
| 40 | + /// - parameters: |
| 41 | + /// - type: The type type that is attempted to be decoded. |
| 42 | + /// - decoder: The `JSONDecoder` that is used for the decoding. |
| 43 | + /// - index: The index of the first byte to decode. |
| 44 | + /// - length: The number of bytes to decode. |
| 45 | + /// - returns: The decoded value if successful or `nil` if there are not enough readable bytes available. |
| 46 | + @inlinable |
| 47 | + func getJSONDecodable<T: Decodable>( |
| 48 | + _ type: T.Type, |
| 49 | + decoder: JSONDecoder = JSONDecoder(), |
| 50 | + at index: Int, |
| 51 | + length: Int |
| 52 | + ) throws -> T? { |
| 53 | + guard let data = self.getData(at: index, length: length, byteTransferStrategy: .noCopy) else { |
| 54 | + return nil |
| 55 | + } |
| 56 | + return try decoder.decode(T.self, from: data) |
| 57 | + } |
| 58 | + |
| 59 | + /// Encodes `value` using the `JSONEncoder` `encoder` and set the resulting bytes into this `ByteBuffer` at the |
| 60 | + /// given `index`. |
| 61 | + /// |
| 62 | + /// - note: The `writerIndex` remains unchanged. |
| 63 | + /// |
| 64 | + /// - parameters: |
| 65 | + /// - value: An `Encodable` value to encode. |
| 66 | + /// - encoder: The `JSONEncoder` to encode `value` with. |
| 67 | + /// - returns: The number of bytes written. |
| 68 | + @inlinable |
| 69 | + @discardableResult |
| 70 | + mutating func setJSONEncodable<T: Encodable>( |
| 71 | + _ value: T, |
| 72 | + encoder: JSONEncoder = JSONEncoder(), |
| 73 | + at index: Int |
| 74 | + ) throws -> Int { |
| 75 | + let data = try encoder.encode(value) |
| 76 | + return self.setBytes(data, at: index) |
| 77 | + } |
| 78 | + |
| 79 | + /// Encodes `value` using the `JSONEncoder` `encoder` and writes the resulting bytes into this `ByteBuffer`. |
| 80 | + /// |
| 81 | + /// If successful, this will move the writer index forward by the number of bytes written. |
| 82 | + /// |
| 83 | + /// - parameters: |
| 84 | + /// - value: An `Encodable` value to encode. |
| 85 | + /// - encoder: The `JSONEncoder` to encode `value` with. |
| 86 | + /// - returns: The number of bytes written. |
| 87 | + @inlinable |
| 88 | + @discardableResult |
| 89 | + mutating func writeJSONEncodable<T: Encodable>( |
| 90 | + _ value: T, |
| 91 | + encoder: JSONEncoder = JSONEncoder() |
| 92 | + ) throws -> Int { |
| 93 | + let result = try self.setJSONEncodable(value, encoder: encoder, at: self.writerIndex) |
| 94 | + self.moveWriterIndex(forwardBy: result) |
| 95 | + return result |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +extension JSONDecoder { |
| 100 | + /// Returns a value of the type you specify, decoded from a JSON object inside the readable bytes of a `ByteBuffer`. |
| 101 | + /// |
| 102 | + /// If the `ByteBuffer` does not contain valid JSON, this method throws the |
| 103 | + /// `DecodingError.dataCorrupted(_:)` error. If a value within the JSON |
| 104 | + /// fails to decode, this method throws the corresponding error. |
| 105 | + /// |
| 106 | + /// - note: The provided `ByteBuffer` remains unchanged, neither the `readerIndex` nor the `writerIndex` will move. |
| 107 | + /// If you would like the `readerIndex` to move, consider using `ByteBuffer.readJSONDecodable(_:length:)`. |
| 108 | + /// |
| 109 | + /// - parameters: |
| 110 | + /// - type: The type of the value to decode from the supplied JSON object. |
| 111 | + /// - buffer: The `ByteBuffer` that contains JSON object to decode. |
| 112 | + /// - returns: The decoded object. |
| 113 | + func decode<T: Decodable>(_ type: T.Type, from buffer: ByteBuffer) throws -> T { |
| 114 | + try buffer.getJSONDecodable( |
| 115 | + T.self, |
| 116 | + decoder: self, |
| 117 | + at: buffer.readerIndex, |
| 118 | + length: buffer.readableBytes |
| 119 | + )! // must work, enough readable bytes// must work, enough readable bytes |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +extension JSONEncoder { |
| 124 | + /// Writes a JSON-encoded representation of the value you supply into the supplied `ByteBuffer`. |
| 125 | + /// |
| 126 | + /// - parameters: |
| 127 | + /// - value: The value to encode as JSON. |
| 128 | + /// - buffer: The `ByteBuffer` to encode into. |
| 129 | + @inlinable |
| 130 | + func encode<T: Encodable>( |
| 131 | + _ value: T, |
| 132 | + into buffer: inout ByteBuffer |
| 133 | + ) throws { |
| 134 | + try buffer.writeJSONEncodable(value, encoder: self) |
| 135 | + } |
| 136 | + |
| 137 | + /// Writes a JSON-encoded representation of the value you supply into a `ByteBuffer` that is freshly allocated. |
| 138 | + /// |
| 139 | + /// - parameters: |
| 140 | + /// - value: The value to encode as JSON. |
| 141 | + /// - allocator: The `ByteBufferAllocator` which is used to allocate the `ByteBuffer` to be returned. |
| 142 | + /// - returns: The `ByteBuffer` containing the encoded JSON. |
| 143 | + func encodeAsByteBuffer<T: Encodable>(_ value: T, allocator: ByteBufferAllocator) throws -> ByteBuffer { |
| 144 | + let data = try self.encode(value) |
| 145 | + var buffer = allocator.buffer(capacity: data.count) |
| 146 | + buffer.writeBytes(data) |
| 147 | + return buffer |
| 148 | + } |
| 149 | +} |
0 commit comments