question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serializing a DataSet with empty data tables loses all column information

See original GitHub issue

We 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

  1. Before outputting the Rows for each table, output the table name and columns/column data types
  2. When reading the data back, add the columns and set the table name before attempting to create the rows.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
harrisonbscommented, Jun 17, 2017

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.

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.

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?

0reactions
robinsmhcommented, Aug 18, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found