1

I'm using the Newtonsoft Json Converter to serialize and deserialize objects and it works great for my program.

But if any of the values stored within the object are null/empty, they are serialized as {}, unable to be deserialized, and my program stops.

For example, if I deserialize the following code, everything works great:

{
"Thing1": 2,
"Thing2": false,
"Thing3": "string",
"Thing4": "2017-10-28T14:04:24.74"
}

But if I try to deserialize the following code:

{
"Thing1": {},
"Thing2": false,
"Thing3": "",
"Thing4": {}
} 

Thing1 and Thing4 will both cause problems during deserialization.

Not sure if this could be related to the way I am reading from my database to serialize:

var r = Serialize(myReader);

string json = JsonConvert.SerializeObject(r, 
                        Formatting.Indented, 
                        new JsonSerializerSettings 
                        { 
                            NullValueHandling = NullValueHandling.Ignore 
                        });

public IEnumerable<Dictionary<string, object>> Serialize(SqlDataReader reader)
           {
            var results = new List<Dictionary<string, object>>();
            var cols = new List<string>();
            for (var i = 0; i < reader.FieldCount; i++)
                cols.Add(reader.GetName(i));

            while (reader.Read())
                results.Add(SerializeRow(cols, reader));

            return results;
            }

private Dictionary<string, object> SerializeRow(IEnumerable<string> cols,
                                                    SqlDataReader reader)
            {
                var result = new Dictionary<string, object>();
                foreach (var col in cols)
                    result.Add(col, reader[col]);
                return result;
            }

I've used:

new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }

Within my deserializer:

BlankObject blnkObj = JsonConvert.DeserializeObject<BlankObject>(json, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

How can I deserialize an empty value? Thanks in advance.

g3muse
  • 121
  • 10
  • 2
    What "problems" does it cause and what did your research show for that? – CodeCaster Jun 15 '17 at 17:32
  • My program errors out whenever an empty value is deserialized. If I comment out the empty values within the BlankObject class (in this case Thing1 and Thing4), deserialization works. `int Thing1 { get; set; }` – g3muse Jun 15 '17 at 17:44
  • The problem is that the value of `"Thing1"` is sometimes an integer and sometimes an object of unknown type. What type should Json.NET when creating a `DataColumn` for such a property? If you know in advance the correct type for `"Thing1"` you could create your own version of `DataTableConverter` as shown [here](https://stackoverflow.com/a/37126529/3744182). – dbc Jun 15 '17 at 20:46

2 Answers2

1

You need to have a class structure like following:

public class RootObject
{
    public object Thing1 { get; set; }
    public bool Thing2 { get; set; }
    public string Thing3 { get; set; }
    public object Thing4 { get; set; }
}
tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
0

Figured out my problem thanks to the comment by @dbc

An object of null value is serialized as {} in JSON instead of null

The deserializer did not like deserializing {} into an int. Because a null object is of a different type than an int or DateTime.

My solution was to serialize the results of the database to have null instead of {} by reading from the database like this:

BlankClass test = new BlankClass();
string json = "";

if (myReader.Read())
{
      test.Thing1 = (int)myReader[0];
      test.Thing2 = (bool)myReader[1];
      test.Thing3 = (string)myReader[2];
      test.Thing4 = (Datetime?)myReader[3];
      json = JsonConvert.SerializeObject(test, Formatting.Indented);
 }

 class BlankClass
 {
      int Thing1 { get; set; }
      bool Thing2 { get; set; }
      string Thing3 { get; set; }
      DateTime? Thing4 { get; set; }
 }
g3muse
  • 121
  • 10