locked
How to serializer the derived interface by System.Text.Json RRS feed

  • Question

  • How to serializer the derived interface by System.Text.Json

    I just want to replace my serialize moudel from Newtonsoft to System.Text.Json in .net core 3.1.0.
    But the result of System.Text.Json is different from Newtonsoft.
    As the sample:
    class Program
        {
            static void Main(string[] args)
            {
                People p = new People()
                {
                    course = new Course(),
                    channel = new Channel(),
                };
                p.course.CourseName = "English";
                p.course.From = "12:00";
                p.channel.Age = 15;
                p.channel.Name = "Lily";
                
                var textjson = System.Text.Json.JsonSerializer.Serialize(p);
                var newtonjson = Newtonsoft.Json.JsonConvert.SerializeObject(p);
                    
                Console.WriteLine($"System.Text.Json:{textjson}");
                Console.WriteLine($"Newtonsoft.Json:{newtonjson}");
    
                Console.ReadLine();
            }       
        }
    
        public class People
        {
            public IChannel channel {set; get;}
    
            public Course course { set; get; }
        
        }
    
        public class Channel : IChannel
        { 
            public int Age { set; get; }
            public string Name { set; get; }
    
        }
    
        public interface IChannel : ICommon
        {
        }
    
        public interface ICommon
        {
            public int Age { set; get; }
            public string Name { set; get; }
        }
    
        public class Course : Schedule
        {
            public string CourseName { set; get; }
    
        }
    
        public class Schedule
        {
            public string From { set; get; }
        
        }
    OUTPUT:
    System.Text.Json:{"channel":{},"course":{"CourseName":"English","From":"12:00"}}
    Newtonsoft.Json:{"channel":{"Age":15,"Name":"Lily"},"course":{"CourseName":"English","From":"12:00"}}

    The derived interface's properties is not serialized as normal.
    I'm not sure what's wrong with the sample. Could someone help me to check this? 

    Monday, December 16, 2019 9:26 AM

Answers

  • Hi campsun, 

    Thank you for posting here.

    According to your question, I make a test on my side and reproduce your problem.

    You can add properties in ‘IChannel’ interface to resolve this issue.

        public interface IChannel : ICommon
        {
            public int Age { set; get; }
            public string Name { set; get; }
        }
    

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by campsun Wednesday, December 25, 2019 5:58 AM
    Tuesday, December 17, 2019 7:40 AM
  • If possible, apply this temporary workaround:

     

    public class People

    {

           [JsonIgnore]

           public IChannel channel { set; get; }

     

           public Course course { set; get; }

     

     

           [JsonPropertyName( nameof( channel ) )]

           public object channelWorkaround

           {

                  get

                  {

                         return channel;

                  }

                  set

                  {

                         channel = (IChannel)value;

                  }

           }

    }

     

     

    Review it in future version of serialiser.

    See also: https://github.com/dotnet/corefx/issues/38650.

    • Edited by Viorel_MVP Thursday, December 19, 2019 6:37 AM
    • Marked as answer by campsun Wednesday, December 25, 2019 5:58 AM
    Thursday, December 19, 2019 6:30 AM

All replies

  • Hi campsun, 

    Thank you for posting here.

    According to your question, I make a test on my side and reproduce your problem.

    You can add properties in ‘IChannel’ interface to resolve this issue.

        public interface IChannel : ICommon
        {
            public int Age { set; get; }
            public string Name { set; get; }
        }
    

    Result:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by campsun Wednesday, December 25, 2019 5:58 AM
    Tuesday, December 17, 2019 7:40 AM
  • Hi Xingyu Zhao,

    Thank for your reply.

    The sample is part of the whole solution.

    And more deep derived interface with solution, so I cannot add all the proprerties to the top level interface.

    Is there any other to do to solve it as "MaxDepth" in JsonSerializerOptions through it is not working.




    • Edited by campsun Wednesday, December 25, 2019 5:59 AM
    Thursday, December 19, 2019 2:03 AM
  • If possible, apply this temporary workaround:

     

    public class People

    {

           [JsonIgnore]

           public IChannel channel { set; get; }

     

           public Course course { set; get; }

     

     

           [JsonPropertyName( nameof( channel ) )]

           public object channelWorkaround

           {

                  get

                  {

                         return channel;

                  }

                  set

                  {

                         channel = (IChannel)value;

                  }

           }

    }

     

     

    Review it in future version of serialiser.

    See also: https://github.com/dotnet/corefx/issues/38650.

    • Edited by Viorel_MVP Thursday, December 19, 2019 6:37 AM
    • Marked as answer by campsun Wednesday, December 25, 2019 5:58 AM
    Thursday, December 19, 2019 6:30 AM
  • Hi Viorel,

    It works well.

    But I decided to go with Newtonsoft.Json until .Net 5 resolve it.

    Many thanks.


    Wednesday, December 25, 2019 5:58 AM