Skip to content

Commit ec70f7d

Browse files
committed
Fixup after rebase
1 parent cb7f55a commit ec70f7d

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

Diff for: Tests/GRPCCoreTests/Test Utilities/Coding+JSON.swift

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
import GRPCCore
1718

1819
import struct Foundation.Data

Diff for: Tests/GRPCInProcessTransportTests/Test Utilities/JSONSerializing.swift

+21-11
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,32 @@
1414
* limitations under the License.
1515
*/
1616

17-
import Foundation
1817
import GRPCCore
1918

20-
struct JSONSerializer<Message: Encodable>: MessageSerializer {
21-
private let encoder = JSONEncoder()
19+
import struct Foundation.Data
20+
import class Foundation.JSONDecoder
21+
import class Foundation.JSONEncoder
2222

23-
func serialize(_ message: Message) throws -> [UInt8] {
24-
let data = try self.encoder.encode(message)
25-
return Array(data)
23+
struct JSONSerializer<Message: Codable>: MessageSerializer {
24+
func serialize<Bytes: GRPCContiguousBytes>(_ message: Message) throws -> Bytes {
25+
do {
26+
let jsonEncoder = JSONEncoder()
27+
let data = try jsonEncoder.encode(message)
28+
return Bytes(data)
29+
} catch {
30+
throw RPCError(code: .internalError, message: "Can't serialize message to JSON. \(error)")
31+
}
2632
}
2733
}
2834

29-
struct JSONDeserializer<Message: Decodable>: MessageDeserializer {
30-
private let decoder = JSONDecoder()
31-
32-
func deserialize(_ serializedMessageBytes: [UInt8]) throws -> Message {
33-
try self.decoder.decode(Message.self, from: Data(serializedMessageBytes))
35+
struct JSONDeserializer<Message: Codable>: MessageDeserializer {
36+
func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> Message {
37+
do {
38+
let jsonDecoder = JSONDecoder()
39+
let data = serializedMessageBytes.withUnsafeBytes { Data($0) }
40+
return try jsonDecoder.decode(Message.self, from: data)
41+
} catch {
42+
throw RPCError(code: .internalError, message: "Can't deserialze message from JSON. \(error)")
43+
}
3444
}
3545
}

0 commit comments

Comments
 (0)