Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this answer
    
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. – ivan_petrushenko Mar 7 at 15:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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