Skip to content

Commit

Permalink
prevent protobuf messages from being json serialized (#1710)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeralsing committed Jul 10, 2022
1 parent 3d46810 commit f3b8c35
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/Proto.Remote/Serialization/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ namespace Proto.Remote;
public class JsonSerializer : ISerializer
{
private readonly Serialization _serialization;
private readonly ConcurrentDictionary<string, Type> _jsonTypes = new ConcurrentDictionary<string, Type>();
private readonly ConcurrentDictionary<string, Type> _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)
{
Expand All @@ -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
};
}

0 comments on commit f3b8c35

Please sign in to comment.