2017-04-06 69 views
1

我注意到序列化中的怪異行爲。 雖然,我有設置JSon.Net特定JSonConverter問題,必須忽略空數組

var SerializerSettings = new JsonSerializerSettings() { 
        NullValueHandling = NullValueHandling.Ignore,  DefaultValueHandling= DefaultValueHandling.Ignore} 
SerializerSettings.Converters.Add(new JsonArrayToNullConverter()); 

    var val = new company() 
    { 
     name = "Bobo Company Renamed" 
    } 
var str = JsonConvert.SerializeObject(val, SerializerSettings); 

其結果將是: { 「DOCUMENT_TYPE」:2, 「位置」:空...

沒有自定義轉換器,這將是 {「document_type」:2,「locations」:[],...

你明白了嗎?

但是,因爲它變成零,它-should-聽NullValueHandling = NullValueHandling.Ignore 但顯然,牛頓,着眼於對象的值被序列化,而不是在發出writer.WriteNull();

:(

任何解決方法嗎?我已經花了這一點。感謝幾個小時!

using Newtonsoft.Json; 

using System; 
using System.Collections; 
using System.Collections.Generic; 

namespace Company.Model.TypeConverters 
{ 
    /// <summary> 
    /// undo's the forced array creation because OData needs it, but for PATCH, we want no [] empty collections, it is considered to be a 
    /// </summary> 
    public class JsonArrayToNullConverter : JsonConverter 
    { 

     public override bool CanConvert(Type objectType) 
     { 
      var canConvert = objectType.IsArray || (objectType.IsGenericType && objectType.GetGenericTypeDefinition().IsAssignableFrom(typeof(IEnumerable<>))); 
      return canConvert; 
     } 
     public override bool CanRead 
     { 
      get 
      { 
       return false; 
      } 
     } 
     public override bool CanWrite 
     { 
      get 
      { 
       return true; 
      } 
     } 
     public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
     { 
      throw new NotImplementedException(); 
     } 

     public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
     { 
      var enumerator = (IEnumerable)value; 

      if (enumerator != null && enumerator.GetEnumerator().MoveNext() == false) 
      { 
       writer.WriteNull(); 
       //value = null; 
       return; 
      } 
      serializer.Serialize(writer, value); 
     } 
    } 
} 

回答

0

的解決方案似乎是使用contractresolver,並設置在JsonSerializerSettings。

public class NullToEmptyListResolver : DefaultContractResolver 
{ 
    protected override IValueProvider CreateMemberValueProvider(MemberInfo member) 
    { 
     IValueProvider provider = base.CreateMemberValueProvider(member); 

     if (member.MemberType == MemberTypes.Property) 
     { 
      Type propType = ((PropertyInfo)member).PropertyType; 
      if (propType.IsArray || (propType.IsGenericType && 
       propType.GetGenericTypeDefinition().IsAssignableFrom(typeof(IEnumerable<>)))) 
      { 
       return new EmptyListValueProvider(provider, propType); 
      } 
     } 

     return provider; 
    } 

    public class EmptyListValueProvider : IValueProvider 
    { 
     private readonly IValueProvider innerProvider; 

     public EmptyListValueProvider(IValueProvider innerProvider, Type listType) 
     { 
      this.innerProvider = innerProvider; 
     } 

     public void SetValue(object target, object value) 
     { 
      throw new NotImplementedException(); 
     } 

     public object GetValue(object target) 
     { 
      var val = innerProvider.GetValue(target) ; 
      if (val == null) return null; 
      var enumerator = (IEnumerable)val; 
      return enumerator.GetEnumerator().MoveNext() == false ? null : val; 
     } 
    } 
+0

在你的'SetValue()'方法中,你應該調用'innerProvider.SetValue(target,value);'而不是拋出一個異常,並且考慮[處置枚舉器](https://stackoverflow.com/q/232558/37441 82)使用後。例如。 [這個答案](https://stackoverflow.com/a/24706061/3744182)隱含。 – dbc

+0

謝謝,但爲了我的目的,它不能寫,只能讀。 –