File: DataTableConverterTests.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (213 lines) | stat: -rw-r--r-- 7,093 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#if !(SILVERLIGHT || NETFX_CORE)
using System;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
#endif
#if !NETFX_CORE
using System.Data;
#endif

namespace Newtonsoft.Json.Tests.Converters
{
  public class DataTableConverterTests : TestFixtureBase
  {
    [Test]
    public void Deserialize()
    {
      string json = @"[
  {
    ""id"": 0,
    ""item"": ""item 0""
  },
  {
    ""id"": 1,
    ""item"": ""item 1""
  }
]";

      DataTable deserializedDataTable = JsonConvert.DeserializeObject<DataTable>(json);
      Assert.IsNotNull(deserializedDataTable);

      Assert.AreEqual(string.Empty, deserializedDataTable.TableName);
      Assert.AreEqual(2, deserializedDataTable.Columns.Count);
      Assert.AreEqual("id", deserializedDataTable.Columns[0].ColumnName);
      Assert.AreEqual(typeof(long), deserializedDataTable.Columns[0].DataType);
      Assert.AreEqual("item", deserializedDataTable.Columns[1].ColumnName);
      Assert.AreEqual(typeof(string), deserializedDataTable.Columns[1].DataType);

      Assert.AreEqual(2, deserializedDataTable.Rows.Count);

      DataRow dr1 = deserializedDataTable.Rows[0];
      Assert.AreEqual(0, dr1["id"]);
      Assert.AreEqual("item 0", dr1["item"]);

      DataRow dr2 = deserializedDataTable.Rows[1];
      Assert.AreEqual(1, dr2["id"]);
      Assert.AreEqual("item 1", dr2["item"]);
    }

    [Test]
    public void Serialize()
    {
      // create a new DataTable.
      DataTable myTable = new DataTable("blah");

      // create DataColumn objects of data types.
      DataColumn colString = new DataColumn("StringCol");
      colString.DataType = typeof(string);
      myTable.Columns.Add(colString);

      DataColumn colInt32 = new DataColumn("Int32Col");
      colInt32.DataType = typeof(int);
      myTable.Columns.Add(colInt32);

      DataColumn colBoolean = new DataColumn("BooleanCol");
      colBoolean.DataType = typeof(bool);
      myTable.Columns.Add(colBoolean);

      DataColumn colTimeSpan = new DataColumn("TimeSpanCol");
      colTimeSpan.DataType = typeof(TimeSpan);
      myTable.Columns.Add(colTimeSpan);

      DataColumn colDateTime = new DataColumn("DateTimeCol");
      colDateTime.DataType = typeof(DateTime);
      colDateTime.DateTimeMode = DataSetDateTime.Utc;
      myTable.Columns.Add(colDateTime);

      DataColumn colDecimal = new DataColumn("DecimalCol");
      colDecimal.DataType = typeof(decimal);
      myTable.Columns.Add(colDecimal);

      // populate one row with values.
      DataRow myNewRow = myTable.NewRow();

      myNewRow["StringCol"] = "Item Name";
      myNewRow["Int32Col"] = 2147483647;
      myNewRow["BooleanCol"] = true;
      myNewRow["TimeSpanCol"] = new TimeSpan(10, 22, 10, 15, 100);
      myNewRow["DateTimeCol"] = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc);
      myNewRow["DecimalCol"] = 64.0021;
      myTable.Rows.Add(myNewRow);

      string json = JsonConvert.SerializeObject(myTable, Formatting.Indented);
      Assert.AreEqual(@"[
  {
    ""StringCol"": ""Item Name"",
    ""Int32Col"": 2147483647,
    ""BooleanCol"": true,
    ""TimeSpanCol"": ""10.22:10:15.1000000"",
    ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
    ""DecimalCol"": 64.0021
  }
]", json);
    }

    public class TestDataTableConverter : JsonConverter
    {
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
        DataTable d = (DataTable)value;
        writer.WriteValue(d.TableName);
      }

      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
        //reader.Read();
        DataTable d = new DataTable((string)reader.Value);

        return d;
      }

      public override bool CanConvert(Type objectType)
      {
        return (objectType == typeof(DataTable));
      }
    }

    [Test]
    public void PassedInJsonConverterOverridesInternalConverter()
    {
      DataTable t1 = new DataTable("Custom");

      string json = JsonConvert.SerializeObject(t1, Formatting.Indented, new TestDataTableConverter());
      Assert.AreEqual(@"""Custom""", json);

      DataTable t2 = JsonConvert.DeserializeObject<DataTable>(json, new TestDataTableConverter());
      Assert.AreEqual(t1.TableName, t2.TableName);
    }

    [Test]
    public void SerializeDataTableWithNull()
    {
      var table = new DataTable();
      table.Columns.Add("item");
      table.Columns.Add("price", typeof(double));
      table.Rows.Add("shirt", 49.99);
      table.Rows.Add("pants", 54.99);
      table.Rows.Add("shoes"); // no price

      var json = JsonConvert.SerializeObject(table);
      Assert.AreEqual(@"["
      + @"{""item"":""shirt"",""price"":49.99},"
      + @"{""item"":""pants"",""price"":54.99},"
      + @"{""item"":""shoes"",""price"":null}]", json);
    }

    [Test]
    public void SerializeDataTableWithNullAndIgnoreNullHandling()
    {
      var table = new DataTable();
      table.Columns.Add("item");
      table.Columns.Add("price", typeof(double));
      table.Rows.Add("shirt", 49.99);
      table.Rows.Add("pants", 54.99);
      table.Rows.Add("shoes"); // no price

      var json = JsonConvert.SerializeObject(table, Formatting.None, new JsonSerializerSettings
        {
          NullValueHandling = NullValueHandling.Ignore
        });
      Assert.AreEqual(@"["
      + @"{""item"":""shirt"",""price"":49.99},"
      + @"{""item"":""pants"",""price"":54.99},"
      + @"{""item"":""shoes""}]", json);
    }

    [Test]
    public void DerializeDataTableWithImplicitNull()
    {
      const string json = @"["
      + @"{""item"":""shirt"",""price"":49.99},"
      + @"{""item"":""pants"",""price"":54.99},"
      + @"{""item"":""shoes""}]";
      var table = JsonConvert.DeserializeObject<DataTable>(json);
      Assert.AreEqual("shirt", table.Rows[0]["item"]);
      Assert.AreEqual("pants", table.Rows[1]["item"]);
      Assert.AreEqual("shoes", table.Rows[2]["item"]);
      Assert.AreEqual(49.99, (double)table.Rows[0]["price"], 0.01);
      Assert.AreEqual(54.99, (double)table.Rows[1]["price"], 0.01);
      Assert.IsInstanceOfType(typeof(System.DBNull), table.Rows[2]["price"]);
    }

    [Test]
    public void DerializeDataTableWithExplicitNull()
    {
      const string json = @"["
      + @"{""item"":""shirt"",""price"":49.99},"
      + @"{""item"":""pants"",""price"":54.99},"
      + @"{""item"":""shoes"",""price"":null}]";
      var table = JsonConvert.DeserializeObject<DataTable>(json);
      Assert.AreEqual("shirt", table.Rows[0]["item"]);
      Assert.AreEqual("pants", table.Rows[1]["item"]);
      Assert.AreEqual("shoes", table.Rows[2]["item"]);
      Assert.AreEqual(49.99, (double)table.Rows[0]["price"], 0.01);
      Assert.AreEqual(54.99, (double)table.Rows[1]["price"], 0.01);
      Assert.IsInstanceOfType(typeof(System.DBNull), table.Rows[2]["price"]);
    }
  }
}
#endif