Code Coverage Statistics for Source File
Newtonsoft.Json\Converters\DataTableConverter.cs
Symbol Coverage: 95.65% (44 of 46)
Branch Coverage: 95.24% (20 of 21)
Cyclomatic Complexity Avg: 3.60 Max:6
Code Lines: 42
Symbol Coverage Trend
View:
L | V | Source |
---|---|---|
1 |
#region License |
|
2 |
// Copyright (c) 2007 James Newton-King |
|
3 |
// |
|
4 |
// Permission is hereby granted, free of charge, to any person |
|
5 |
// obtaining a copy of this software and associated documentation |
|
6 |
// files (the "Software"), to deal in the Software without |
|
7 |
// restriction, including without limitation the rights to use, |
|
8 |
// copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9 |
// copies of the Software, and to permit persons to whom the |
|
10 |
// Software is furnished to do so, subject to the following |
|
11 |
// conditions: |
|
12 |
// |
|
13 |
// The above copyright notice and this permission notice shall be |
|
14 |
// included in all copies or substantial portions of the Software. |
|
15 |
// |
|
16 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
17 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
18 |
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
19 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
20 |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
21 |
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
22 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
23 |
// OTHER DEALINGS IN THE SOFTWARE. |
|
24 |
#endregion |
|
25 |
||
26 |
#if !SILVERLIGHT |
|
27 |
using System; |
|
28 |
using System.Data; |
|
29 |
||
30 |
namespace Newtonsoft.Json.Converters |
|
31 |
{ |
|
32 |
/// <summary> |
|
33 |
/// Converts a <see cref="DataTable"/> to and from JSON. |
|
34 |
/// </summary> |
|
35 |
public class DataTableConverter : JsonConverter |
|
36 |
{ |
|
37 |
/// <summary> |
|
38 |
/// Writes the JSON representation of the object. |
|
39 |
/// </summary> |
|
40 |
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> |
|
41 |
/// <param name="value">The value.</param> |
|
42 |
/// <param name="serializer">The calling serializer.</param> |
|
43 |
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
|
44 |
{ |
|
45 |
4 |
DataTable table = (DataTable)value;
|
46 |
||
47 |
4 |
writer.WriteStartArray();
|
48 |
||
49 |
4 |
foreach (DataRow row in table.Rows)
|
50 |
{ |
|
51 |
6 |
writer.WriteStartObject();
|
52 |
6 |
foreach (DataColumn column in row.Table.Columns)
|
53 |
{ |
|
54 |
28 |
writer.WritePropertyName(column.ColumnName);
|
55 |
28 |
serializer.Serialize(writer, row[column]);
|
56 |
} |
|
57 |
6 |
writer.WriteEndObject();
|
58 |
} |
|
59 |
||
60 |
4 |
writer.WriteEndArray();
|
61 |
4 |
}
|
62 |
||
63 |
/// <summary> |
|
64 |
/// Reads the JSON representation of the object. |
|
65 |
/// </summary> |
|
66 |
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param> |
|
67 |
/// <param name="objectType">Type of the object.</param> |
|
68 |
/// <param name="existingValue">The existing value of object being read.</param> |
|
69 |
/// <param name="serializer">The calling serializer.</param> |
|
70 |
/// <returns>The object value.</returns> |
|
71 |
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
|
72 |
{ |
|
73 |
DataTable dt; |
|
74 |
||
75 |
6 |
if (reader.TokenType == JsonToken.PropertyName)
|
76 |
{ |
|
77 |
5 |
dt = new DataTable((string)reader.Value);
|
78 |
5 |
reader.Read();
|
79 |
} |
|
80 |
else |
|
81 |
{ |
|
82 |
1 |
dt = new DataTable();
|
83 |
} |
|
84 |
||
85 |
6 |
reader.Read();
|
86 |
||
87 |
15 |
while (reader.TokenType == JsonToken.StartObject)
|
88 |
{ |
|
89 |
9 |
DataRow dr = dt.NewRow();
|
90 |
9 |
reader.Read();
|
91 |
||
92 |
47 |
while (reader.TokenType == JsonToken.PropertyName)
|
93 |
{ |
|
94 |
38 |
string columnName = (string) reader.Value;
|
95 |
||
96 |
38 |
reader.Read();
|
97 |
||
98 |
38 |
if (!dt.Columns.Contains(columnName))
|
99 |
{ |
|
100 |
28 |
Type columnType = GetColumnDataType(reader.TokenType);
|
101 |
28 |
dt.Columns.Add(new DataColumn(columnName, columnType));
|
102 |
} |
|
103 |
||
104 |
38 |
dr[columnName] = reader.Value;
|
105 |
38 |
reader.Read();
|
106 |
} |
|
107 |
||
108 |
9 |
dr.EndEdit();
|
109 |
9 |
dt.Rows.Add(dr);
|
110 |
||
111 |
9 |
reader.Read();
|
112 |
} |
|
113 |
||
114 |
6 |
reader.Read();
|
115 |
||
116 |
6 |
return dt;
|
117 |
6 |
}
|
118 |
||
119 |
private static Type GetColumnDataType(JsonToken tokenType) |
|
120 |
{ |
|
121 |
28 |
switch (tokenType)
|
122 |
{ |
|
123 |
case JsonToken.Integer: |
|
124 |
6 |
return typeof (long);
|
125 |
case JsonToken.Float: |
|
126 |
4 |
return typeof (double);
|
127 |
case JsonToken.String: |
|
128 |
case JsonToken.Null: |
|
129 |
case JsonToken.Undefined: |
|
130 |
14 |
return typeof (string);
|
131 |
case JsonToken.Boolean: |
|
132 |
4 |
return typeof (bool);
|
133 |
case JsonToken.Date: |
|
134 |
0 |
return typeof (DateTime);
|
135 |
default: |
|
136 |
0 |
throw new ArgumentOutOfRangeException();
|
137 |
} |
|
138 |
28 |
}
|
139 |
||
140 |
/// <summary> |
|
141 |
/// Determines whether this instance can convert the specified value type. |
|
142 |
/// </summary> |
|
143 |
/// <param name="valueType">Type of the value.</param> |
|
144 |
/// <returns> |
|
145 |
/// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>. |
|
146 |
/// </returns> |
|
147 |
public override bool CanConvert(Type valueType) |
|
148 |
{ |
|
149 |
293 |
return (valueType == typeof(DataTable));
|
150 |
293 |
}
|
151 |
} |
|
152 |
} |
|
153 |
#endif |