~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Converters/DataTableConverter.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 || NETFX_CORE || PORTABLE)
 
27
using System;
 
28
using System.Data;
 
29
using Newtonsoft.Json.Serialization;
 
30
 
 
31
namespace Newtonsoft.Json.Converters
 
32
{
 
33
  /// <summary>
 
34
  /// Converts a <see cref="DataTable"/> to and from JSON.
 
35
  /// </summary>
 
36
  public class DataTableConverter : JsonConverter
 
37
  {
 
38
    /// <summary>
 
39
    /// Writes the JSON representation of the object.
 
40
    /// </summary>
 
41
    /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
 
42
    /// <param name="value">The value.</param>
 
43
    /// <param name="serializer">The calling serializer.</param>
 
44
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 
45
    {
 
46
      DataTable table = (DataTable)value;
 
47
      DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;
 
48
 
 
49
      writer.WriteStartArray();
 
50
 
 
51
      foreach (DataRow row in table.Rows)
 
52
      {
 
53
        writer.WriteStartObject();
 
54
        foreach (DataColumn column in row.Table.Columns)
 
55
        {
 
56
          if (serializer.NullValueHandling == NullValueHandling.Ignore && (row[column] == null || row[column] == DBNull.Value))
 
57
            continue;
 
58
 
 
59
          writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(column.ColumnName) : column.ColumnName);
 
60
          serializer.Serialize(writer, row[column]);
 
61
        }
 
62
        writer.WriteEndObject();
 
63
      }
 
64
 
 
65
      writer.WriteEndArray();
 
66
    }
 
67
 
 
68
    /// <summary>
 
69
    /// Reads the JSON representation of the object.
 
70
    /// </summary>
 
71
    /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
 
72
    /// <param name="objectType">Type of the object.</param>
 
73
    /// <param name="existingValue">The existing value of object being read.</param>
 
74
    /// <param name="serializer">The calling serializer.</param>
 
75
    /// <returns>The object value.</returns>
 
76
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 
77
    {
 
78
      DataTable dt;
 
79
 
 
80
      if (reader.TokenType == JsonToken.PropertyName)
 
81
      {
 
82
        dt = new DataTable((string)reader.Value);
 
83
        reader.Read();
 
84
      }
 
85
      else
 
86
      {
 
87
        dt = new DataTable();
 
88
      }
 
89
 
 
90
      reader.Read();
 
91
 
 
92
      while (reader.TokenType == JsonToken.StartObject)
 
93
      {
 
94
        DataRow dr = dt.NewRow();
 
95
        reader.Read();
 
96
 
 
97
        while (reader.TokenType == JsonToken.PropertyName)
 
98
        {
 
99
          string columnName = (string)reader.Value;
 
100
 
 
101
          reader.Read();
 
102
 
 
103
          if (!dt.Columns.Contains(columnName))
 
104
          {
 
105
            Type columnType = GetColumnDataType(reader.TokenType);
 
106
            dt.Columns.Add(new DataColumn(columnName, columnType));
 
107
          }
 
108
 
 
109
          dr[columnName] = reader.Value ?? DBNull.Value;
 
110
          reader.Read();
 
111
        }
 
112
 
 
113
        dr.EndEdit();
 
114
        dt.Rows.Add(dr);
 
115
 
 
116
        reader.Read();
 
117
      }
 
118
 
 
119
      return dt;
 
120
    }
 
121
 
 
122
    private static Type GetColumnDataType(JsonToken tokenType)
 
123
    {
 
124
      switch (tokenType)
 
125
      {
 
126
        case JsonToken.Integer:
 
127
          return typeof (long);
 
128
        case JsonToken.Float:
 
129
          return typeof (double);
 
130
        case JsonToken.String:
 
131
        case JsonToken.Null:
 
132
        case JsonToken.Undefined:
 
133
          return typeof (string);
 
134
        case JsonToken.Boolean:
 
135
          return typeof (bool);
 
136
        case JsonToken.Date:
 
137
          return typeof (DateTime);
 
138
        default:
 
139
          throw new ArgumentOutOfRangeException();
 
140
      }
 
141
    }
 
142
 
 
143
    /// <summary>
 
144
    /// Determines whether this instance can convert the specified value type.
 
145
    /// </summary>
 
146
    /// <param name="valueType">Type of the value.</param>
 
147
    /// <returns>
 
148
    ///         <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
 
149
    /// </returns>
 
150
    public override bool CanConvert(Type valueType)
 
151
    {
 
152
      return (valueType == typeof(DataTable));
 
153
    }
 
154
  }
 
155
}
 
156
#endif
 
 
b'\\ No newline at end of file'