converter.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Reflection;
using System.Globalization;
using System.Collections;
using System.Text;
namespace CTI.UI
{
public class Converter
{
public static void WriteDataRow(StringBuilder sb, DataRow row)
{
sb.Append("{");
foreach (DataColumn column in row.Table.Columns)
{
sb.AppendFormat("\"{0}\":", column.ColumnName);
WriteValue(sb, row[column]);
sb.Append(",");
}
// Remove the trailing comma.
if (row.Table.Columns.Count > 0)
{
--sb.Length;
}
sb.Append("}");
}
public static void WriteDataSet(StringBuilder sb, DataSet ds)
{
sb.Append("{\"Tables\":{");
foreach (DataTable table in ds.Tables)
{
sb.AppendFormat("\"{0}\":", table.TableName);
WriteDataTable(sb, table);
sb.Append(",");
}
// Remove the trailing comma.
if (ds.Tables.Count > 0)
{
--sb.Length;
}
sb.Append("}}");
}
public static void WriteDataTable(StringBuilder sb, DataTable table)
{
sb.Append("[");
foreach (DataRow row in table.Rows)
{
WriteDataRow(sb, row);
sb.Append(",");
}
// Remove the trailing comma.
if (table.Rows.Count > 0)
{
--sb.Length;
}
sb.Append("]");
}
public static void WriteEnumerable(StringBuilder sb, IEnumerable e)
{
bool hasItems = false;
sb.Append("[");
foreach (object val in e)
{
WriteValue(sb, val);
sb.Append(",");
hasItems = true;
}
// Remove the trailing comma.
if (hasItems)
{
--sb.Length;
}
sb.Append("]");
}
public static void WriteHashtable(StringBuilder sb, Hashtable e)
{
bool hasItems = false;
sb.Append("{");
foreach (string key in e.Keys)
{
sb.AppendFormat("\"{0}\":", key.ToLower());
WriteValue(sb, e[key]);
sb.Append(",");
hasItems = true;
}
// Remove the trailing comma.
if (hasItems)
{
--sb.Length;
}
sb.Append("}");
}
public static void WriteObject(StringBuilder sb, object o)
{
MemberInfo[] members = o.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public);
sb.Append("{");
bool hasMembers = false;
foreach (MemberInfo member in members)
{
bool hasValue = false;
object val = null;
if ((member.MemberType & MemberTypes.Field) == MemberTypes.Field)
{
FieldInfo field = (FieldInfo)member;
val = field.GetValue(o);
hasValue = true;
}
else if ((member.MemberType & MemberTypes.Property) == MemberTypes.Property)
{
PropertyInfo property = (PropertyInfo)member;
if (property.CanRead && property.GetIndexParameters().Length == 0)
{
val = property.GetValue(o, null);
hasValue = true;
}
}
if (hasValue)
{
sb.Append("\"");
sb.Append(member.Name);
sb.Append("\":");
WriteValue(sb, val);
sb.Append(",");
hasMembers = true;
}
}
if (hasMembers)
{
--sb.Length;
}
sb.Append("}");
}
public static void WriteString(StringBuilder sb, string s)
{
sb.Append("\"");
foreach (char c in s)
{
switch (c)
{
case '\"':
sb.Append("\\\"");
break;
case '\\':
sb.Append("\\\\");
break;
case '\b':
sb.Append("\\b");
break;
case '\f':
sb.Append("\\f");
break;
case '\n':
sb.Append("\\n");
break;
case '\r':
sb.Append("\\r");
break;
case '\t':
sb.Append("\\t");
break;
default:
int i = (int)c;
if (i < 32 || i > 127)
{
sb.AppendFormat("\\u{0:X04}", i);
}
else
{
sb.Append(c);
}
break;
}
}
sb.Append("\"");
}
public static void WriteValue(StringBuilder sb, object val)
{
if (val == null || val == System.DBNull.Value)
{
sb.Append("null");
}
else if (val is string || val is Guid)
{
WriteString(sb, val.ToString());
}
else if (val is bool)
{
sb.Append(val.ToString().ToLower());
}
else if (val is double ||
val is float ||
val is long ||
val is int ||
val is short ||
val is byte ||
val is decimal)
{
sb.AppendFormat(CultureInfo.InvariantCulture.NumberFormat, "{0}", val);
}
else if (val.GetType().IsEnum)
{
sb.Append((int)val);
}
else if (val is DateTime)
{
sb.Append("new Date(\"");
sb.Append(((DateTime)val).ToString("MMMM, d yyyy HH:mm:ss", new CultureInfo("en-US", false).DateTimeFormat));
sb.Append("\")");
}
else if (val is DataSet)
{
WriteDataSet(sb, val as DataSet);
}
else if (val is DataTable)
{
WriteDataTable(sb, val as DataTable);
}
else if (val is DataRow)
{
WriteDataRow(sb, val as DataRow);
}
else if (val is Hashtable)
{
WriteHashtable(sb, val as Hashtable);
}
else if (val is IEnumerable)
{
WriteEnumerable(sb, val as IEnumerable);
}
else
{
WriteObject(sb, val);
}
}
public static string Convert2Json(object o)
{
StringBuilder sb = new StringBuilder();
WriteValue(sb, o);
return sb.ToString();
}
}
}