Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change in JSON generated for a System.Data.DataTable in a .NET Core project vs .NET Framework

The program below generates different JSON when run in a .NET Core project vs a .NET Framework app.

Code

class Program
{
    internal static readonly MediaTypeFormatter DefaultFormatter = new JsonMediaTypeFormatter
    {
        UseDataContractJsonSerializer = false,
        SerializerSettings =
        {
            NullValueHandling = NullValueHandling.Ignore,
            DateTimeZoneHandling = DateTimeZoneHandling.Utc,
            DateFormatHandling = DateFormatHandling.IsoDateFormat
        }
    };

    private static DataTable BuildTestDataTable()
    {
        var testDataTable = new DataTable();
        testDataTable.Columns.Add("Label", typeof(string));
        testDataTable.Columns.Add("Metric", typeof(decimal));
        testDataTable.Rows.Add("A", 10);
        testDataTable.Rows.Add("B", 20);
        return testDataTable;
    }

    static void Main(string[] args)
    {
        DataTable table = BuildTestDataTable();

        ObjectContent oc = new ObjectContent(table.GetType(), table, DefaultFormatter);

        Console.WriteLine(oc.ReadAsStringAsync().Result);

        Console.ReadKey();
    }
}

.Net Framework:

[{"Label":"A","Metric":10.0},{"Label":"B","Metric":20.0}]

.Net Core:

{
    "DataTable.RemotingVersion": {
        "_Major": 2,
        "_Minor": 0,
        "_Build": -1,
        "_Revision": -1
    },
    "XmlSchema": "<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<xs:schema xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">\r\n  <xs:element name=\"Table1\">\r\n    <xs:complexType>\r\n      <xs:sequence>\r\n        <xs:element name=\"Label\" type=\"xs:string\" msdata:targetNamespace=\"\" minOccurs=\"0\" />\r\n        <xs:element name=\"Metric\" type=\"xs:decimal\" msdata:targetNamespace=\"\" minOccurs=\"0\" />\r\n      </xs:sequence>\r\n    </xs:complexType>\r\n  </xs:element>\r\n  <xs:element name=\"tmpDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"Table1\" msdata:UseCurrentLocale=\"true\">\r\n    <xs:complexType>\r\n      <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\" />\r\n    </xs:complexType>\r\n  </xs:element>\r\n</xs:schema>",
    "XmlDiffGram": "<diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\">\r\n  <tmpDataSet>\r\n    <Table1 diffgr:id=\"Table11\" msdata:rowOrder=\"0\" diffgr:hasChanges=\"inserted\">\r\n      <Label>A</Label>\r\n      <Metric>10</Metric>\r\n    </Table1>\r\n    <Table1 diffgr:id=\"Table12\" msdata:rowOrder=\"1\" diffgr:hasChanges=\"inserted\">\r\n      <Label>B</Label>\r\n      <Metric>20</Metric>\r\n    </Table1>\r\n  </tmpDataSet>\r\n</diffgr:diffgram>"
}

Can someone illuminate me on the cause of this, and whether it is an intentional difference ?

like image 870
stevec Avatar asked Oct 18 '22 09:10

stevec


1 Answers

As per https://github.com/JamesNK/Newtonsoft.Json/issues/1383

It is in netstandard1.5 but netstandard1.3 is the highest that Json.NET supports. When a netstandard2.0 build is eventually added then I'll add support for it.

like image 145
stevec Avatar answered Oct 21 '22 09:10

stevec