c# - How do I prevent undefined values from serializing to 0 for float values in .Net? -


i have rather large object class defined bunch of primitive attribute values (int, float, bool, string). getting object client application json string deserialize c# .net class can save them sql database. problem having serializer giving me default value of 0 float parameters breaks app undefined values need handled differently value of 0. (note: 0 values acceptable if user has defined them 0, can't assume undefined value 0.)

i have literally hundreds of these primitive attributes, i'm hoping there way make work globally instead of having write custom attribute type objects.

here's how i'm deserializing json string c# object

using system.web.script.serialization; // note: used deserialize json objects using newtonsoft.json; using newtonsoft.json.linq;  rootobject obj = jsonconvert.deserializeobject<rootobject>(jsonobjectfromclient); 

and here's object class

public class seatdefinition { public string definitionid { get; set; } public string r3_toltype { get; set; } public float r3_value { get; set; } // note: 0, shouldn't assumed 0 if undefined public bool r3_verified { get; set; } public float r4_minus { get; set; } // same here public float r4_plus { get; set; } // , 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 } 

can help?

edit 2016-01-05 11:38pm pst turns out i'm moron. auto-magic of deserializer leave values null if state in class definition should nullable. needed resolve problem change

public bool r3_verified { get; set; } 

to

public bool? r3_verified { get; set; } 

and left null values not passed in needed.

thank @dbc pointing me in right direction.

the natural way define float properties nullables:

    [jsonproperty(nullvaluehandling = nullvaluehandling.ignore)]     public float? r3_value { get; set; } 

setting nullvaluehandling.ignore prevents property being serialized json when not present. it's possible setting jsonserializersettings.nullvaluehandling avoiding need add attribute each property.

another possibility (which don't recommend) define special "sentinal" constant value indicates undefined floating value; set attribute [defaultvalue(constants.uninitializedfloat)] on each float inform json.net of default value; set [jsonproperty(defaultvaluehandling = defaultvaluehandling.ignoreandpopulate)] indicate that, when property missing json, default value should automatically assigned:

public static class constants {     public const double uninitializedfloat = float.minvalue;      public static bool isuninitialized(this float value)     {         return value == uninitializedfloat;     } }  public class seatdefinition {     [jsonproperty(defaultvaluehandling = defaultvaluehandling.ignoreandpopulate)]     [defaultvalue(constants.uninitializedfloat)]     public float r3_value { get; set; } // note: 0, shouldn't assumed 0 if undefined } 

i don't recommend because there's small chance sentinal value might not round-trip if accidentally serialized , deserialized. docs:

a value might not round-trip if floating-point number involved. value said round-trip if operation converts original floating-point number form, inverse operation transforms converted form floating-point number, , final floating-point number equal original floating-point number. round trip might fail because 1 or more least significant digits lost or changed in conversion.

thus sentinal might rounded , appear become initialized value.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -