All files
[pithos-ms-client] / trunk / Libraries / Json40r2 / Source / Src / Newtonsoft.Json.Tests / Converters / DataSetConverterTests.cs
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       DataSet dataSet = new DataSet("dataSet");
19       dataSet.Namespace = "NetFrameWork";
20       DataTable table = new DataTable();
21       DataColumn idColumn = new DataColumn("id", typeof(int));
22       idColumn.AutoIncrement = true;
23
24       DataColumn itemColumn = new DataColumn("item");
25       table.Columns.Add(idColumn);
26       table.Columns.Add(itemColumn);
27       dataSet.Tables.Add(table);
28
29       for (int i = 0; i < 2; i++)
30       {
31         DataRow newRow = table.NewRow();
32         newRow["item"] = "item " + i;
33         table.Rows.Add(newRow);
34       }
35
36       dataSet.AcceptChanges();
37
38       string json = JsonConvert.SerializeObject(dataSet, Formatting.Indented);
39       
40       Assert.AreEqual(@"{
41   ""Table1"": [
42     {
43       ""id"": 0,
44       ""item"": ""item 0""
45     },
46     {
47       ""id"": 1,
48       ""item"": ""item 1""
49     }
50   ]
51 }", json);
52
53       DataSet deserializedDataSet = JsonConvert.DeserializeObject<DataSet>(json);
54       Assert.IsNotNull(deserializedDataSet);
55
56       Assert.AreEqual(1, deserializedDataSet.Tables.Count);
57
58       DataTable dt = deserializedDataSet.Tables[0];
59
60       Assert.AreEqual("Table1", dt.TableName);
61       Assert.AreEqual(2, dt.Columns.Count);
62       Assert.AreEqual("id", dt.Columns[0].ColumnName);
63       Assert.AreEqual(typeof(long), dt.Columns[0].DataType);
64       Assert.AreEqual("item", dt.Columns[1].ColumnName);
65       Assert.AreEqual(typeof(string), dt.Columns[1].DataType);
66
67       Assert.AreEqual(2, dt.Rows.Count);
68     }
69
70     [Test]
71     public void SerializeMultiTableDataSet()
72     {
73       DataSet ds = new DataSet();
74       ds.Tables.Add(CreateDataTable("FirstTable", 2));
75       ds.Tables.Add(CreateDataTable("SecondTable", 1));
76
77       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       DataSet deserializedDs = JsonConvert.DeserializeObject<DataSet>(json, new IsoDateTimeConverter());
110
111       Assert.AreEqual(@"{
112   ""FirstTable"": [
113     {
114       ""StringCol"": ""Item Name"",
115       ""Int32Col"": 1,
116       ""BooleanCol"": true,
117       ""TimeSpanCol"": ""10.22:10:15.1000000"",
118       ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
119       ""DecimalCol"": 64.0021
120     },
121     {
122       ""StringCol"": ""Item Name"",
123       ""Int32Col"": 2,
124       ""BooleanCol"": true,
125       ""TimeSpanCol"": ""10.22:10:15.1000000"",
126       ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
127       ""DecimalCol"": 64.0021
128     }
129   ],
130   ""SecondTable"": [
131     {
132       ""StringCol"": ""Item Name"",
133       ""Int32Col"": 1,
134       ""BooleanCol"": true,
135       ""TimeSpanCol"": ""10.22:10:15.1000000"",
136       ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
137       ""DecimalCol"": 64.0021
138     }
139   ]
140 }", json);
141
142       Assert.IsNotNull(deserializedDs);
143
144     }
145
146     [Test]
147     public void DeserializeMultiTableDataSet()
148     {
149       string json = @"{
150   ""FirstTable"": [
151     {
152       ""StringCol"": ""Item Name"",
153       ""Int32Col"": 2147483647,
154       ""BooleanCol"": true,
155       ""TimeSpanCol"": ""10.22:10:15.1000000"",
156       ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
157       ""DecimalCol"": 64.0021
158     }
159   ],
160   ""SecondTable"": [
161     {
162       ""StringCol"": ""Item Name"",
163       ""Int32Col"": 2147483647,
164       ""BooleanCol"": true,
165       ""TimeSpanCol"": ""10.22:10:15.1000000"",
166       ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
167       ""DecimalCol"": 64.0021
168     }
169   ]
170 }";
171
172       DataSet ds = JsonConvert.DeserializeObject<DataSet>(json, new IsoDateTimeConverter());
173       Assert.IsNotNull(ds);
174
175       Assert.AreEqual(2, ds.Tables.Count);
176       Assert.AreEqual("FirstTable", ds.Tables[0].TableName);
177       Assert.AreEqual("SecondTable", ds.Tables[1].TableName);
178
179       DataTable dt = ds.Tables[0];
180       Assert.AreEqual("StringCol", dt.Columns[0].ColumnName);
181       Assert.AreEqual(typeof(string), dt.Columns[0].DataType);
182       Assert.AreEqual("Int32Col", dt.Columns[1].ColumnName);
183       Assert.AreEqual(typeof(long), dt.Columns[1].DataType);
184       Assert.AreEqual("BooleanCol", dt.Columns[2].ColumnName);
185       Assert.AreEqual(typeof(bool), dt.Columns[2].DataType);
186       Assert.AreEqual("TimeSpanCol", dt.Columns[3].ColumnName);
187       Assert.AreEqual(typeof(string), dt.Columns[3].DataType);
188       Assert.AreEqual("DateTimeCol", dt.Columns[4].ColumnName);
189       Assert.AreEqual(typeof(string), dt.Columns[4].DataType);
190       Assert.AreEqual("DecimalCol", dt.Columns[5].ColumnName);
191       Assert.AreEqual(typeof(double), dt.Columns[5].DataType);
192
193       Assert.AreEqual(1, ds.Tables[0].Rows.Count);
194       Assert.AreEqual(1, ds.Tables[1].Rows.Count);
195     }
196
197     private DataTable CreateDataTable(string dataTableName, int rows)
198     {
199       // create a new DataTable.
200       DataTable myTable = new DataTable(dataTableName);
201
202       // create DataColumn objects of data types.
203       DataColumn colString = new DataColumn("StringCol");
204       colString.DataType = typeof(string);
205       myTable.Columns.Add(colString);
206
207       DataColumn colInt32 = new DataColumn("Int32Col");
208       colInt32.DataType = typeof(int);
209       myTable.Columns.Add(colInt32);
210
211       DataColumn colBoolean = new DataColumn("BooleanCol");
212       colBoolean.DataType = typeof(bool);
213       myTable.Columns.Add(colBoolean);
214
215       DataColumn colTimeSpan = new DataColumn("TimeSpanCol");
216       colTimeSpan.DataType = typeof(TimeSpan);
217       myTable.Columns.Add(colTimeSpan);
218
219       DataColumn colDateTime = new DataColumn("DateTimeCol");
220       colDateTime.DataType = typeof(DateTime);
221       colDateTime.DateTimeMode = DataSetDateTime.Utc;
222       myTable.Columns.Add(colDateTime);
223
224       DataColumn colDecimal = new DataColumn("DecimalCol");
225       colDecimal.DataType = typeof(decimal);
226       myTable.Columns.Add(colDecimal);
227
228       for (int i = 1; i <= rows; i++)
229       {
230         DataRow myNewRow = myTable.NewRow();
231
232         myNewRow["StringCol"] = "Item Name";
233         myNewRow["Int32Col"] = i;
234         myNewRow["BooleanCol"] = true;
235         myNewRow["TimeSpanCol"] = new TimeSpan(10, 22, 10, 15, 100);
236         myNewRow["DateTimeCol"] = new DateTime(2000, 12, 29, 0, 0, 0, DateTimeKind.Utc);
237         myNewRow["DecimalCol"] = 64.0021;
238         myTable.Rows.Add(myNewRow);
239       }
240
241       return myTable;
242     }
243
244     public class DataSetAndTableTestClass
245     {
246       public string Before { get; set; }
247       public DataSet Set { get; set; }
248       public string Middle { get; set; }
249       public DataTable Table { get; set; }
250       public string After { get; set; }
251     }
252
253     [Test]
254     public void Blah()
255     {
256       DataSet ds = new DataSet();
257       ds.Tables.Add(CreateDataTable("FirstTable", 2));
258       ds.Tables.Add(CreateDataTable("SecondTable", 1));
259
260       DataSetAndTableTestClass c = new DataSetAndTableTestClass
261         {
262           Before = "Before",
263           Set = ds,
264           Middle = "Middle",
265           Table = CreateDataTable("LoneTable", 2),
266           After = "After"
267         };
268
269       string json = JsonConvert.SerializeObject(c, Formatting.Indented, new IsoDateTimeConverter());
270
271       Assert.AreEqual(@"{
272   ""Before"": ""Before"",
273   ""Set"": {
274     ""FirstTable"": [
275       {
276         ""StringCol"": ""Item Name"",
277         ""Int32Col"": 1,
278         ""BooleanCol"": true,
279         ""TimeSpanCol"": ""10.22:10:15.1000000"",
280         ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
281         ""DecimalCol"": 64.0021
282       },
283       {
284         ""StringCol"": ""Item Name"",
285         ""Int32Col"": 2,
286         ""BooleanCol"": true,
287         ""TimeSpanCol"": ""10.22:10:15.1000000"",
288         ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
289         ""DecimalCol"": 64.0021
290       }
291     ],
292     ""SecondTable"": [
293       {
294         ""StringCol"": ""Item Name"",
295         ""Int32Col"": 1,
296         ""BooleanCol"": true,
297         ""TimeSpanCol"": ""10.22:10:15.1000000"",
298         ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
299         ""DecimalCol"": 64.0021
300       }
301     ]
302   },
303   ""Middle"": ""Middle"",
304   ""Table"": [
305     {
306       ""StringCol"": ""Item Name"",
307       ""Int32Col"": 1,
308       ""BooleanCol"": true,
309       ""TimeSpanCol"": ""10.22:10:15.1000000"",
310       ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
311       ""DecimalCol"": 64.0021
312     },
313     {
314       ""StringCol"": ""Item Name"",
315       ""Int32Col"": 2,
316       ""BooleanCol"": true,
317       ""TimeSpanCol"": ""10.22:10:15.1000000"",
318       ""DateTimeCol"": ""2000-12-29T00:00:00Z"",
319       ""DecimalCol"": 64.0021
320     }
321   ],
322   ""After"": ""After""
323 }", json);
324
325       DataSetAndTableTestClass c2 = JsonConvert.DeserializeObject<DataSetAndTableTestClass>(json, new IsoDateTimeConverter());
326
327       Assert.AreEqual(c.Before, c2.Before);
328       Assert.AreEqual(c.Set.Tables.Count, c2.Set.Tables.Count);
329       Assert.AreEqual(c.Middle, c2.Middle);
330       Assert.AreEqual(c.Table.Rows.Count, c2.Table.Rows.Count);
331       Assert.AreEqual(c.After, c2.After);
332     }
333   }
334 }
335 #endif