mycroforge/MicroForge.CLI/Extensions/ObjectStreamExtensions.cs
2024-04-21 23:56:27 +02:00

20 lines
638 B
C#

using System.Text.Json;
namespace MicroForge.CLI.Extensions;
public static class ObjectStreamExtensions
{
public static async Task<string> SerializeAsync(
this object @object,
JsonSerializerOptions? jsonSerializerOptions = null
)
{
using var stream = new MemoryStream();
using var reader = new StreamReader(stream);
var options = jsonSerializerOptions ?? Shared.DefaultJsonSerializerOptions.Default;
await JsonSerializer.SerializeAsync(stream, @object, options);
stream.Position = 0;
return await reader.ReadToEndAsync();
}
}