9

How should i mark properties, that they should not be serialize if they have default values? For example if i have boolean value it shoudl be serialized if is only set to true, the same with nullable value, if they are null i don't want to include them in my serialization file.

5
  • What's the aim of this!?
    – Arnaud F.
    Feb 24, 2012 at 11:04
  • to have smaller serialized file cos i need to sent it sometimes thrue gprs network where i need to pay for trafic, anyway during restoration (deserialization) they will be set to default value if they don't exist so how to avoid serializing them?
    – kosnkov
    Feb 24, 2012 at 11:07
  • I think you'll either have to write your own serializer, or postprocess the result of the built-in serializer with your own rules.
    – Roy Dictus
    Feb 24, 2012 at 11:08
  • I think that it would be nice to simple mark the property to achive it.
    – kosnkov
    Feb 24, 2012 at 11:13
  • You should have better to (G)Zip your XML before sending it over the network in that case.
    – Arnaud F.
    Feb 24, 2012 at 12:59

3 Answers 3

23

Ok I found it myself. It is [DefaultValue(false)]. If I mark some property with this attr then it will be serialized only if is different than value in ().

System.ComponentModel.DefaultValueAttribute

1
  • Thanks for the solution! Any idea how to avoid that I have to put the default value twice? [DefaultValue("Status")] public string statusTag = "Status";
    – pfuhlert
    Oct 25, 2017 at 13:39
4

There is such thing as Specified property. I can't find msdn documentation on it but this article should be helpful. Basically you have to write something like this:

//this property would not be serialized if it contains String.Empty value
public string PropertyName   {   get; set;  }


[XmlIgnore]
public bool PropertyNameSpecified
{
    get  { return PropertyName != String.Empty; }
}
2
  • get { return PropertyName != String.Empty; } what is that? u re comparing bool with string, and when i add xmlIgnore it never appears in serialized file even if this property isn't null or is true in case of bool.
    – kosnkov
    Feb 24, 2012 at 12:34
  • I've edited my answer. Hope this give you proper understanding of whats happening there. Feb 24, 2012 at 13:18
0

The answer of @kosnkov put me on the right track. In my case I wanted so make a bool by default true even when its deserialized or not. I ended up with something like this:

public class Person {
   public string Name {get;set;}

   [DefaultValue(true)]
   public bool IsAdult {get;set;} = true
}

So this Json

{"Name" :"SomePerson"}

Results into this object

{ Name : "SomePerson", IsAdult : true}

To explain:

  • The Default value attribute tells the serializer what the default value is. So it only impacts the decission should the serializer override the current value or not, because it either doesn't match the supplied value or the default value.
  • The = true sets the initial value
  • You still have to supply the initial value, because the serializer won't touch the IsAdult property but by systemDefault a bool is false.

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.