Я не знаю, является ли это лучшим решением.
Код для десериализации
var item = JsonConvert.DeserializeObject<ItemType>(responseString);
switch (item.Type)
{
case Type.Job:
return JsonConvert.DeserializeObject<Job>(responseString);
case Type.Story:
return JsonConvert.DeserializeObject<Story>(responseString);
case Type.Comment:
return JsonConvert.DeserializeObject<Comment>(responseString);
case Type.Poll:
return JsonConvert.DeserializeObject<Poll>(responseString);
case Type.PollOpt:
return JsonConvert.DeserializeObject<PollOpt>(responseString);
default:
throw new ArgumentOutOfRangeException();
}
Все классы предметов
public class ItemType
{
public Type Type { get; set; }
}
public enum Type
{
None,
Job,
Story,
Comment,
Poll,
PollOpt,
}
public interface IItem
{
}
public class Story : IItem
{
public string By { get; set; }
public long Descendants { get; set; }
public long Id { get; set; }
public List<long> Kids { get; set; }
public long Score { get; set; }
public long Time { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public Uri Url { get; set; }
}
public class Comment : IItem
{
public string By { get; set; }
public long Id { get; set; }
public List<long> Kids { get; set; }
public long Parent { get; set; }
public string Text { get; set; }
public long Time { get; set; }
public string Type { get; set; }
}
public class Job : IItem
{
public string By { get; set; }
public long Id { get; set; }
public long Score { get; set; }
public string Text { get; set; }
public long Time { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public string Url { get; set; }
}
public class Poll : IItem
{
public string By { get; set; }
public long Descendants { get; set; }
public long Id { get; set; }
public List<long> Kids { get; set; }
public List<long> Parts { get; set; }
public long Score { get; set; }
public string Text { get; set; }
public long Time { get; set; }
public string Title { get; set; }
public string Type { get; set; }
}
public class PollOpt : IItem
{
public string By { get; set; }
public long Id { get; set; }
public long Poll { get; set; }
public long Score { get; set; }
public string Text { get; set; }
public long Time { get; set; }
public string Type { get; set; }
}