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 lost the property of derived class after serialize. #51906

Closed
Mutuduxf opened this issue Apr 27, 2021 · 3 comments
Closed

System.Text.Json lost the property of derived class after serialize. #51906

Mutuduxf opened this issue Apr 27, 2021 · 3 comments
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner

Comments

@Mutuduxf
Copy link

Target framework 5.0,here is the demo.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.Json;

namespace SystemTextJsonTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var apple = new Apple
            {
                Id = Guid.NewGuid(),
                Bananas = new List<Banana>
                {
                    new Banana0{Id = Guid.NewGuid(),Value = 1},
                    new Banana1{Id = Guid.NewGuid(),Value = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)}
                }
            };
            //The property "Value" of the derived class is lost
            //{"Id":"fead1a5a-87a2-41c2-a1ba-c48fda1da67a","Bananas":[{"Id":"8fe8ff02-4bc8-42db-8c44-e014ae522d00"},{"Id":"236edd74-8cdd-40f9-87e0-70fe0a2adba4"}]}
            var json = JsonSerializer.Serialize(apple);
            Console.WriteLine(json);
            Console.ReadLine();
        }
    }

    public class Apple
    {
        public Guid Id { get; set; }
        public List<Banana> Bananas { get; set; }
    }

    public class Banana
    {
        public Guid Id { get; set; }
    }

    public class Banana0 : Banana
    {
        public int Value { get; set; }
    }

    public class Banana1 : Banana
    {
        public string Value { get; set; }
    }
}
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Apr 27, 2021
@ghost
Copy link

ghost commented Apr 27, 2021

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

Issue Details

Target framework 5.0,here is the demo.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.Json;

namespace SystemTextJsonTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var apple = new Apple
            {
                Id = Guid.NewGuid(),
                Bananas = new List<Banana>
                {
                    new Banana0{Id = Guid.NewGuid(),Value = 1},
                    new Banana1{Id = Guid.NewGuid(),Value = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)}
                }
            };
            //The property "Value" of the derived class is lost
            //{"Id":"fead1a5a-87a2-41c2-a1ba-c48fda1da67a","Bananas":[{"Id":"8fe8ff02-4bc8-42db-8c44-e014ae522d00"},{"Id":"236edd74-8cdd-40f9-87e0-70fe0a2adba4"}]}
            var json = JsonSerializer.Serialize(apple);
            Console.WriteLine(json);
            Console.ReadLine();
        }
    }

    public class Apple
    {
        public Guid Id { get; set; }
        public List<Banana> Bananas { get; set; }
    }

    public class Banana
    {
        public Guid Id { get; set; }
    }

    public class Banana0 : Banana
    {
        public int Value { get; set; }
    }

    public class Banana1 : Banana
    {
        public string Value { get; set; }
    }
}
Author: Mutuduxf
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@huoyaoyuan
Copy link
Member

This is the expected behaviour. Unless the variable type is object, the json serializer will use variable type, not object type for serialization.

@eiriktsarpalis
Copy link
Member

As @huoyaoyuan mentioned, this behaviour is by design. We are working on adding opt-in behaviour that should permit polymorphic serialization in future releases.

Duplicate of #45189.

@ghost ghost locked as resolved and limited conversation to collaborators May 27, 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