我试图通过EntitySetController提供OData时使用默认的.Net序列化方法。 使用时:
[DataContract]
public class TestClassA
{
public int Id { get; set; }
public string Stam { get; set; }
[DataMember]
public TestClassB TestClassB { get; set; }
}
public class TestClassB
{
public int Ids { get; set; }
public string Name { get; set; }
}
调用GET的结果
{
"odata.metadata":"http://**MyHost**/odata/$metadata#TestClassA","value":[
{
"TestClassB":{
"Ids":110,"Name":"Bla"
}
}
]
}
适用于DataContract属性。
但是,在尝试使用[JsonConverter]属性时:
public class TestClassA
{
public int Id { get; set; }
public string Stam { get; set; }
[JsonConverter(typeof(MyFormatter))]
[DataMember(EmitDefaultValue = false)]
public TestClassB TestClassB { get; set; }
}
public class TestClassB
{
public int Ids { get; set; }
public string Name { get; set; }
}
public class MyFormatter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
}
完全忽略了JsonConverter。 (永远不会创建MyFormatter类)。 知道如何让它发挥作用吗?