Comments on: How to Convert DataTable to JSON in C# https://code-maze.com/convert-datatable-json-csharp/ Learn. Code. Succeed. Thu, 10 Nov 2022 08:05:32 +0000 hourly 1 https://wordpress.org/?v=6.7.5 By: Ivan Matec https://code-maze.com/convert-datatable-json-csharp/#comment-6901 Thu, 10 Nov 2022 08:05:32 +0000 https://drafts.code-maze.com/?p=76499#comment-6901 In reply to Byron Adams.

Hi, Byron! Thank you for the comment. I agree it is possible to return serialized dataset or a data table in the API endpoint and it would be serialized to a selected format, including JSON. It is the consequence of the API configuration, not actually the built-in support we were looking for in this article.

]]>
By: Byron Adams https://code-maze.com/convert-datatable-json-csharp/#comment-6900 Thu, 10 Nov 2022 00:13:53 +0000 https://drafts.code-maze.com/?p=76499#comment-6900 there should be a built-in method to do it”. I was surprised to see this statement. For years now, I have had all my webAPIs like this
public object GetData(MyContext c)
{
var ds=new DataSet(“R”);
//setup some parameters p
c.GetData(ds, “SELECT * FROM Table1”, “T1”, p);
c.GetData(ds, “SELECT * FROM Table2”, “T2”, p);
return ds;
}

Actually, it does not seem to matter what I return, the object automatically gets serialized to json.

]]>
By: Ivan Matec https://code-maze.com/convert-datatable-json-csharp/#comment-6868 Wed, 02 Nov 2022 09:08:56 +0000 https://drafts.code-maze.com/?p=76499#comment-6868 In reply to James Curran.

Hi, James!
Thank you for the comment. I’ve tested your suggestion regarding the StringBuilder and there is a slight improvement in the performance but it is noticeable on a smaller amount of records. For example, for 1000 records StringBuilder is even faster than Newtonsoft. But the order of methods per performance is the same in all other examples.

]]>
By: Marinko Spasojević https://code-maze.com/convert-datatable-json-csharp/#comment-6867 Wed, 02 Nov 2022 07:24:15 +0000 https://drafts.code-maze.com/?p=76499#comment-6867 In reply to James Curran.

Hi James.

Regarding the _faker field, we didn’t show it in the snippet but it exists in our source code. Now, I added it to the snippet as well to avoid confusion.

About those OfType methods, yeah, that was exactly the case. Somehow the snippet swallowed those <DataRow> and <DataColumn> parts, I’ve fixed those as well.

About the last suggestion, we will see with the author of the article to test it.

Thank you as always.

]]>
By: James Curran https://code-maze.com/convert-datatable-json-csharp/#comment-6865 Tue, 01 Nov 2022 16:57:15 +0000 https://drafts.code-maze.com/?p=76499#comment-6865 Your GenerateDummyDataTable method uses a _faker object, which isn’t identified.

I assume that the “dataTable.Rows.OfType()” references should be dataTable.Rows.OfType<DataRow>(), with the generic type being swallowed by html. I prefer using “dataTable.Rows.Cast<DataRow>()” but it’s pretty much the same either way.

It’s funny that System.Text.Json did so badly here, since it’s whole reason for existing is to be faster that Newtonsoft. The delay here is probably because it has to do two conversions.

The StringBuilder version would probably get a boost, if you preallocated it to a reasonable large size. I’d suggest something like :    var jsonStringBuilder = new StringBuilder(dataTable.Columns.Count * dataTable.Rows.Count * 25); 

]]>