Hello,
If you want to write this correctly you should mark the method async, here is a base framework. The main reason for using async is to keep things responsive, otherwise stick with synchronous code.
Requires System.Text.Json reference as this is not Newtonsoft library.
public class Demo
{
public static async Task<string> WorkTask(object obj)
{
string json = string.Empty;
await using var stream = new MemoryStream();
await JsonSerializer.SerializeAsync(stream, obj, obj.GetType());
stream.Position = 0;
using var reader = new StreamReader(stream);
return await reader.ReadToEndAsync();
}
}