PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Current/NetJsWire2/NetJsWire.Direct/customJsonConverter/DataRowConverter.cs

#
C# | 194 lines | 87 code | 31 blank | 76 comment | 2 complexity | 42a375619f45f1c34dc2e92f03aa9402 MD5 | raw file
Possible License(s): GPL-3.0
  1. #region Header
  2. // Copyright 2007-2011, Jerónimo Milea
  3. //
  4. // This file is part of NetJsWire.
  5. //
  6. // NetJsWire is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // NetJsWire is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with NetJsWire. If not, see <http://www.gnu.org/licenses/>.
  18. #endregion
  19. using System;
  20. using System.Data;
  21. using Newtonsoft.Json;
  22. using Newtonsoft.Json.Converters;
  23. namespace NetJsWire.Direct.customJsonConverter
  24. {
  25. class DataRowConverter : JsonConverter
  26. {
  27. /// <summary>
  28. /// Writes the JSON representation of the object.
  29. /// </summary>
  30. /// <param name = "writer">The <see cref = "JsonWriter" /> to write to.</param>
  31. /// <param name = "value">The value.</param>
  32. public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer )
  33. {
  34. var row = value as DataRow;
  35. // *** HACK: need to use root serializer to write the column value
  36. // should be fixed in next ver of JSON.NET with writer.Serialize(object)
  37. var ser = new JsonSerializer();
  38. ser.Converters.Add( new DataRowConverter() );
  39. ser.Converters.Add( new JavaScriptDateTimeConverter() );
  40. writer.WriteStartObject();
  41. foreach ( DataColumn column in row.Table.Columns )
  42. {
  43. writer.WritePropertyName( column.ColumnName );
  44. ser.Serialize( writer, row[ column ] );
  45. }
  46. writer.WriteEndObject();
  47. }
  48. /// <summary>
  49. /// Determines whether this instance can convert the specified value type.
  50. /// </summary>
  51. /// <param name = "valueType">Type of the value.</param>
  52. /// <returns>
  53. /// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
  54. /// </returns>
  55. public override bool CanConvert( Type valueType )
  56. {
  57. return typeof( DataRow ).IsAssignableFrom( valueType );
  58. }
  59. /// <summary>
  60. /// Reads the JSON representation of the object.
  61. /// </summary>
  62. /// <param name = "reader">The <see cref = "JsonReader" /> to read from.</param>
  63. /// <param name = "objectType">Type of the object.</param>
  64. /// <returns>The object value.</returns>
  65. public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
  66. {
  67. throw new NotImplementedException();
  68. }
  69. }
  70. class DataRowViewConverter : JsonConverter
  71. {
  72. /// <summary>
  73. /// Writes the JSON representation of the object.
  74. /// </summary>
  75. /// <param name = "writer">The <see cref = "JsonWriter" /> to write to.</param>
  76. /// <param name = "value">The value.</param>
  77. public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer )
  78. {
  79. var row = value as DataRowView;
  80. // *** HACK: need to use root serializer to write the column value
  81. // should be fixed in next ver of JSON.NET with writer.Serialize(object)
  82. var ser = new JsonSerializer();
  83. ser.Converters.Add( new JavaScriptDateTimeConverter() );
  84. writer.WriteStartObject();
  85. var i = 0;
  86. foreach ( DataColumn column in row.DataView.Table.Columns )
  87. {
  88. writer.WritePropertyName( column.ColumnName );
  89. ser.Serialize( writer, row[ i ] );
  90. i++;
  91. }
  92. writer.WriteEndObject();
  93. }
  94. /// <summary>
  95. /// Determines whether this instance can convert the specified value type.
  96. /// </summary>
  97. /// <param name = "valueType">Type of the value.</param>
  98. /// <returns>
  99. /// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
  100. /// </returns>
  101. public override bool CanConvert( Type valueType )
  102. {
  103. return typeof( DataRowView ).IsAssignableFrom( valueType );
  104. }
  105. /// <summary>
  106. /// Reads the JSON representation of the object.
  107. /// </summary>
  108. /// <param name = "reader">The <see cref = "JsonReader" /> to read from.</param>
  109. /// <param name = "objectType">Type of the object.</param>
  110. /// <returns>The object value.</returns>
  111. public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
  112. {
  113. throw new NotImplementedException();
  114. }
  115. }
  116. class DataRowCollectionConverter : JsonConverter
  117. {
  118. /// <summary>
  119. /// Writes the JSON representation of the object.
  120. /// </summary>
  121. /// <param name = "writer">The <see cref = "JsonWriter" /> to write to.</param>
  122. /// <param name = "dataRows">The value.</param>
  123. public override void WriteJson( JsonWriter writer, object dataRows, JsonSerializer serializer )
  124. {
  125. var rows = dataRows as DataRowCollection;
  126. var converter = new DataRowConverter();
  127. // *** HACK: need to use root serializer to write the column value
  128. // should be fixed in next ver of JSON.NET with writer.Serialize(object)
  129. var ser = new JsonSerializer();
  130. ser.Converters.Add( new JavaScriptDateTimeConverter() );
  131. writer.WriteStartArray();
  132. if ( dataRows != null )
  133. {
  134. foreach ( DataRow dataRow in rows )
  135. {
  136. converter.WriteJson( writer, dataRow, serializer );
  137. }
  138. }
  139. writer.WriteEndArray();
  140. }
  141. /// <summary>
  142. /// Determines whether this instance can convert the specified value type.
  143. /// </summary>
  144. /// <param name = "valueType">Type of the value.</param>
  145. /// <returns>
  146. /// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
  147. /// </returns>
  148. public override bool CanConvert( Type valueType )
  149. {
  150. return typeof( DataRowCollection ).IsAssignableFrom( valueType );
  151. }
  152. /// <summary>
  153. /// Reads the JSON representation of the object.
  154. /// </summary>
  155. /// <param name = "reader">The <see cref = "JsonReader" /> to read from.</param>
  156. /// <param name = "objectType">Type of the object.</param>
  157. /// <returns>The object value.</returns>
  158. public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
  159. {
  160. throw new NotImplementedException();
  161. }
  162. }
  163. }