Description
We have a serializer interface (https://github.com/FoundatioFx/Foundatio/blob/master/src/Foundatio/Serializer/ISerializer.cs#L6-L9) which takes a stream as the input and we have extensions that work from it. In our use cases the payloads are usually very small and fit inside normal buffer lengths. Now I totally understand async io and the benefits but we use in memory streams for our serialization and we don't want to do async serialization as it totally kills performance and really complicates some scenarios (esp in messaging handlers at scale). Can you add overloads for this? It would make it a little bit easier for this use case?
I propose that we add overloads to both System.Text.Json.JsonSerializer.Serialize
and System.Text.Json.JsonSerializer.Deserialize
that take streams. As @ahsonkhan provided the following code which sums up what needs to happen signature wise.
public static partial class JsonSerializer
{
// Existing APIs:
public static ValueTask<object> DeserializeAsync(Stream utf8Json,
Type returnType,
JsonSerializerOptions options = null,
CancellationToken cancellationToken = default);
public static ValueTask<TValue> DeserializeAsync<TValue>(Stream utf8Json,
JsonSerializerOptions options = null,
CancellationToken cancellationToken = default);
public static Task SerializeAsync(Stream utf8Json,
object value,
Type inputType,
JsonSerializerOptions options = null,
CancellationToken cancellationToken = default);
public static Task SerializeAsync<TValue>(Stream utf8Json,
TValue value,
JsonSerializerOptions options = null,
CancellationToken cancellationToken = default);
// Proposal to add following synchronous APIs:
public static object Deserialize(Stream utf8Json,
Type returnType,
JsonSerializerOptions options = null);
public static TValue Deserialize<TValue>(Stream utf8Json,
JsonSerializerOptions options = null);
public static void Serialize(Stream utf8Json,
object value,
Type inputType,
JsonSerializerOptions options = null);
public static void Serialize<TValue>(Stream utf8Json,
TValue value,
JsonSerializerOptions options = null);
}
Edit by @ahsonkhan: Just modified some indentation/spacing so the API surface aligns better.