I create test console app,(use net core3.0), and code as below:
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
namespace TestConsoleApp1
{
    class Program
    {
        async static Task Main(string[] args)
        {
            var obj = new { Name = "Test", Age = 18 };
            string json = string.Empty;
            using (var stream = new MemoryStream())
            {
                await JsonSerializer.SerializeAsync(stream, obj);
                stream.Position = 0;
                using var reader = new StreamReader(stream);
                json = await reader.ReadToEndAsync();
            }
            Console.WriteLine(json);    //output {"Name":"Test","Age":18}, is ok
            Console.WriteLine(await GetJson(obj));  //output {}, why?
            Console.ReadLine();
        }
        private static async Task<string> GetJson(object obj)
        {
            string json = string.Empty;
            using (var stream = new MemoryStream())
            {
                await JsonSerializer.SerializeAsync(stream, obj);
                stream.Position = 0;
                using var reader = new StreamReader(stream);
                json = await reader.ReadToEndAsync();
            }
            return json;
        }
    }
}
{"Name":"Test","Age":18} //ok
the same code, but to output different results, why?
This is because System.Text.Json.Serializer.SerializeAsync is a generic method and it uses the generic parameter, not the runtime type.
You need to call either:
await JsonSerializer.SerializeAsync(stream, obj, obj.GetType());; orawait JsonSerializer.SerializeAsync<T>(stream, obj);Note that this behavior is different from Json.NET because JsonConvert's SerializeObject uses runtime type.
async static Task Main(string[] args)
    {
        var obj = new { Name = "Test", Age = 18 };
        Console.WriteLine(await GetJson(obj));
    }
    private static async Task<string> GetJson(object obj)
    {
        string json = string.Empty;
        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();
        }
    }
{"Name":"Test","Age":18}
There is more to it, because the non-async version, Serialise() does not adhere to the same rules and desrialises the run-time type when explicitly told to serialize <object>.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With