/Current/NetJsWire2/NetJsWire.Direct/customJsonConverter/DataRowConverter.cs
# · C# · 194 lines · 87 code · 31 blank · 76 comment · 2 complexity · 42a375619f45f1c34dc2e92f03aa9402 MD5 · raw file
- #region Header
- // Copyright 2007-2011, Jerónimo Milea
- //
- // This file is part of NetJsWire.
- //
- // NetJsWire is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // NetJsWire is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with NetJsWire. If not, see <http://www.gnu.org/licenses/>.
- #endregion
-
- using System;
- using System.Data;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Converters;
-
- namespace NetJsWire.Direct.customJsonConverter
- {
- class DataRowConverter : JsonConverter
- {
- /// <summary>
- /// Writes the JSON representation of the object.
- /// </summary>
- /// <param name = "writer">The <see cref = "JsonWriter" /> to write to.</param>
- /// <param name = "value">The value.</param>
- public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer )
- {
- var row = value as DataRow;
-
-
-
- // *** HACK: need to use root serializer to write the column value
- // should be fixed in next ver of JSON.NET with writer.Serialize(object)
- var ser = new JsonSerializer();
-
- ser.Converters.Add( new DataRowConverter() );
- ser.Converters.Add( new JavaScriptDateTimeConverter() );
- writer.WriteStartObject();
- foreach ( DataColumn column in row.Table.Columns )
- {
- writer.WritePropertyName( column.ColumnName );
- ser.Serialize( writer, row[ column ] );
- }
- writer.WriteEndObject();
-
- }
-
-
- /// <summary>
- /// Determines whether this instance can convert the specified value type.
- /// </summary>
- /// <param name = "valueType">Type of the value.</param>
- /// <returns>
- /// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
- /// </returns>
- public override bool CanConvert( Type valueType )
- {
- return typeof( DataRow ).IsAssignableFrom( valueType );
- }
-
- /// <summary>
- /// Reads the JSON representation of the object.
- /// </summary>
- /// <param name = "reader">The <see cref = "JsonReader" /> to read from.</param>
- /// <param name = "objectType">Type of the object.</param>
- /// <returns>The object value.</returns>
- public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
- {
- throw new NotImplementedException();
- }
- }
-
- class DataRowViewConverter : JsonConverter
- {
- /// <summary>
- /// Writes the JSON representation of the object.
- /// </summary>
- /// <param name = "writer">The <see cref = "JsonWriter" /> to write to.</param>
- /// <param name = "value">The value.</param>
- public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer )
- {
- var row = value as DataRowView;
-
-
-
- // *** HACK: need to use root serializer to write the column value
- // should be fixed in next ver of JSON.NET with writer.Serialize(object)
- var ser = new JsonSerializer();
-
- ser.Converters.Add( new JavaScriptDateTimeConverter() );
-
- writer.WriteStartObject();
- var i = 0;
- foreach ( DataColumn column in row.DataView.Table.Columns )
- {
- writer.WritePropertyName( column.ColumnName );
-
- ser.Serialize( writer, row[ i ] );
- i++;
- }
- writer.WriteEndObject();
-
- }
-
-
- /// <summary>
- /// Determines whether this instance can convert the specified value type.
- /// </summary>
- /// <param name = "valueType">Type of the value.</param>
- /// <returns>
- /// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
- /// </returns>
- public override bool CanConvert( Type valueType )
- {
- return typeof( DataRowView ).IsAssignableFrom( valueType );
- }
-
- /// <summary>
- /// Reads the JSON representation of the object.
- /// </summary>
- /// <param name = "reader">The <see cref = "JsonReader" /> to read from.</param>
- /// <param name = "objectType">Type of the object.</param>
- /// <returns>The object value.</returns>
- public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
- {
- throw new NotImplementedException();
- }
- }
-
- class DataRowCollectionConverter : JsonConverter
- {
- /// <summary>
- /// Writes the JSON representation of the object.
- /// </summary>
- /// <param name = "writer">The <see cref = "JsonWriter" /> to write to.</param>
- /// <param name = "dataRows">The value.</param>
- public override void WriteJson( JsonWriter writer, object dataRows, JsonSerializer serializer )
- {
- var rows = dataRows as DataRowCollection;
- var converter = new DataRowConverter();
-
-
-
-
- // *** HACK: need to use root serializer to write the column value
- // should be fixed in next ver of JSON.NET with writer.Serialize(object)
- var ser = new JsonSerializer();
-
- ser.Converters.Add( new JavaScriptDateTimeConverter() );
- writer.WriteStartArray();
- if ( dataRows != null )
- {
- foreach ( DataRow dataRow in rows )
- {
- converter.WriteJson( writer, dataRow, serializer );
- }
- }
- writer.WriteEndArray();
-
- }
-
-
- /// <summary>
- /// Determines whether this instance can convert the specified value type.
- /// </summary>
- /// <param name = "valueType">Type of the value.</param>
- /// <returns>
- /// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
- /// </returns>
- public override bool CanConvert( Type valueType )
- {
- return typeof( DataRowCollection ).IsAssignableFrom( valueType );
- }
-
- /// <summary>
- /// Reads the JSON representation of the object.
- /// </summary>
- /// <param name = "reader">The <see cref = "JsonReader" /> to read from.</param>
- /// <param name = "objectType">Type of the object.</param>
- /// <returns>The object value.</returns>
- public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
- {
- throw new NotImplementedException();
- }
- }
- }