Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c#/.net core3.0 System.Text.Json - JsonSerializer.SerializeAsync?

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;
        }
    }
}

run output

{"Name":"Test","Age":18} //ok

{} //why?

the same code, but to output different results, why?

like image 259
ruson Avatar asked Dec 02 '22 10:12

ruson


1 Answers

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());; or
  • await JsonSerializer.SerializeAsync<T>(stream, obj);

Note that this behavior is different from Json.NET because JsonConvert's SerializeObject uses runtime type.


Test

Code

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();
        }
    }

Output

{"Name":"Test","Age":18}

UPDATE

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>.

like image 85
tymtam Avatar answered Jan 07 '23 12:01

tymtam