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

System.Text.Json serialize derived class property from a list #46846

Closed
Anduin2017 opened this issue Jan 12, 2021 · 2 comments
Closed

System.Text.Json serialize derived class property from a list #46846

Anduin2017 opened this issue Jan 12, 2021 · 2 comments

Comments

@Anduin2017
Copy link
Member

I'm migrating a project from Newtonsoft.Json to System.Text.Json in .NET 5.

I have class:

abstract class Car
{
    public string Name { get; set; } = "Default Car Name";
}

class Tesla : Car
{
    public string TeslaEngineName { get; set; } = "My Tesla Engine";
}

I tried:

var cars = new List<Car> 
{ 
    new Tesla(),
    new Tesla(),
    new Tesla()
};

var json = JsonSerializer.Serialize(cars);

Console.WriteLine(json);

The output is:

[
  {"Name":"Default Car Name"},
  {"Name":"Default Car Name"},
  {"Name":"Default Car Name"}
]

Which lost my property: TeslaEngineName.

So how can I serialize derived an object with all properties?

Thanks!

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Jan 12, 2021
@Joe4evr
Copy link
Contributor

Joe4evr commented Jan 12, 2021

#45189

@layomia
Copy link
Contributor

layomia commented Feb 3, 2021

Will be addressed by #45189.

@layomia layomia closed this as completed Feb 3, 2021
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Feb 3, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Mar 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants