29 lines
927 B
C#
29 lines
927 B
C#
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using NetClient.Abstractions;
|
|
|
|
namespace NetClient.Test.TestDoubles;
|
|
|
|
public sealed class TestJsonSerializer : IHttpContentSerializer
|
|
{
|
|
private readonly JsonSerializerOptions _options;
|
|
|
|
public TestJsonSerializer(JsonSerializerOptions? options = null)
|
|
{
|
|
_options = options ?? new JsonSerializerOptions(JsonSerializerDefaults.Web);
|
|
}
|
|
|
|
public string ContentType => "application/json";
|
|
|
|
public Task SerializeAsync<T>(Stream stream, T? value, CancellationToken cancellationToken = default)
|
|
{
|
|
return JsonSerializer.SerializeAsync(stream, value, _options, cancellationToken);
|
|
}
|
|
|
|
public Task<T?> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default)
|
|
{
|
|
return JsonSerializer.DeserializeAsync<T>(stream, _options, cancellationToken).AsTask();
|
|
}
|
|
} |