/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program 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 this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Collections.ObjectModel;
namespace Everest.Library.Json.Converters{
public class DataTableConverter : JavaScriptConverter
{
public DataTableConverter()
{
this._supportedTypes = new ReadOnlyCollection<Type>(new Type[] { typeof(DataTable) });
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotSupportedException();
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
DataTable table = obj as DataTable;
if (table == null)
{
throw new ArgumentException("obj");
}
IDictionary<string, object> dictionary = new Dictionary<string, object>(2);
//
if (table.ExtendedProperties["TotalCount"] != null)
{
dictionary["TitleCount"] = int.Parse(table.ExtendedProperties["TotalCount"].ToString());
}
else
{
dictionary["TitleCount"] = table.Rows.Count;
}
if (table.Rows.Count > 0)
{
DataRow[] rowArray = new DataRow[table.Rows.Count];
for (int j = 0; j < rowArray.Length; j++)
{
rowArray[j] = table.Rows[j];
}
dictionary["Rows"] = rowArray;
return dictionary;
}
dictionary["Rows"] = new string[0];
return dictionary;
}
private ReadOnlyCollection<Type> _supportedTypes;
public override IEnumerable<Type> SupportedTypes
{
get { return this._supportedTypes; }
}
}
}
|