Skip to content

Commit 882788e

Browse files
authored
Move test JSON (de)serializer to its own file (#61)
This is a nit, but tests across different files are using `JSONSerializer` and `JSONDeserializer`, but it was defined at the bottom of one set of tests. For tidiness and to improve discoverability a bit, they should be in their own file.
1 parent efa4422 commit 882788e

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

Tests/GRPCNIOTransportHTTP2Tests/ControlMessages.swift

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,3 @@ enum CancellationKind: Codable {
133133

134134
struct Empty: Codable {
135135
}
136-
137-
struct JSONSerializer<Message: Encodable>: MessageSerializer {
138-
private let encoder = JSONEncoder()
139-
140-
func serialize<Bytes: GRPCContiguousBytes>(_ message: Message) throws -> Bytes {
141-
let data = try self.encoder.encode(message)
142-
return Bytes(data)
143-
}
144-
}
145-
146-
struct JSONDeserializer<Message: Decodable>: MessageDeserializer {
147-
private let decoder = JSONDecoder()
148-
149-
func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> Message {
150-
try serializedMessageBytes.withUnsafeBytes {
151-
try self.decoder.decode(Message.self, from: Data($0))
152-
}
153-
}
154-
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2025, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Foundation
18+
import GRPCCore
19+
20+
struct JSONSerializer<Message: Encodable>: MessageSerializer {
21+
private let encoder = JSONEncoder()
22+
23+
func serialize<Bytes: GRPCContiguousBytes>(_ message: Message) throws -> Bytes {
24+
let data = try self.encoder.encode(message)
25+
return Bytes(data)
26+
}
27+
}
28+
29+
struct JSONDeserializer<Message: Decodable>: MessageDeserializer {
30+
private let decoder = JSONDecoder()
31+
32+
func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> Message {
33+
try serializedMessageBytes.withUnsafeBytes {
34+
try self.decoder.decode(Message.self, from: Data($0))
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)