I am writing a web api using my existing model classes and model services which are being used by many other applications too which needs the model to be fully exposed in serialization. Can I use the same model object for web api and expose only few fields.
I have tried using JsonContractResolver, but it fails to serialize child properties in a nested class.
/*Model class*/
public class A
{
public int A1 { get; set; }
public DateTime A2 { get; set; }
public List<B> A3 { get; set; }
}
public class B
{
public string B1 { get; set; }
public string B2 { get; set; }
public string B3 { get; set; }
}
Expected output: When a web app method gets the members of class A, it should return the following JSON:
{"A1":1,"A2":"2017-02-10","A3":[{"B1":"test1","B2":"test2","B3":"test3"}]}
When the web API gets the member of class A, it should return:
{"A1":1,"A3":[{"B1":"test1"}]}
How can I adjust my code to achieve this result?