0

I have three classes: Hotel (implements IHotel) , Room (implements IRoom , which is inherited from IRoomDetails and IBookedRoom), ClientModel (implement IClientModel).

public interface IRoomDetails
    {
        string Number { get; set; }
        float PricePerDay { get; set; }
    }

public interface IBookedRoom
    {
        bool IsAvailable { get; set; }
        IClientModel Reservator { get; set; }
        string TermFrom { get; set; }
        string TermTo { get; set; }
        string Term { get; }
    }
public interface IRoom : IRoomDetails , IBookedRoom
    {

    }
public class Room : ObservableObject, IRoom
    {
       //Realization here
    }

If i try to serialize List<IRoomDetails> all works perfect, as well as List<IBookedRoom>, BUT if i create interface IRoom , which inherits from both of them and try to serialize List<IRoom>:

 {
    "Adress": "Pokrovska , 1B",
    "Name": "Victoria",
    "TotalRooms": 2,
    "AvailableRooms": 2,
    "Rooms": [
      {},
      {}
    ]
  }

As we see data is not serialized. (List of rooms is not empty) . Any suggestions?

dbc
  • 104,963
  • 20
  • 228
  • 340
Majestr32
  • 3
  • 1
  • 2
    Using Newtonsoft.Json solved problem – Majestr32 Nov 24 '20 at 10:19
  • Probably something to do with `System.Text.Json`'s very restricted support for polymorphism while serializing. Polymorphic subtype properties are only serialized *when the property is declared as an `object`*. See [Why does System.Text Json Serialiser not serialise this generic property but Json.NET does?](https://stackoverflow.com/a/62033671/3744182) for confirmation, and [Is polymorphic deserialization possible in System.Text.Json?](https://stackoverflow.com/q/58074304/3744182) for workarounds. Switching back to Json.NET perfectly reasonable here. – dbc Nov 24 '20 at 14:26
  • If you need further help, might you [edit] your question to include a full [mcve]? As it is we only have some of the classes that demonstrate the problem. – dbc Nov 24 '20 at 14:27
  • I have already tested System.Text.Json and found out that it has very restricted support for polymorphism as you said before , so method Serialize() doesn`t work here properly. Acting in the same manner , but with Newtonsoft.Json serializer solved problem from the first time. I guess it is one of the namespace problems rather than mine. – Majestr32 Nov 24 '20 at 19:10

0 Answers0