Closed yangyachao closed 7 years ago
@JamesNK , I have the same problem when convert datatable to json on .net core 2.0. Any one help?
The issue is due to the serializer don't use the DataSetConverter in netcore 2.0 (the current support of json.net v10.0.3 is netstandard 1.3)
I have used the same source code of DataSetConverter/DataTableConverter in a separate project created in dotnet core 2.0 (with some modification ) and json is created normally as expected.
I hosted the demo project in github. You can use the accompanied library until it's implemented in the next release. of json.net The generated json is:
{
"table1": [
{
"id": 0,
"item": "item 0"
},
{
"id": 1,
"item": "item 1"
}
]
}
I expect, once Json.Net support netcore 2.0 (which is final release), DataSetConverter/DataTableConverter will be available.
@moh-hassan Thank you so much ,But there is still a small problem,When the columnValue is DBNull
DBNull is supported in netstandard2 (netcore 2) Review the source code public sealed class DBNull : ISerializable, IConvertible
If you serialized the dataset to xml , `dataSet.WriteXml(textwriter) you get xml without error in case some columns have DBNull values.
I think this error is related to the internals of json.net of handling DBNull. You can avoid this error by supplying settings to JsonConvert.SerializeObject to tell it how to handle null values:
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
settings.Converters.Add(new DataSetConverter()); // using the acompanied library
var json = JsonConvert.SerializeObject(dataSet, settings);
I updated my demo in github to handle DBnull and generate a corresponding xml serialization.
Update:
json.net currently v10.0.3 handle DBNull internally with a conditional flag HAVE_ADO_NET
which is disabled in netstandard1.3 version. Once it's ported to netstandard2, I expect it will be handled without firing exception.
It will be fixed with a netstandard20 assembly - https://github.com/JamesNK/Newtonsoft.Json/commit/ab3315f1d5e57c70203c904be79d8e951bf09794
Is there a target date for when an 11.x version of Newtonsoft.Json will be available on Nuget (even as prerelease)?
@kfarris9 there is a version up now, (been up for a while)
When is the next stable build release in which this change set is included?