In the docs for the .NET SDK, you provide an example of a custom ITypeSerializer implementation using System.Text.Json.
public class DotnetJsonSerializer : ITypeSerializer
{
public T Deserialize<T>(ReadOnlyMemory<byte> buffer)
{
return JsonSerializer.Deserialize<T>(buffer.Span);
}
public T Deserialize<T>(Stream stream)
{
using var ms = new MemoryStream();
stream.CopyTo(ms);
var span = new ReadOnlySpan<byte>(ms.GetBuffer()).Slice(0, (int)ms.Length);
return JsonSerializer.Deserialize<T>(span);
}
public ValueTask<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default)
{
return JsonSerializer.DeserializeAsync<T>(stream, null, cancellationToken);
}
public void Serialize(Stream stream, object obj)
{
using var jsonUtf8Writer = new Utf8JsonWriter(stream);
JsonSerializer.Serialize(jsonUtf8Writer, obj);
}
public ValueTask SerializeAsync(Stream stream, object obj, CancellationToken cancellationToken = default)
{
return new ValueTask(JsonSerializer.SerializeAsync(stream, obj, null, cancellationToken));
}
}
My question is, is the code provided in that example correct and safe for production (for KV gets and sets without projection)? What concerns me about the code is that in the doc’s explanation of the code, it refers to Google Gson. Goggle Gson has nothing to do with .NET, so its reference in the docs is clearly a mistake. It looks like that whole paragraph was copy and pasted from the Java docs. My concern is that the code following the explanation might also be incorrect/copy and pasted from the Java docs.