2

is there someone who can help me with the serialization of IList<IShows>. I managed to serialize it with help of JsonConvert.SerializeObject but ... Let's say I have classes like:

public class Show : IShow
{
    public string Name { get; set; }
    public IList<IEpisode> Episodes { get; set; }
    public int Count { get; set; }       
}
public class Episode : IEpisode
{
    public string Name { get; set; }
    public DateTime Date { get; set; }
    public IList<ILink> Links { get; set; }
}
public class Link : ILink
{
    public string URL { get; set; }
    public string Name { get; set; }
    public string Quality { get; set; }
}

I'm getting something like:

[
  {
    "Name": "Supernatural",
    "Episodes": [
      {
        "Name": "There's Something About Mary",
        "Date": "2017-05-12T02:20:05+01:00",
        "Links": [
          {
            "URL": "url",
            "Name": "HDTV",
            "Quality": "480p"
          },
          {
            "URL": "url",
            "Name": "HDTV",
            "Quality": "720p"
          }
        ]
      }
    ]
  }
]

but I would like to add additional tag before each show (show) and another tag (shows) before all shows.

Does someone know how to do it?

1 Answer 1

2

That´s probably not the way JSON was meant to be, the structure looks fine to me.

But if you really need to, you could do something like this:

public class Root {
    public IList<ShowWrapper> Shows { get; set;}
}
public class ShowWrapper { //silly name though ;)
    public IShow Show { get; set;}
}

and serialize instance of Root instead of

IList<IShow>. 

This should produce the desired structure, but feels very unnatural.

What serializer are you using btw. Maybe someone out there knows a less "manual" way to achieve this.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm new to JSON so it might not be the best way. I'll take your advice and I'll keep that JSON. BTW I'm using JsonConvert.SerializeObject. Thank you for your answer.
@OndřejDlesk As a word of advice, unless you're explicitly trying to match a specified JSON format, or you're dealing with third party's format, you shouldn't care what the JSON looks like. As long as you design your class structure logically (which you have here), that's all you should worry about.
good advice @Rob! @OndřejDlesk: Would help future reader when you edit the question and leave the hint about JsonConvert there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.