Broken DefaultValueHandling?

Jun 25, 2012 at 10:38 PM

According to the following statement should be true regarding DefaultValueHandling and setting it to Ignore

"For value types like int and DateTime the serializer will skip the default unitialized value for that value type"

I cannot get it to work as described though (using .net 4 and json 4.5.7)

Consider the following class

public class D {
	public int No;
	public string Name;
	public double Amount;
	public bool Check;
	public D Parent;
	public byte Byte;
	public uint Uint;

Used in the test below

public void TestMethod3() {
	var d = new D();
	var settings = new JsonSerializerSettings{DefaultValueHandling = DefaultValueHandling.Ignore};

	//writes {"No":0,"Amount":0.0,"Check":false,"Byte":0,"Uint":0}

It should (by my iterpretation of the documentation) write {}, but it doesn't. 


Aug 27, 2012 at 8:58 PM

Why has no one replied? I am having the same issues on 4.5.

Oddly, if you specify DefaultValue(0), etc on these, it works.

It seems like the default value its reading is incorrect? Are they defaulted to something like -1 instead?

Sep 16, 2012 at 12:03 PM

This has been fixed in 4.5.9