0

更新:由於答案,我發現舊標題不相關,並從'如何實現自定義JsonConverter使用JsonConvert '到目前的狀況。如何綁定一個JSON到一個接口集合,反之亦然使用MVC.Net


我在很緊,時間限制的情況下,我必須做的標題說什麼......

我嘗試過很多辦法,直到早上,模型不綁定的方法類似於MVC中的FormCollection返回空值等等,因爲我向我的模型添加了一個接口列表,所以我需要處理它,...早上客戶報告錯誤,他們的系統處於壓力之下,所以我不能幫助它,但工作,直到它修復。

這是我現在的代碼:

會發生什麼?一旦作家的方法返回服務器返回500內部服務器錯誤...

自然他們這樣做使用序列化對象,但我不知道如何使用序列化器做這個定製,所以我真的需要調用這種方法接受序列化器設置。

public class CollectionInterfaceConverterForModels<TInterface> : JsonConverter 
{ 
    private readonly TypeNameSerializationBinder Binder; 

    public CollectionInterfaceConverterForModels() 
    { 
     Binder = new TypeNameSerializationBinder("Cartable.Models.{0}, Cartable"); 
    } 

    public override bool CanConvert(Type objectType) 
    { 
     return (objectType == typeof(IList<TInterface>)); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     //IList<TInterface> items = serializer.Deserialize<List<TInterface>>(reader).Cast<TInterface>().ToList(); 
     //return items; 
     var json = existingValue.ToString(); 
     if (json == null) 
      json = ""; 

     return JsonConvert.DeserializeObject<List<TInterface>>(json, 
      new JsonSerializerSettings 
      { 
       TypeNameHandling = TypeNameHandling.Auto, 
       Binder = Binder 
      }); 
    } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     //serializer.Serialize(writer, value, typeof(IList<T>)); 
     string serializeObject = JsonConvert.SerializeObject(value, Formatting.Indented, new JsonSerializerSettings 
     { 
      TypeNameHandling = TypeNameHandling.Auto, 
      Binder = Binder 
     }); 
     writer.WriteRaw(serializeObject); 
     writer.Flush(); 
    } 
} 

型號樣品:

public class IncomingFaxesVM 
{ 
    public long Code { get; set; } 
. 
. 
. 

    [JsonConverter(typeof(CollectionInterfaceConverterForModels<IMetadata>))] 
    public List<IMetadata> Metadata { get; set; } 
} 

回答

0

然而,我不能做什麼,我問過,但我解決了用另一種方式

所以,如果你找到答案的問題,我標記你的,其他明智的我將標題改爲界面綁定,以便其他用戶可以輕鬆找到它,這是我解決問題的答案。

public class CollectionInterfaceConverterForModels<TInterface> : JsonConverter 
{ 
    private readonly TypeNameSerializationBinder Binder; 

    public CollectionInterfaceConverterForModels() 
    { 
     Binder = new TypeNameSerializationBinder("Cartable.Models.{0}, Cartable"); 
    } 

    public override bool CanConvert(Type objectType) 
    { 
     return (objectType == typeof(IList<TInterface>)); 
    } 
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
     var currentBinder = serializer.Binder; 
     var currentTypeNameHandling = serializer.TypeNameHandling; 

     serializer.TypeNameHandling = TypeNameHandling.Auto; 
     serializer.Binder = Binder; 

     IList<TInterface> items = serializer.Deserialize<List<TInterface>>(reader).ToList(); 

     serializer.TypeNameHandling = currentTypeNameHandling; 
     serializer.Binder = currentBinder; 

     return items; 
    } 

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     //The reason store these, is to leave system unchanged, in case it customized on general section by another programmer 
     var currentBinder = serializer.Binder; 
     var currentTypeNameHandling = serializer.TypeNameHandling; 

     serializer.TypeNameHandling=TypeNameHandling.Auto; 
     serializer.Binder = Binder; 

     serializer.Serialize(writer, value, typeof(List<TInterface>)); 

     serializer.TypeNameHandling = currentTypeNameHandling; 
     serializer.Binder = currentBinder; 
    } 
} 
相關問題