automated-testing-demo/shared/Serializer/JsonSerializer.cs

22 lines
595 B
C#
Raw Normal View History

2024-06-14 14:48:52 +00:00
using Confluent.Kafka;
using System.Text.Json;
namespace Serializers
{
public class JsonSerializer<T> : ISerializer<T>, IDeserializer<T>
{
public byte[] Serialize(T data, SerializationContext context)
{
return JsonSerializer.SerializeToUtf8Bytes(data);
}
public T Deserialize(ReadOnlySpan<byte> data, bool isNull, SerializationContext context)
{
if (!isNull)
{
return JsonSerializer.Deserialize<T>(data);
}
throw new Exception ("Data cannot be null");
}
}
}