在反序列化期间设置值时,如何防止Newtonsoft.JSON重用默认值

时间:2014-09-10 22:48:40

标签: json.net

这是一个奇怪的情况,但是当我们不想将数据分成单独的表时,它是我们用来处理SQL后端的一种模式。

这里的问题是,在反序列化时,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);
    }
}

1 个答案:

答案 0 :(得分: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);

    }
 }