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; }
}
1 Answer 1
Although you are using the
using
statement withStringReader
andStringWriter
both theXmlReader
andXmlWriter
are implementingIDisposable
as well, so the usage of it should be enclosed in ausing
statement too.Catching an exception without doing anything with it is usually bad practice. You should at least log the exception.
Although you have named your variables and methods well, I am a little bit concerned about the
info
parameter because it isn't telling anything about what it is.
-
\$\begingroup\$ CA2202 Do not dispose objects multiple times Object 'textWriter' can be disposed more than once in method 'BackgroundJobInfoSerializer.Serialize(BackgroundJobInfo)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object. \$\endgroup\$indigo153– indigo1532016年03月07日 15:53:06 +00:00Commented Mar 7, 2016 at 15:53
DataContractSerializer
, why not use IgnoreDataMemberAttribute rather than coming up with a newDoNotSerializeAttribute
type to use alongsideDataMemberAttribute
? \$\endgroup\$