2014-09-10 58 views
0

這是一個奇怪的情況,但這是一種我們用來處理SQL後端的模式,當我們不想將數據分解爲單獨的桌子。在反序列化過程中設置值時,如何防止Newtonsoft.JSON重複使用默認值

這裏的問題是,關於反序列化,MyList有兩個項目,而不是一個。問題的根源在於當解串器到達MyList屬性時,它將重新使用現有值而不是重置屬性。

下面是代碼:

public class Jason 
{ 
    public object FromJSON(string json, Type t) 
    { 
     JsonSerializerSettings s = new JsonSerializerSettings(); 
     s.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); 
     s.NullValueHandling = NullValueHandling.Ignore; 
     return Newtonsoft.Json.JsonConvert.DeserializeObject(json, t, s); 
    } 

    public string ToJSON(object o) 
    { 
     JsonSerializerSettings s = new JsonSerializerSettings(); 
     s.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); 
     s.NullValueHandling = NullValueHandling.Ignore; 
     return Newtonsoft.Json.JsonConvert.SerializeObject(o, s); 
    } 

    public string MyListString 
    { 
     get { return ToJSON(_MyList); } 
     set { _MyList = (List<string>)FromJSON(value, typeof(List<string>)); } 
    } 

    private List<string> _MyList = new List<string>(); 
    public List<string> MyList 
    { 
     get { return _MyList; } 
     set { _MyList = value; } 
    } 

} 

[TestFixture] 
public class JasonTests : TestFixtureBase 
{ 
    [Test] 
    public void Jason() 
    { 
     Jason j = new Jason(); 
     j.MyList.Add("hello"); 

     string json = j.ToJSON(j); 

     Jason j2 = (Jason)j.FromJSON(json, typeof(Jason)); 

     Assert.AreEqual(j.MyList.Count, j2.MyList.Count); 
    } 
} 
+0

可能重複http://stackoverflow.com/questions/25553459/property-getter-invoked-during-deserialization-instead-的制定者) – 2014-09-11 01:54:26

回答

0

好,檢查源代碼,並試圖解決什麼我認爲是一個錯誤,那麼谷歌搜索四周,看看是否有人有這個問題,它發生,我認爲後可能會有一個設置,果然,有。這裏是固定的代碼:

public class Jason 
{ 
    public object FromJSON(string json, Type t) 
    { 
     JsonSerializerSettings s = new JsonSerializerSettings(); 
     s.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); 
     s.NullValueHandling = NullValueHandling.Ignore; 
     s.ObjectCreationHandling = ObjectCreationHandling.Replace; // without this, you end up with duplicates. 
     return Newtonsoft.Json.JsonConvert.DeserializeObject(json, t, s); 
    } 

    public string ToJSON(object o) 
    { 
     JsonSerializerSettings s = new JsonSerializerSettings(); 
     s.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); 
     s.NullValueHandling = NullValueHandling.Ignore; 
     s.ObjectCreationHandling = ObjectCreationHandling.Replace; // without this, you end up with duplicates. 
     return Newtonsoft.Json.JsonConvert.SerializeObject(o, s); 
    } 

    public string MyListString 
    { 
     get { return ToJSON(_MyList); } 
     set { _MyList = (List<string>)FromJSON(value, typeof(List<string>)); } 
    } 

    private List<string> _MyList = new List<string>(); 
    public List<string> MyList 
    { 
     get { return _MyList; } 
     set { _MyList = value; } 
    } 
} 

[TestFixture] 
public class JasonTests : TestFixtureBase 
{ 

    [Test] 
    public void Jason() 
    { 
     Jason j = new Jason(); 
     j.MyList.Add("hello"); 

     string json = j.ToJSON(j); 

     Jason j2 = (Jason)j.FromJSON(json, typeof(Jason)); 

     Assert.AreEqual(j.MyList.Count, j2.MyList.Count); 

    } 
} 
[屬性吸氣反序列化,而不是設定器期間調用](的
相關問題