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 ignores JsonPropertyName on BaseClass #31160

Closed
robertmuehsig opened this issue Oct 13, 2019 · 3 comments
Closed

System.Text.Json ignores JsonPropertyName on BaseClass #31160

robertmuehsig opened this issue Oct 13, 2019 · 3 comments

Comments

@robertmuehsig
Copy link

I'm not sure if this is a bug or by design, but while moving from JSON.NET to System.Text.Json I discovered the following behavior:

"JsonPropertyNames" are ignored on base classes.

    class Program
    {
        static void Main(string[] args)
        {
            RealClass test = new RealClass();

            // output will be "{"Link":"Foo"}" 
            // and not {"_LinkFromBaseClass":"Foo"}

            Console.WriteLine(JsonSerializer.Serialize(test));


            Console.WriteLine("Hello World!");
        }
    }

    public abstract class SomeBaseClass
    {
        [JsonPropertyName("_LinkFromBaseClass")]
        public virtual string Link { get;  }
    }

    public class RealClass : SomeBaseClass
    {
        public RealClass()
        {
        }

        public override string Link { get { return "Foo"; } }
    }

Is this "by design"? Currently we just put the "JsonPropertyName" attribute on all implementations for the baseclass, but it's quite easy to miss.
I also checked the JsonSerializerOptions, but didn't find anything useful, but maybe I miss something.

@ericstj
Copy link
Member

ericstj commented Nov 4, 2019

We can consider this when adding support for polymorphic serialization #29937

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 5.0 milestone Feb 1, 2020
@layomia layomia modified the milestones: 5.0.0, Future Jun 20, 2020
@eiriktsarpalis
Copy link
Member

Duplicate of #51165

@eiriktsarpalis eiriktsarpalis marked this as a duplicate of #51165 Oct 15, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Nov 15, 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

6 participants