How do I prevent undefined values from serializing to 0 for float values in .Net?
<p>I have a rather large object class that is defined by a bunch of primitive attribute values (int, float, bool, string). I am getting the object from a client application as a json string which I deserialize into a C# .Net class so that I can save them to a SQL database. The problem I am having is the serializer is giving me a default value of 0 for float parameters which breaks my app as undefined values need to be handled differently than a value of 0. (Note: 0 values are acceptable if the user has defined them to be 0, but I can't assume that an undefined value is 0.)</p>

<p>I have literally hundreds of these primitive attributes, so I'm hoping there is a way to make this work globally instead of having to write custom attribute type objects.</p>

<p>Here's how I'm deserializing the JSON string into a C# object</p>

<pre><code>using System.Web.Script.Serialization; // Note: used to deserialize JSON objects
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

RootObject obj = JsonConvert.DeserializeObject&lt;RootObject&gt;(JSONObjectFromClient);
</code></pre>

<p>And here's my object class</p>

<pre><code>public class SeatDefinition
{
public string DefinitionID { get; set; }
public string r3_tolType { get; set; }
public float r3_value { get; set; } // Note: this could be 0, but it shouldn't be assumed to be 0 if undefined
public bool r3_verified { get; set; }
public float r4_minus { get; set; } // same here
public float r4_plus { get; set; } // and here
public string r4_tolType { get; set; }
public float r4_value { get; set; } //etc
public bool r4_verified { get; set; }
public float r5_minus { get; set; }
public float r5_plus { get; set; }
public string r5_tolType { get; set; }
public float r5_value { get; set; }
public bool r5_verified { get; set; }
// ... 400 more such attributes
}
</code></pre>

<p>Can anyone help?</p>

<p><strong>EDIT 2016-01-05 11:38PM PST</strong>
Turns out I'm a moron. The auto-magic of the deserializer will leave values as null if you state in the class definition that they should be nullable. All I needed to do to resolve my problem was change</p>

<pre><code>public bool r3_verified { get; set; }
</code></pre>

<p>to</p>

<pre><code>public bool? r3_verified { get; set; }
</code></pre>

<p>and I was left with null values for those that were not passed in as I needed.</p>

<p>Thank you @dbc for pointing me in the right direction.</p>