【发布时间】:2014-03-11 19:53:43
【问题描述】:
我遇到了一个奇怪的问题,Newtonsoft Json 条件序列化意外启动。我把它提炼成一个简单的例子(我们使用的是 newtonsoft json 5.0.8):
public class TestClass
{
public string Foo { get; set; }
public bool FooSpecified { get; set; }
public TestClass()
{ }
public TestClass(string foo, bool spec)
{
Foo = foo;
FooSpecified = spec;
}
}
class Program
{
static void Main(string[] args)
{
TestClass test1 = new TestClass("foo", false);
string serial1 = JsonConvert.SerializeObject(test1);
Console.WriteLine("Test 1: {0}", serial1);
TestClass test2 = new TestClass("bar", true);
string serial2 = JsonConvert.SerializeObject(test2);
Console.WriteLine("Test 2: {0}", serial2);
}
}
上面的输出:
Test 1: {"FooSpecified":false}
Test 2: {"Foo":"bar","FooSpecified":true}
似乎有一个名为“xSpecified”的布尔属性,其中“x”与另一个属性的名称匹配,这相当于条件序列化。我在 Newtonsoft json 条件序列化文档 (http://james.newtonking.com/json/help/index.html?topic=html/ConditionalProperties.htm) 的任何地方都没有发现这一点,也没有任何谷歌搜索显示此行为的其他命中。
有谁知道这是预期的行为还是我遇到了某种错误?
【问题讨论】:
-
这看起来很相关:json.codeplex.com/workitem/19091
标签: c# json serialization json.net