I don't want to serialize everything. I created a custom attribute DoNotSerializeAttribute
. If some property in info
contain that attribute then ignore it and do not serialize it.
Is my code bug-free? Maybe I can improve it or I miss something?
class Program { static void Main(string[] args) { var s = Serialize(new BackgroundJobInfo {Text = "toto", BackgroundJob = new BackgroundJob { Password = "pass"}}); var myJob = Deserialize(s); } public static string Serialize(BackgroundJobInfo info) { using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture)) { var writer = XmlWriter.Create(stringWriter); var dataContractSerializer = new DataContractSerializer(typeof(BackgroundJobInfo), null, int.MaxValue, true, true, new MySurrogate()); dataContractSerializer.WriteObject(writer, info); writer.Flush(); return stringWriter.ToString(); } } public static BackgroundJobInfo Deserialize(string info) { using (var stringReader = new StringReader(info)) { try { var xmlReader = XmlReader.Create(stringReader); var dataContractSerializer = new DataContractSerializer(typeof(BackgroundJobInfo)); return (BackgroundJobInfo) dataContractSerializer.ReadObject(xmlReader); } catch (Exception e) { // hopefully, will never happen return null; } } } } internal class MySurrogate : IDataContractSurrogate { public Type GetDataContractType(Type type) { return typeof (BackgroundJobInfo); } public object GetObjectToSerialize(object obj, Type targetType) { var maskedProperties = obj.GetType().GetProperties(); var b = maskedProperties.Where(m => m.GetCustomAttributes(typeof(DataMemberAttribute), true).Any() && m.GetCustomAttributes(typeof(DoNotSerializeAttribute), true).Any()); foreach (var member in b) { member.SetValue(obj, null, null); } return obj; } internal class DoNotSerializeAttribute : Attribute { } [KnownType(typeof(BackgroundJob))] [DataContract] public class BackgroundJobInfo { [DataMember(Name = "text")] public string Text { get; set; } [DataMember(Name = "backgroundJob")] public BackgroundJob BackgroundJob { get; set; } } [DataContract] public class BackgroundJob { [DataMember(Name = "password")] [DoNotSerializeAttribute] public string Password { get; set; } }
DataContractSerializer
, why not use IgnoreDataMemberAttribute rather than coming up with a newDoNotSerializeAttribute
type to use alongsideDataMemberAttribute
?\$\endgroup\$