Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Converters\DataSetConverter.cs

Symbol Coverage: 100.00% (21 of 21)

Branch Coverage: 100.00% (9 of 9)

Cyclomatic Complexity Avg: 1.75 Max:3

Code Lines: 19


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="DataSet"/> to and from JSON.
34
  /// </summary>
35
  public class DataSetConverter : 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
 3
      DataSet dataSet = (DataSet)value;
46

  
47
 3
      DataTableConverter converter = new DataTableConverter();
48

  
49
 3
      writer.WriteStartObject();
50

  
51
 3
      foreach (DataTable table in dataSet.Tables)
52
      {
53
 3
        writer.WritePropertyName(table.TableName);
54
        
55
 3
        converter.WriteJson(writer, table, serializer);
56
      }
57

  
58
 3
      writer.WriteEndObject();
59
 3
    }
60

  
61
    /// <summary>
62
    /// Reads the JSON representation of the object.
63
    /// </summary>
64
    /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
65
    /// <param name="objectType">Type of the object.</param>
66
    /// <param name="existingValue">The existing value of object being read.</param>
67
    /// <param name="serializer">The calling serializer.</param>
68
    /// <returns>The object value.</returns>
69
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
70
    {
71
 3
      DataSet ds = new DataSet();
72

  
73
 3
      DataTableConverter converter = new DataTableConverter();
74

  
75
 3
      reader.Read();
76

  
77
 8
      while (reader.TokenType == JsonToken.PropertyName)
78
      {
79
 5
        DataTable dt = (DataTable)converter.ReadJson(reader, typeof (DataTable), null, serializer);
80
 5
        ds.Tables.Add(dt);
81
      }
82

  
83
 3
      reader.Read();
84

  
85
 3
      return ds;
86
 3
    }
87

  
88
    /// <summary>
89
    /// Determines whether this instance can convert the specified value type.
90
    /// </summary>
91
    /// <param name="valueType">Type of the value.</param>
92
    /// <returns>
93
    /// 	<c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
94
    /// </returns>
95
    public override bool CanConvert(Type valueType)
96
    {
97
 294
      return (valueType == typeof(DataSet));
98
 294
    }
99
  }
100
}
101
#endif