Serializing a DataSet with empty data tables loses all column information
See original GitHub issueWe have introduced some optimisations at work to a web service to improve the speed. The aim here is to serialize some data to a file to avoid going to the database if we have already made a call to the same Stored Proc with the same arguments within a cache timeout window
ds = DataAccess.ExecuteDataSet
Try
If fileCache.CacheFilePath IsNot Nothing AndAlso Not File.Exists(fileCache.CacheFilePath) Then
ds.Serialize(fileCache.CacheFilePathTemp)
fileCache.RenameTemp()
End If
Catch ex As Exception
' couldn't write to the cache? Don't want this to affect the report, so just write to the output
' and move on
Debug.Print("Could not write to report cache: " + ex.Message)
End Try
Now consider that the DataSet contains two tables, but the tables are empty When serialized, we get the following JSON
{
"Table": [],
"Table1": []
}
We have lost the following information
- The name of the table
- The name of any columns and their associated data type
The fix is as follows
- Before outputting the Rows for each table, output the table name and columns/column data types
- When reading the data back, add the columns and set the table name before attempting to create the rows.
Issue Analytics
- State:
- Created 6 years ago
- Comments:16 (3 by maintainers)
Top Results From Across the Web
c# - Serializing a DataSet with empty data tables loses all ...
I tried to serialise a dataset to json.Everything is working fine,but column informations are missing ,if no records.I found some solutions,but ...
Read more >Converting Dataset to Json with Empty datatable's columns
Hi everyone, I want to convert DataSet to Json using newtonsoft. I converted it successfully but an issue here is the datatable with...
Read more >DataTable JSON Serialization in JSON.NET and ... - Rick Strahl
I've recently made a switch to JSON.NET for my JavaScript parsing which has been a great move for flexibility. One thing missing from...
Read more >Cutting Edge: Binary Serialization of DataSets
First, the DataSet is a disconnected object—you can use it without a physical connection to the data source. In addition, the DataSet provides...
Read more >Optimizing Serialization in .NET - part 2
Provides code and techniques to enable developers to optimize serialization of DataSets/DataTables.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
With all due respect, I don’t think this issue should be closed. It should be fixed in the manner I said above - use a setting to determine if you want to preserve column information. Serializing the ROWS data only can lead to a lossy deserialization which caused a serious issue for my client because the object that was serialized and then deserialzed again was NOT equal to the original object.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/
Saying it’s supposed to be near enough is good enough is not actually good enough. And if I have a solution, why wouldn’t you include it back into the code in the manner highlighted?
I can’t believe that this is still a problem - but it is. Even more so in ASP .Net Core where use of the session state requires everything to be serialised.
We don’t serialise to json because we want ‘nice’ clean json, we serialise because we want a json representation of ALL the relevant data. For a datatable, this must absolutely include the column names and types even if there are no rows, and it must be reversible back to an equivalent datatable when de-serialized.
PLEASE PLEASE PLEASE add an option to include the table schema in the json, otherwise what’s the point of supporting DataSet/DataTable at all? It it simply not fit for purpose currently.