Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dbnetlink/typeddatatableconverter
A version of a JSON.NET DataTable converted that allows you to specify the data types of columns if known
https://github.com/dbnetlink/typeddatatableconverter
Last synced: about 22 hours ago
JSON representation
A version of a JSON.NET DataTable converted that allows you to specify the data types of columns if known
- Host: GitHub
- URL: https://github.com/dbnetlink/typeddatatableconverter
- Owner: dbnetlink
- Created: 2023-12-06T08:57:03.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-06T09:32:46.000Z (about 1 year ago)
- Last Synced: 2024-11-05T12:26:08.752Z (about 2 months ago)
- Language: C#
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TypedDataTableConverter
A version of the JSON.NET DataTable converter that allows you to specify the data types of columns if known.The default datatable converter will sometimes not accurately identify the type of column if the JSON being parsed has either a missing or ambiguously formatted value in the first row.
If you know the types of the values in the JSON file up front to the converter which will use these values rather than tying to infer them from the data. You can supply this information to the converted either as a dictionary of types keyed on the property names or as the type of the object the JSON represents. The converter will fall back to it's default mechanism for identifying a type if it cannot find a column type in the supplied values.
```
var typedConverter = new TypedDataTableConverter(typeof(Employee));
DataTable dataTable = JsonConvert.DeserializeObject(json, typedConverter);
```
or
```
var employeeDataTypes = new Dictionary();
employeeDataTypes["HireDate"] = typeof(DateTime);
employeeDataTypes["StartDate"] = typeof(DateTime);
employeeDataTypes["Active"] = typeof(bool);
employeeDataTypes["Salary"] = typeof(decimal);
var typedConverter = new TypedDataTableConverter(employeeDataTypes);
DataTable dataTable = JsonConvert.DeserializeObject(json, typedConverter);
```The converter code is a direct copy of the Newtonsoft [repository code](https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/DataTableConverter.cs) with a few small changes