ServiceStack/ServiceStack.Text — pull request #85 by houlgap (2012-03-06 17:00:52+00:00)
Title: Added tests and support for the DefaultValue attribute

I've only just started using ServiceStack.Text and am looking at it to replace xml serialization.

I've noticed that many of the value type fields within the objects I'm serializing are set to their defaults, and those fields are still in the output stream.

Take for example the following class

class DefaultValueTestClass
{
public DefaultValueTestClass()
{
Id = 0;
IsDefault = false;
Text = string.Empty;
}

public int Id { get; set; }
public bool IsDefault { get; set; }
public string Text { get; set; }
}

serializing a default instance would result in a serialized output of

{Id:0,IsDefault:false,Text:}

If the fields specified their default values, ie.

{
[DefaultValue(0)]
public int Id { get; set;}

[DefaultValue(false)]
public bool IsDefault { get; set; }

[DefaultValue("")]
public string Text { get; set; }
}

then the serialized output would be

{}