8

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?

1 Answer 1

21

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

Sign up to request clarification or add additional context in comments.

1 Comment

Info: "using var reader = new StreamReader(stream)" requieres C# 8.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.