Json.Net 序列化问题
[DataContract] public class pcc { [DataMember] public string Name { get; set; } } public class ccc : pcc { public string cName { get; set; } }
由于 pcc 定义了 DataContract ,所以要求下面的所有属性要定义 DataMember 才能进行序列化。否则按 IgnoreDataMember 处理。
修改源码
类:JsonTypeReflector.GetObjectMemberSerialization 想办法让它返回 MemberSerialization.OptOut
类: DefaultContractResolver 增加字段
public MemberSerialization DefaultMemberSerialization = MemberSerialization.OptIn;
public static MemberSerialization GetObjectMemberSerialization(DefaultContractResolver resoler, Type objectType, bool ignoreSerializableAttribute) { JsonObjectAttribute objectAttribute = GetJsonObjectAttribute(objectType); if (objectAttribute != null) return objectAttribute.MemberSerialization; #if !PocketPC && !NET20 DataContractAttribute dataContractAttribute = GetDataContractAttribute(objectType); if (dataContractAttribute != null) { return resoler.DefaultMemberSerialization; } #endif #if !(SILVERLIGHT || NETFX_CORE || PORTABLE) if (!ignoreSerializableAttribute) { SerializableAttribute serializableAttribute = GetSerializableAttribute(objectType); if (serializableAttribute != null) return MemberSerialization.Fields; } #endif // the default return MemberSerialization.OptOut; }
初始化:
jSetting.ContractResolver = new DefaultContractResolver
{
DefaultMembersSearchFlags = BindingFlags.Instance | BindingFlags.Public,
SerializeCompilerGeneratedMembers = true,
DefaultMemberSerialization = MemberSerialization.OptOut
};
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。