Skip to content

Commit

Permalink
Fixup after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
gjcairo committed Jan 17, 2025
1 parent cb7f55a commit ec70f7d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions Tests/GRPCCoreTests/Test Utilities/Coding+JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import GRPCCore

import struct Foundation.Data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,32 @@
* limitations under the License.
*/

import Foundation
import GRPCCore

struct JSONSerializer<Message: Encodable>: MessageSerializer {
private let encoder = JSONEncoder()
import struct Foundation.Data
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder

func serialize(_ message: Message) throws -> [UInt8] {
let data = try self.encoder.encode(message)
return Array(data)
struct JSONSerializer<Message: Codable>: MessageSerializer {
func serialize<Bytes: GRPCContiguousBytes>(_ message: Message) throws -> Bytes {
do {
let jsonEncoder = JSONEncoder()
let data = try jsonEncoder.encode(message)
return Bytes(data)
} catch {
throw RPCError(code: .internalError, message: "Can't serialize message to JSON. \(error)")
}
}
}

struct JSONDeserializer<Message: Decodable>: MessageDeserializer {
private let decoder = JSONDecoder()

func deserialize(_ serializedMessageBytes: [UInt8]) throws -> Message {
try self.decoder.decode(Message.self, from: Data(serializedMessageBytes))
struct JSONDeserializer<Message: Codable>: MessageDeserializer {
func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> Message {
do {
let jsonDecoder = JSONDecoder()
let data = serializedMessageBytes.withUnsafeBytes { Data($0) }
return try jsonDecoder.decode(Message.self, from: data)
} catch {
throw RPCError(code: .internalError, message: "Can't deserialze message from JSON. \(error)")
}
}
}

0 comments on commit ec70f7d

Please sign in to comment.