I'm attempting to serialize a .NET DataTable to a JSON file and then deserialize the JSON file back into a DataTable. Fairly straightforward I thought.
However, I have a table, 3 rows by 3 columns, each element is of type double. If any value in the first row is null, when JSON.Net deserializes the json file to a DataTable object, all values of the column that was null in the first row become strings.
To be clear, it is only if a value in the first row is null that this happens. If any values are null in any other row than the first, the remaining values in that column remain doubles.
If I replace the null with a double, everything works as expected (I can't do this in my case, however).
If I set NullValueHandling = NullValueHandling.Ignore, all values stay as doubles, except the first row now gets listed as the last row:
Example:
"Column2": 1.0,
"Column3": 1.1
},
{
"Column1": 0.0,
"Column2": 0.5,
"Column3": 2.0
},
Becomes:
"Column2": 1.0,
"Column3": 1.1
},
{
"Column2": 0.5,
"Column3": 2.0,
"Column1": 0.0
},
I need to be able to deserialize the JSON, keep the Columns in order, and not have null values in the first row cause all values in that row to become strings. I also need to keep the Column1 of the first row (in the case above) null - don't care if it is a null string or a DBNull.
Any thoughts? (My test code below..comment/uncomment NullValueHandling to see the problem)
DataTable table = new DataTable("MyTable");
table.Columns.Add("Column1", typeof(double));
table.Columns.Add("Column2", typeof(double));
table.Columns.Add("Column3", typeof(double));
for (int i = 0; i < 10; i++) {
if (i == 0)
table.Rows.Add(null, 1.0, 1.1);
else
table.Rows.Add(0.0, 0.5, 2.0);
}
JsonSerializer serializer = new JsonSerializer();
//serializer.TypeNameHandling = TypeNameHandling.All;
serializer.NullValueHandling = NullValueHandling.Ignore;
using (StreamWriter sw1 = new StreamWriter("1st.json"))
using (JsonWriter writer1 = new JsonTextWriter(sw1))
{
writer1.Formatting = Formatting.Indented;
serializer.Serialize(writer1, table);
}
DataTable newtable;
using (StreamReader sr = new StreamReader("1st.json"))
using (JsonReader reader = new JsonTextReader(sr))
{
newtable = (DataTable)serializer.Deserialize(reader, typeof(DataTable));
}
using (StreamWriter sw = new StreamWriter("3rd.json"))
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
serializer.Serialize(writer, newtable);
}
See Question&Answers more detail:
os