JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.84k stars 3.26k forks source link

serializes a DataSet to JSON on dotnet core 2.0 #1409

Closed yangyachao closed 7 years ago

yangyachao commented 7 years ago
2017-08-28 12 01 49
TimRowe commented 7 years ago

@JamesNK , I have the same problem when convert datatable to json on .net core 2.0. Any one help?

moh-hassan commented 7 years ago

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.

yangyachao commented 7 years ago

@moh-hassan Thank you so much ,But there is still a small problem,When the columnValue is DBNull 084101f8-0e48-4323-a72c-f55368f6372f

moh-hassan commented 7 years ago

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.

JamesNK commented 7 years ago

It will be fixed with a netstandard20 assembly - https://github.com/JamesNK/Newtonsoft.Json/commit/ab3315f1d5e57c70203c904be79d8e951bf09794

kfarris9 commented 7 years ago

Is there a target date for when an 11.x version of Newtonsoft.Json will be available on Nuget (even as prerelease)?

EvanCarroll commented 6 years ago

@kfarris9 there is a version up now, (been up for a while)

https://www.nuget.org/packages/Newtonsoft.Json/11.0.1-beta1

pranavagg commented 6 years ago

When is the next stable build release in which this change set is included?