wpf NewtonSoft.json 基类序列化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28561026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NewtonSoft.json base class serialization
提问by Ankit Jain
When I updated the concerned Newtonsoft.json.dll from .NET 3.5 to .NET 4.5 then base class is not Serializing. Before updating, the base class was serializing.
当我将相关的 Newtonsoft.json.dll 从 .NET 3.5 更新到 .NET 4.5 时,基类不是序列化。在更新之前,基类正在序列化。
public class MyBaseClass
{
public string BaseProp1 { get; set; }
public string BaseProp2 { get; set; }
}
[DataContract]
public class MyDerivedClass : MyBaseClass
{
[DataMember]
public DateTime DerProp1 { get; set; }
public string DerProp2 { get; set; }
}
class Program
{
static void Main(string[] args)
{
MyDerivedClass derc = new MyDerivedClass();
derc.BaseProp1 = "BaseProp1";
derc.DerProp1 = DateTime.Now;
derc.BaseProp2 = "BaseProp2";
derc.DerProp2 = "DerProp2";
Newtonsoft.Json.Converters.IsoDateTimeConverter conv = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
conv.DateTimeFormat = "MM/dd/yyyy HH:mm:ss zzz";
string jsonSerializedObject = JsonConvert.SerializeObject(derc, conv);
}
}
The program does not serialize members of the base class. The reason for that is that I have not specified the [DataContract] in the base class. I need ALL the members to be serialized. Problems:
该程序不会序列化基类的成员。原因是我没有在基类中指定[DataContract]。我需要将所有成员序列化。问题:
- I can not remove [DataContract] from the derived class as it is extensively used by WCF.
- I do not want to add [DataContract] to the base class as there are a LOT of members.
- 我无法从派生类中删除 [DataContract],因为它被 WCF 广泛使用。
- 我不想将 [DataContract] 添加到基类,因为有很多成员。
What is the best way to go ahead?
最好的方法是什么?
回答by M?rten Wikstr?m
The base class properties are ignored by default. You can change this behavior by creating a custom contract resolver.
默认情况下忽略基类属性。您可以通过创建自定义合同解析程序来更改此行为。
class MyContractResolver : DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var list = base.CreateProperties(type, memberSerialization);
foreach (var prop in list)
{
prop.Ignored = false; // Don't ignore any property
}
return list;
}
}
This resolver will effectively make sure that no properties are ignored. You might want to apply some logic if you do wantto ignore some properties.
这个解析器将有效地确保没有任何属性被忽略。如果您确实想忽略某些属性,则可能需要应用一些逻辑。
To use the resolver; create a JsonSerializerSettingsinstance and supply that to the JsonConvert.SerializeObjectmethod.
使用解析器;创建一个JsonSerializerSettings实例并将其提供给JsonConvert.SerializeObject方法。
Your last line would be replaced by:
您的最后一行将被替换为:
JsonSerializerSettings settings = new JsonSerializerSettings
{
ContractResolver = new MyContractResolver(),
Converters = { conv },
};
string jsonSerializedObject = JsonConvert.SerializeObject(derc, settings);
Base class properties would then be serialized:
然后将基类属性序列化:
{
"DerProp1":"02-17-2015 13:39:29 +01:00",
"DerProp2":"DerProp2",
"BaseProp1":"BaseProp1",
"BaseProp2":"BaseProp2"
}
回答by puko
Use JsonProperty
使用 Json 属性
public class MyBaseClass
{
[JsonProperty("BaseProp1")]
public string BaseProp1 { get; set; }
[JsonProperty("BaseProp2")]
public string BaseProp2 { get; set; }
}
[DataContract]
public class MyDerivedClass : MyBaseClass
{
[JsonProperty("DerProp1")]
[DataMember]
public DateTime DerProp1 { get; set; }
[JsonProperty("DerProp2")]
public string DerProp2 { get; set; }
}

