Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle null/empty values in JsonConvert.DeserializeObject

Tags:

json

c#

json.net

I have the following code:

return (DataTable)JsonConvert.DeserializeObject(_data, (typeof(DataTable))); 

Then, I tried:

var jsonSettings = new JsonSerializerSettings {     NullValueHandling = NullValueHandling.Ignore };  return (DataTable)JsonConvert.DeserializeObject<DataTable>(_data, jsonSettings); 

The return line is throwing the error:

{"Error converting value \"\" to type 'System.Double'."}

Lots of solutions online suggesting creating custom Class with nullable types but this won't work for me. I can't expect the json to be in a certain format. I have no control over the column count, column type, or column names.

like image 408
Kyle Avatar asked Aug 04 '15 15:08

Kyle


People also ask

How do I ignore NULL values in JSON Deserializing?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.

What does JsonConvert return?

Return ValueThe deserialized object from the JSON string.

How to deserialize object without [jsonproperty] attribute in JSON?

JsonConvert.DeserializeObject can leave reference type member properties null during deserialization without [JsonProperty] attribute on the property. 1. Synchronize the property name in the class and in the JSON file.

Does an empty string trigger serialization exception in NewtonSoft?

I'd want to rely on json serializer to throw serialization exception if it can't correctly deserialize required fields, but an empty string doesn't trigger serialization exception (be it just an empty string "" or an emtpy json string """" ); using Newtonsoft. Json ; using Newtonsoft.

What does ignore nullvaluehandling mean?

NullValueHandling setting This sample serializes an object to JSON with NullValueHandling set to Ignore so that properties with a default value aren't included in the JSON result.

How to ignore deserialization errors in NewtonSoft?

With Newtonsoft, you can choose to ignore deserialization errors. To do that, pass in an error handling lambda in the settings: All deserialization errors are ignored, and objects with errors are excluded from the results. In other words, the “bad apples” are removed from the bunch.


Video Answer


2 Answers

You can supply settings to JsonConvert.DeserializeObject to tell it how to handle null values, in this case, and much more:

var settings = new JsonSerializerSettings                     {                         NullValueHandling = NullValueHandling.Ignore,                         MissingMemberHandling = MissingMemberHandling.Ignore                     }; var jsonModel = JsonConvert.DeserializeObject<Customer>(jsonString, settings); 
like image 179
Thomas Hagström Avatar answered Sep 24 '22 12:09

Thomas Hagström


An alternative solution for Thomas Hagström, which is my prefered, is to use the property attribute on the member variables.

For example when we invoke an API, it may or may not return the error message, so we can set the NullValueHandling property for ErrorMessage:

     public class Response     {         public string Status;          public string ErrorCode;          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]         public string ErrorMessage;     }       var response = JsonConvert.DeserializeObject<Response>(data); 

The benefit of this is to isolate the data definition (what) and deserialization (use), the deserilazation needn’t to care about the data property, so that two persons can work together, and the deserialize statement will be clean and simple.

like image 20
Eynzhang Avatar answered Sep 21 '22 12:09

Eynzhang