Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json.Tests\Converters\DataSetConverterTests.cs

Symbol Coverage: 100.00% (93 of 93)

Branch Coverage: 100.00% (9 of 9)

Cyclomatic Complexity Avg: 1.40 Max:2

Code Lines: 150


L V Source
1
#if !SILVERLIGHT
2
using System;
3
using System.Collections.Generic;
4
using System.Linq;
5
using System.Text;
6
using Newtonsoft.Json.Converters;
7
using NUnit.Framework;
8
using Newtonsoft.Json.Tests.TestObjects;
9
using System.Data;
10

  
11
namespace Newtonsoft.Json.Tests.Converters
12
{
13
  public class DataSetConverterTests : TestFixtureBase
14
  {
15
    [Test]
16
    public void SerializeAndDeserialize()
17
    {
18
 1
      DataSet dataSet = new DataSet("dataSet");
19
 1
      dataSet.Namespace = "NetFrameWork";
20
 1
      DataTable table = new DataTable();
21
 1
      DataColumn idColumn = new DataColumn("id", typeof(int));
22
 1
      idColumn.AutoIncrement = true;
23

  
24
 1
      DataColumn itemColumn = new DataColumn("item");
25
 1
      table.Columns.Add(idColumn);
26
 1
      table.Columns.Add(itemColumn);
27
 1
      dataSet.Tables.Add(table);
28

  
29
 1
      for (int i = 0; i < 2; i++)
30
      {
31
 2
        DataRow newRow = table.NewRow();
32
 2
        newRow["item"] = "item " + i;
33
 2
        table.Rows.Add(newRow);
34
      }
35

  
36
 1
      dataSet.AcceptChanges();
37

  
38
 1
      string json = JsonConvert.SerializeObject(dataSet, Formatting.Indented);
39
      
40
 1
      Assert.AreEqual(@"{
41
 1
  ""Table1"": [
42
 1
    {
43
 1
      ""id"": 0,
44
 1
      ""item"": ""item 0""
45
 1
    },
46
 1
    {
47
 1
      ""id"": 1,
48
 1
      ""item"": ""item 1""
49
 1
    }
50
 1
  ]
51
 1
}", json);
52

  
53
 1
      DataSet deserializedDataSet = JsonConvert.DeserializeObject<DataSet>(json);
54
 1
      Assert.IsNotNull(deserializedDataSet);
55

  
56
 1
      Assert.AreEqual(1, deserializedDataSet.Tables.Count);
57

  
58
 1
      DataTable dt = deserializedDataSet.Tables[0];
59

  
60
 1
      Assert.AreEqual("Table1", dt.TableName);
61
 1
      Assert.AreEqual(2, dt.Columns.Count);
62
 1
      Assert.AreEqual("id", dt.Columns[0].ColumnName);
63
 1
      Assert.AreEqual(typeof(long), dt.Columns[0].DataType);
64
 1
      Assert.AreEqual("item", dt.Columns[1].ColumnName);
65
 1
      Assert.AreEqual(typeof(string), dt.Columns[1].DataType);
66

  
67
 1
      Assert.AreEqual(2, dt.Rows.Count);
68
 1
    }
69

  
70
    [Test]
71
    public void SerializeMultiTableDataSet()
72
    {
73
 1
      DataSet ds = new DataSet();
74
 1
      ds.Tables.Add(CreateDataTable("FirstTable", 2));
75
 1
      ds.Tables.Add(CreateDataTable("SecondTable", 1));
76

  
77
 1
      string json = JsonConvert.SerializeObject(ds, Formatting.Indented, new IsoDateTimeConverter());
78
      // {
79
      //   "FirstTable": [
80
      //     {
81
      //       "StringCol": "Item Name",
82
      //       "Int32Col": 1,
83
      //       "BooleanCol": true,
84
      //       "TimeSpanCol": "10.22:10:15.1000000",
85
      //       "DateTimeCol": "2000-12-29T00:00:00Z",
86
      //       "DecimalCol": 64.0021
87
      //     },
88
      //     {
89
      //       "StringCol": "Item Name",
90
      //       "Int32Col": 2,
91
      //       "BooleanCol": true,
92
      //       "TimeSpanCol": "10.22:10:15.1000000",
93
      //       "DateTimeCol": "2000-12-29T00:00:00Z",
94
      //       "DecimalCol": 64.0021
95
      //     }
96
      //   ],
97
      //   "SecondTable": [
98
      //     {
99
      //       "StringCol": "Item Name",
100
      //       "Int32Col": 1,
101
      //       "BooleanCol": true,
102
      //       "TimeSpanCol": "10.22:10:15.1000000",
103
      //       "DateTimeCol": "2000-12-29T00:00:00Z",
104
      //       "DecimalCol": 64.0021
105
      //     }
106
      //   ]
107
      // }
108

  
109
 1
      DataSet deserializedDs = JsonConvert.DeserializeObject<DataSet>(json, new IsoDateTimeConverter());
110

  
111
 1
      Assert.AreEqual(@"{
112
 1
  ""FirstTable"": [
113
 1
    {
114
 1
      ""StringCol"": ""Item Name"",
115
 1
      ""Int32Col"": 1,
116
 1
      ""BooleanCol"": true,
117
 1
      ""TimeSpanCol"": ""10.22:10:15.1000000"",
118
 1
      ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
119
 1
      ""DecimalCol"": 64.0021
120
 1
    },
121
 1
    {
122
 1
      ""StringCol"": ""Item Name"",
123
 1
      ""Int32Col"": 2,
124
 1
      ""BooleanCol"": true,
125
 1
      ""TimeSpanCol"": ""10.22:10:15.1000000"",
126
 1
      ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
127
 1
      ""DecimalCol"": 64.0021
128
 1
    }
129
 1
  ],
130
 1
  ""SecondTable"": [
131
 1
    {
132
 1
      ""StringCol"": ""Item Name"",
133
 1
      ""Int32Col"": 1,
134
 1
      ""BooleanCol"": true,
135
 1
      ""TimeSpanCol"": ""10.22:10:15.1000000"",
136
 1
      ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
137
 1
      ""DecimalCol"": 64.0021
138
 1
    }
139
 1
  ]
140
 1
}", json);
141

  
142
 1
      Assert.IsNotNull(deserializedDs);
143

  
144
 1
    }
145

  
146
    [Test]
147
    public void DeserializeMultiTableDataSet()
148
    {
149
 1
      string json = @"{
150
 1
  ""FirstTable"": [
151
 1
    {
152
 1
      ""StringCol"": ""Item Name"",
153
 1
      ""Int32Col"": 2147483647,
154
 1
      ""BooleanCol"": true,
155
 1
      ""TimeSpanCol"": ""10.22:10:15.1000000"",
156
 1
      ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
157
 1
      ""DecimalCol"": 64.0021
158
 1
    }
159
 1
  ],
160
 1
  ""SecondTable"": [
161
 1
    {
162
 1
      ""StringCol"": ""Item Name"",
163
 1
      ""Int32Col"": 2147483647,
164
 1
      ""BooleanCol"": true,
165
 1
      ""TimeSpanCol"": ""10.22:10:15.1000000"",
166
 1
      ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
167
 1
      ""DecimalCol"": 64.0021
168
 1
    }
169
 1
  ]
170
 1
}";
171

  
172
 1
      DataSet ds = JsonConvert.DeserializeObject<DataSet>(json, new IsoDateTimeConverter());
173
 1
      Assert.IsNotNull(ds);
174

  
175
 1
      Assert.AreEqual(2, ds.Tables.Count);
176
 1
      Assert.AreEqual("FirstTable", ds.Tables[0].TableName);
177
 1
      Assert.AreEqual("SecondTable", ds.Tables[1].TableName);
178

  
179
 1
      DataTable dt = ds.Tables[0];
180
 1
      Assert.AreEqual("StringCol", dt.Columns[0].ColumnName);
181
 1
      Assert.AreEqual(typeof(string), dt.Columns[0].DataType);
182
 1
      Assert.AreEqual("Int32Col", dt.Columns[1].ColumnName);
183
 1
      Assert.AreEqual(typeof(long), dt.Columns[1].DataType);
184
 1
      Assert.AreEqual("BooleanCol", dt.Columns[2].ColumnName);
185
 1
      Assert.AreEqual(typeof(bool), dt.Columns[2].DataType);
186
 1
      Assert.AreEqual("TimeSpanCol", dt.Columns[3].ColumnName);
187
 1
      Assert.AreEqual(typeof(string), dt.Columns[3].DataType);
188
 1
      Assert.AreEqual("DateTimeCol", dt.Columns[4].ColumnName);
189
 1
      Assert.AreEqual(typeof(string), dt.Columns[4].DataType);
190
 1
      Assert.AreEqual("DecimalCol", dt.Columns[5].ColumnName);
191
 1
      Assert.AreEqual(typeof(double), dt.Columns[5].DataType);
192

  
193
 1
      Assert.AreEqual(1, ds.Tables[0].Rows.Count);
194
 1
      Assert.AreEqual(1, ds.Tables[1].Rows.Count);
195
 1
    }
196

  
197
    private DataTable CreateDataTable(string dataTableName, int rows)
198
    {
199
      // create a new DataTable.
200
 2
      DataTable myTable = new DataTable(dataTableName);
201

  
202
      // create DataColumn objects of data types.
203
 2
      DataColumn colString = new DataColumn("StringCol");
204
 2
      colString.DataType = typeof(string);
205
 2
      myTable.Columns.Add(colString);
206

  
207
 2
      DataColumn colInt32 = new DataColumn("Int32Col");
208
 2
      colInt32.DataType = typeof(int);
209
 2
      myTable.Columns.Add(colInt32);
210

  
211
 2
      DataColumn colBoolean = new DataColumn("BooleanCol");
212
 2
      colBoolean.DataType = typeof(bool);
213
 2
      myTable.Columns.Add(colBoolean);
214

  
215
 2
      DataColumn colTimeSpan = new DataColumn("TimeSpanCol");
216
 2
      colTimeSpan.DataType = typeof(TimeSpan);
217
 2
      myTable.Columns.Add(colTimeSpan);
218

  
219
 2
      DataColumn colDateTime = new DataColumn("DateTimeCol");
220
 2
      colDateTime.DataType = typeof(DateTime);
221
 2
      colDateTime.DateTimeMode = DataSetDateTime.Utc;
222
 2
      myTable.Columns.Add(colDateTime);
223

  
224
 2
      DataColumn colDecimal = new DataColumn("DecimalCol");
225
 2
      colDecimal.DataType = typeof(decimal);
226
 2
      myTable.Columns.Add(colDecimal);
227

  
228
 2
      for (int i = 1; i <= rows; i++)
229
      {
230
 3
        DataRow myNewRow = myTable.NewRow();
231

  
232
 3
        myNewRow["StringCol"] = "Item Name";
233
 3
        myNewRow["Int32Col"] = i;
234
 3
        myNewRow["BooleanCol"] = true;
235
 3
        myNewRow["TimeSpanCol"] = new TimeSpan(10, 22, 10, 15, 100);
236
 3
        myNewRow["DateTimeCol"] = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc);
237
 3
        myNewRow["DecimalCol"] = 64.0021;
238
 3
        myTable.Rows.Add(myNewRow);
239
      }
240

  
241
 2
      return myTable;
242
 2
    }
243
  }
244
}
245
#endif