I need to pass information about custom exception types from the WebApi web service to the client. To do this, I'm trying to use the Json.Net + CustomContractResolver serializer to control what is sent to the client.
public class Data
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public Data()
{
Prop1 = "Hi";
Prop2 = "Bye";
}
}
public class CustomContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty property = base.CreateProperty(member, memberSerialization);
property.ShouldSerialize = instance => property.PropertyName == "Prop1";
return property;
}
}
class Program
{
static void Main(string[] args)
{
var settings = new JsonSerializerSettings
{
ContractResolver = new CustomContractResolver()
};
string json = JsonConvert.SerializeObject(new Data(), settings);
Console.WriteLine(json);
Console.ReadLine();
}
}
I tried to run this code using Json.Net 6.0.4 and 6.0.6. Used .Net Framework 4.5.0 and 4.5.2. But the problem is the same - when the data is inherited from Exception CustomContractResolver does not work (nuget configuration line: package id = "Newtonsoft.Json" version = "6.0.6" targetFramework = "net45" or package id = "Newtonsoft.Json" version = "6.0.6" targetFramework = "net452")
, UnitTest (xUnit.Net), PCL .Net 4.5.0 ( nuget: package id = "Newtonsoft.Json" version = "6.0.6" targetFramework = "portable-net45 + win + wpa81 + MonoAndroid10 + MonoTouch10" ).
- , , Json?