2022-02-03 15:26:44 +00:00
|
|
|
|
namespace ASC.Common.Caching;
|
|
|
|
|
|
|
|
|
|
public class ProtobufSerializer<T> : ISerializer<T> where T : IMessage<T>, new()
|
2019-07-19 13:10:15 +00:00
|
|
|
|
{
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public byte[] Serialize(T data, SerializationContext context) => data.ToByteArray();
|
|
|
|
|
}
|
2019-07-19 13:10:15 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public class ProtobufDeserializer<T> : IDeserializer<T> where T : IMessage<T>, new()
|
|
|
|
|
{
|
|
|
|
|
private readonly MessageParser<T> _parser;
|
2019-07-19 13:10:15 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public ProtobufDeserializer() => _parser = new MessageParser<T>(() => new T());
|
2019-07-19 13:10:15 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public T Deserialize(ReadOnlySpan<byte> data, bool isNull, SerializationContext context)
|
|
|
|
|
=> _parser.ParseFrom(data.ToArray());
|
|
|
|
|
}
|
2019-08-28 12:58:53 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public static class GuidExtension
|
|
|
|
|
{
|
|
|
|
|
public static ByteString ToByteString(this Guid id) => ByteString.CopyFrom(id.ToByteArray());
|
2022-02-01 14:51:29 +00:00
|
|
|
|
|
2022-02-03 15:26:44 +00:00
|
|
|
|
public static Guid FromByteString(this ByteString id) => new Guid(id.ToByteArray());
|
|
|
|
|
}
|