From f3b8c3521da5e588ff1b53e96e6af0d7883d3c47 Mon Sep 17 00:00:00 2001 From: Roger Johansson Date: Sun, 10 Jul 2022 13:19:39 +0200 Subject: [PATCH] prevent protobuf messages from being json serialized (#1710) --- .../Serialization/JsonSerializer.cs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Proto.Remote/Serialization/JsonSerializer.cs b/src/Proto.Remote/Serialization/JsonSerializer.cs index 3dd0dd152b..768f942561 100644 --- a/src/Proto.Remote/Serialization/JsonSerializer.cs +++ b/src/Proto.Remote/Serialization/JsonSerializer.cs @@ -12,14 +12,12 @@ namespace Proto.Remote; public class JsonSerializer : ISerializer { private readonly Serialization _serialization; - private readonly ConcurrentDictionary _jsonTypes = new ConcurrentDictionary(); + private readonly ConcurrentDictionary _jsonTypes = new(); public JsonSerializer(Serialization serialization) => _serialization = serialization; - public ByteString Serialize(object obj) - { - return ByteString.CopyFromUtf8(System.Text.Json.JsonSerializer.Serialize(obj, _serialization.JsonSerializerOptions)); - } + public ByteString Serialize(object obj) => + ByteString.CopyFromUtf8(System.Text.Json.JsonSerializer.Serialize(obj, _serialization.JsonSerializerOptions)); public object Deserialize(ByteString bytes, string typeName) { @@ -29,10 +27,11 @@ public object Deserialize(ByteString bytes, string typeName) return message; } - public string GetTypeName(object obj) - { - return obj?.GetType()?.AssemblyQualifiedName ?? throw new ArgumentNullException(nameof(obj)); - } + public string GetTypeName(object obj) => obj.GetType().AssemblyQualifiedName ?? throw new ArgumentNullException(nameof(obj)); - public bool CanSerialize(object obj) => true; + public bool CanSerialize(object obj) => obj switch + { + IMessage => false, + _ => true + }; } \ No newline at end of file