4
我剛將版本的NewtonSoft JSON.NET從版本3.0.0更新到3.5.0,並且我注意到受保護的成員不是隱式序列化的。NewtonSoft JSON.NET的升級不會隱式地序列化受保護的成員
我有下面的類:
public class SimpleFileContainer : IDto
{
public virtual string Name { get; protected set; }
public virtual string Path { get; protected set; }
public SimpleFileContainer(string name, string path)
{
Name = name;
Path = path;
}
}
下面的測試代碼沒有通過
var json = JsonConvert.SerializeObject(new SimpleFileContainer("Name", "Path"));
var deserialised = JsonConvert.DeserializeObject<SimpleFileContainer >(json);
Assert.That(deserialised.Name, Is.EqualTo("Name");
兩個名稱和路徑屬性爲null,除非我要麼使屬性設置公共或添加更新該類具有以下屬性:
[JsonObject(MemberSerialization.OptOut)]
public class SimpleFileContainer : IDto
{
[JsonProperty]
public virtual string Name { get; protected set; }
[JsonProperty]
public virtual string Path { get; protected set; }
public SimpleFileContainer(string name, string path)
{
Name = name;
Path = path;
}
}
這是一個合理的大小項目使用序列化過程很多,我不想通過代碼將這些屬性添加到每個類和成員。
有沒有辦法解決這個問題?
此設置還會序列化字段。不僅屬性。 – Kugel 2013-01-12 12:58:28