I will not go into everything that I tried ...
This is roughly what I'm trying to accomplish.
public interface IClientDTO {
string FirstName { get; }
string LastName { get; }
}
public class DTO : IClientDTO {
public string FirstName { get; set; }
public string LastName { get; set; }
public string SSN { get; set; }
}
class Program {
static void Main(string[] args) {
DTO dto = new DTO() { FirstName = "John", LastName = "Doe", SSN = "111001111" };
IClientDTO clientDTO = dto;
string sDTO = JsonConvert.SerializeObject(dto);
string sClientDTO = JsonConvert.SerializeObject(clientDTO);
Debug.WriteLine(sDTO);
Debug.WriteLine(sClientDTO);
}
}
I want my conclusion to look like this ...
{"PLA": "111001111", "FirstName": "John", "LastName": "Doe"}
{"FirstName": "John", "LastName": "Doe"}
I did not think it would be difficult, but since the Serializer always defines the type as DTO, I always get the SSN value even in cases where I do not want to.
This, of course, is just a small test. Suppose I have a WebAPI application mocking something like this.
public class AdminController : ApiController {
public DTO Get() { return new Model().DTO); }
}
public class ClientController : ApiController {
public IClientDTO Get() { return new Model().DTO); }
}
I would like the Serializer to serialize only those elements that are defined in the return type.