Newtonsoft.json: serializes a DataSet to JSON on dotnet core 2.0

Created on 28 Aug 2017  路  8Comments  路  Source: JamesNK/Newtonsoft.Json

2017-08-28 12 01 49

netstandard20

Most helpful comment

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

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

All 8 comments

@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 锛孊ut there is still a small problem,When the columnValue is DBNull
084101f8-0e48-4323-a72c-f55368f6372f

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.

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)

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

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

Was this page helpful?
0 / 5 - 0 ratings