Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] System.Text.Json Serialize An Empty interface will get a "{}" #54661

Closed
egmkang opened this issue Jun 24, 2021 · 3 comments
Closed
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner

Comments

@egmkang
Copy link

egmkang commented Jun 24, 2021

using System;
using NewtonsoftJson = Newtonsoft.Json.JsonConvert;
using SystemJson = System.Text.Json.JsonSerializer;

public interface Empty { }

public class ObjectA : Empty
{
    public int id { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Empty a = new ObjectA()
        {
            id = 1,
        };

        Console.WriteLine("System.Text.Json, Serialize<Empty> => {0}", SystemJson.Serialize(a));
        Console.WriteLine("System.Text.Json, Serialize<Object> => {0}", SystemJson.Serialize(a as object));
        Console.WriteLine("Newtonsoft.Json, Serialize<Empty> => {0}", NewtonsoftJson.SerializeObject(a));
        Console.WriteLine("Newtonsoft.Json, Serialize<Object> => {0}", NewtonsoftJson.SerializeObject(a as object));
    }
}

and output is:

System.Text.Json, Serialize<Empty> => {}
System.Text.Json, Serialize<Object> => {"id":1}
Newtonsoft.Json, Serialize<Empty> => {"id":1}
Newtonsoft.Json, Serialize<Object> => {"id":1}

Serialize<Empty> will return {}, is this the correct behavior?

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Jun 24, 2021
@ghost
Copy link

ghost commented Jun 24, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details
using System;
using NewtonsoftJson = Newtonsoft.Json.JsonConvert;
using SystemJson = System.Text.Json.JsonSerializer;

public interface Empty { }

public class ObjectA : Empty
{
    public int id { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Empty a = new ObjectA()
        {
            id = 1,
        };

        Console.WriteLine("System.Text.Json, Serialize<Empty> => {0}", SystemJson.Serialize(a));
        Console.WriteLine("System.Text.Json, Serialize<Object> => {0}", SystemJson.Serialize(a as object));
        Console.WriteLine("Newtonsoft.Json, Serialize<Empty> => {0}", NewtonsoftJson.SerializeObject(a));
        Console.WriteLine("Newtonsoft.Json, Serialize<Object> => {0}", NewtonsoftJson.SerializeObject(a as object));
    }
}

and output is:

System.Text.Json, Serialize<Empty> => {}
System.Text.Json, Serialize<Object> => {"id":1}
Newtonsoft.Json, Serialize<Empty> => {"id":1}
Newtonsoft.Json, Serialize<Object> => {"id":1}

Serialize<Empty> will return {}, is this the correct behavior?

Author: egmkang
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@Joe4evr
Copy link
Contributor

Joe4evr commented Jun 24, 2021

Yes, this is expected at the moment. See #45189.

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Jun 28, 2021

Support for polymorphic serialization is tracked by #30083. Note that the feature has been cut from .NET 6.

Duplicate of #30083

@ghost ghost locked as resolved and limited conversation to collaborators Jul 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants