0
\$\begingroup\$

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; }
}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ "Is my code bug-free? " thats something for you to test by yourself having unit tests. \$\endgroup\$
    – Heslacher
    Mar 7, 2016 at 15:03
  • \$\begingroup\$ Since you're using DataContractSerializer, why not use IgnoreDataMemberAttribute rather than coming up with a new DoNotSerializeAttribute type to use alongside DataMemberAttribute? \$\endgroup\$
    – Dan Lyons
    Mar 7, 2016 at 18:54

1 Answer 1

1
\$\begingroup\$
  • Although you are using the using statement with StringReader and StringWriter both the XmlReader and XmlWriter are implementing IDisposable as well, so the usage of it should be enclosed in a using 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.

\$\endgroup\$
1
  • \$\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
    Mar 7, 2016 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.