I would like to be able to build a solution using Microsoft ASP.NET Web API. I would like to be able able to have a complex object like 'Person' below which implements an interface 'IDisplayInfo'. When the Person is serialized, I would like all properties to be serialized normally, but when another object that only specifies the interface like the WorkOrder object is serialized, I would like only the properties on the interface to be serialized. I would like it to work with both XML and JSON. I tried overriding the DefaultContractResolver, but I'm having trouble understanding how this works.
Thank you for your help!
public interface IDisplayInfo
{
string Id { get; }
string Display { get; }
}
public class Person : IDisplayInfo
{
public string Id { get; set; }
public string Display { get { return FirstName + " " + LastName; } }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class WorkOrder
{
public string Title { get; set; }
public IDisplayInfo CreatedBy { get; set; }
}
Serialized WorkOrder should look like this: { Title: "test", CreatedBy: { Id: "1", Display: "Bob Fox" } }
Serialized Person should look like this: { Id: "1", Display: "Bob Fox", FirstName: "Bob", LastName: "Fox" }