一种简单,轻量,灵活的C#对象转Json对象的方案
简单,是因为只有一个类
轻量,是因为整个类代码只有300行
灵活,是因为扩展方式只需要继承重写某个方法即可
首先我将这个类称之为JsonBuilder,我希望它以StringBuilder的方式来实现Json字符串的转换
public class JsonBuilder { protected StringBuilder Buff = new StringBuilder(4096);//字符缓冲区 public string ToJsonString(object obj) { ....... return Buff.ToString(); } ....... }
然后我为希望为每一个基础类型单独完成一个方法,并且方法可以被重写
protected virtual void AppendByte(Byte value) protected virtual void AppendDecimal(Decimal value) protected virtual void AppendInt16(Int16 value) protected virtual void AppendInt32(Int32 value) protected virtual void AppendInt64(Int64 value) protected virtual void AppendSByte(SByte value) protected virtual void AppendUInt16(UInt16 value) protected virtual void AppendUInt32(UInt32 value) protected virtual void AppendUInt64(UInt64 value) protected virtual void AppendBoolean(Boolean value) protected virtual void AppendChar(Char value) protected virtual void AppendString(String value) protected virtual void AppendDateTime(DateTime value) protected virtual void AppendGuid(Guid value) protected virtual void AppendDouble(Double value) protected virtual void AppendSingle(Single value) protected virtual void AppendEnum(Enum value)
为了使子类重写时更方便,我将数字类型合并为一个
protected virtual void AppendNumber(IConvertible number)
但仍然保留原有方法,只是原有方法直接调用AppendNumber,就像这样
protected virtual void AppendByte(Byte value) { AppendNumber(value); } protected virtual void AppendDecimal(Decimal value) { AppendNumber(value); } protected virtual void AppendInt16(Int16 value) { AppendNumber(value); } protected virtual void AppendInt32(Int32 value) { AppendNumber(value); } protected virtual void AppendInt64(Int64 value) { AppendNumber(value); } protected virtual void AppendSByte(SByte value) { AppendNumber(value); } protected virtual void AppendUInt16(UInt16 value) { AppendNumber(value); } protected virtual void AppendUInt32(UInt32 value) { AppendNumber(value); } protected virtual void AppendUInt64(UInt64 value) { AppendNumber(value); } protected virtual void AppendDouble(Double value) { AppendNumber(value); } protected virtual void AppendSingle(Single value) { AppendNumber(value); }
这样的好处是我可以在子类中灵活的选择重写全部的数字类型,还是只重写某种特定类型
然后接着,我需要完成一些已知类型的转换方法,比如数组,集合,字典,数据表等等
protected virtual void AppendArray(IEnumerable array)//数组,集合 protected virtual void AppendJson(IDictionary dict)//字典 protected virtual void AppendDataSet(DataSet dataset)//数据表集 protected virtual void AppendDataTable(DataTable table)//单表 protected virtual void AppendDataView(DataView view)//表视图
ps:这些方法,单个实现都不困难,限于篇幅问题,这里就不介绍了,最后会放出完整代码
已知类型处理完后,再添加一个对未知类型的处理方法,这里我用的是最基本的反射
protected virtual void AppendOther(object obj) { Type t = obj.GetType(); Buff.Append('{'); string fix = ""; foreach (var p in t.GetProperties()) { if (p.CanRead) {
Buff.Append(fix); AppendKey(p.Name, false); object value = p.GetValue(obj, null); AppendObject(value); fix = ","; } } Buff.Append('}'); }
实际上有2个方法是目前为止不存在的
现在我们把他加上
/// <summary> 追加Key /// </summary> /// <param name="key"></param> /// <param name="escape">key中是否有(引号,回车,制表符等)特殊字符,需要转义</param> protected virtual void AppendKey(string key, bool escape) { if (escape) { AppendString(key); } else { Buff.Append('"'); Buff.Append(key); Buff.Append('"'); } Buff.Append(':'); } private Dictionary<object, object> _LoopObject = new Dictionary<object, object>();//循环引用对象缓存区 //泛对象 protected void AppendObject(object obj) { if (obj == null) Buff.Append("null");else if (obj is String) AppendString((String)obj); else if (obj is Int32) AppendInt32((Int32)obj); else if (obj is Boolean) AppendBoolean((Boolean)obj); else if (obj is DateTime) AppendDateTime((DateTime)obj); else if (obj is Double) AppendDouble((Double)obj); else if (obj is Enum) AppendEnum((Enum)obj); else if (obj is Decimal) AppendDecimal((Decimal)obj) ; else if (obj is Char) AppendChar((Char)obj); else if (obj is Single) AppendSingle((Single)obj); else if (obj is Guid) AppendGuid((Guid)obj); else if (obj is Byte) AppendByte((Byte)obj); else if (obj is Int16) AppendInt16((Int16)obj); else if (obj is Int64) AppendInt64((Int64)obj); else if (obj is SByte) AppendSByte((SByte)obj); else if (obj is UInt32) AppendUInt32((UInt32)obj); else if (obj is UInt64) AppendUInt64((UInt64)obj); else if (_LoopObject.ContainsKey(obj) == false) { _LoopObject.Add(obj, null); if (obj is IDictionary) AppendJson((IDictionary)obj); else if (obj is IEnumerable) AppendArray((IEnumerable)obj);
else if (obj is DataSet) AppendDataSet((DataSet)obj);
else if (obj is DataTable) AppendDataTable((DataTable)obj);
else if (obj is DataView) AppendDataView((DataView)obj); else AppendOther(obj); _LoopObject.Remove(obj); } else { Buff.Append("undefined"); } }
这2个方法都比较好理解,
一个是用来处理Key的,这里预留了一个参数escape,是为了性能的一些考虑,比如反射时的属性名称,这个是绝对不可能出现一些特殊符号的,所以可以直接作为Json的Key使用
另一个方法是用来作为泛对象(不是泛型对象)的入口方法,所有对象通过这个方法都可以找到对应的处理方法
还有一个对象_LoopObject,这个对象是为了解决循环引用的问题的,比如常用的对象Page(当然没有人会把这个对象转为Json,这里只是用来做说明)中就有一个Page的属性,指向this,如果没有额外的处理,解析将会进入一个循环递归,直到栈溢出(目前已知的几个第三方组件对这个情况的支持都不好,都会抛出异常,这个以后的文章会详细说明),而我这里做的处理是将这种无法解析循环引用对象都返回undefined,也正好可以区别于空对象的null
完整代码
using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Text; namespace blqw { /// <summary> 用于将C#转换为Json字符串 /// </summary> public class JsonBuilder { private Dictionary<object, object> _LoopObject = new Dictionary<object, object>();//循环引用对象缓存区 protected StringBuilder Buff = new StringBuilder(4096);//字符缓冲区 public string ToJsonString(object obj) { Buff.Length = 0; AppendObject(obj); return Buff.ToString(); } //泛对象 protected void AppendObject(object obj) { if (obj == null) Buff.Append("null"); else if (obj is String) AppendString((String)obj); else if (obj is Int32) AppendInt32((Int32)obj); else if (obj is Boolean) AppendBoolean((Boolean)obj); else if (obj is DateTime) AppendDateTime((DateTime)obj); else if (obj is Double) AppendDouble((Double)obj); else if (obj is Enum) AppendEnum((Enum)obj); else if (obj is Decimal) AppendDecimal((Decimal)obj); else if (obj is Char) AppendChar((Char)obj); else if (obj is Single) AppendSingle((Single)obj); else if (obj is Guid) AppendGuid((Guid)obj); else if (obj is Byte) AppendByte((Byte)obj); else if (obj is Int16) AppendInt16((Int16)obj); else if (obj is Int64) AppendInt64((Int64)obj); else if (obj is SByte) AppendSByte((SByte)obj); else if (obj is UInt32) AppendUInt32((UInt32)obj); else if (obj is UInt64) AppendUInt64((UInt64)obj); else if (_LoopObject.ContainsKey(obj) == false) { _LoopObject.Add(obj, null); if (obj is IDictionary) AppendJson((IDictionary)obj); else if (obj is IEnumerable) AppendArray((IEnumerable)obj); else if (obj is DataSet) AppendDataSet((DataSet)obj); else if (obj is DataTable) AppendDataTable((DataTable)obj); else if (obj is DataView) AppendDataView((DataView)obj); else AppendOther(obj); _LoopObject.Remove(obj); } else { Buff.Append("undefined"); } } protected virtual void AppendOther(object obj) { Type t = obj.GetType(); Buff.Append('{'); string fix = ""; foreach (var p in t.GetProperties()) { if (p.CanRead) { Buff.Append(fix); AppendKey(p.Name, false); object value = p.GetValue(obj, null); AppendObject(value); fix = ","; } } Buff.Append('}'); } /// <summary> " /// </summary> public const char Quot = '"'; /// <summary> : /// </summary> public const char Colon = ':'; /// <summary> , /// </summary> public const char Comma = ','; /// <summary> 追加Key /// </summary> /// <param name="key"></param> /// <param name="escape">key中是否有(引号,回车,制表符等)特殊字符,需要转义</param> protected virtual void AppendKey(string key, bool escape) { if (escape) { AppendString(key); } else { Buff.Append(Quot); Buff.Append(key); Buff.Append(Quot); } Buff.Append(Colon); } //基本类型转换Json字符串写入Buff protected virtual void AppendByte(Byte value) { AppendNumber(value); } protected virtual void AppendDecimal(Decimal value) { AppendNumber(value); } protected virtual void AppendInt16(Int16 value) { AppendNumber(value); } protected virtual void AppendInt32(Int32 value) { AppendNumber(value); } protected virtual void AppendInt64(Int64 value) { AppendNumber(value); } protected virtual void AppendSByte(SByte value) { AppendNumber(value); } protected virtual void AppendUInt16(UInt16 value) { AppendNumber(value); } protected virtual void AppendUInt32(UInt32 value) { AppendNumber(value); } protected virtual void AppendUInt64(UInt64 value) { AppendNumber(value); } protected virtual void AppendDouble(Double value) { AppendNumber(value); } protected virtual void AppendSingle(Single value) { AppendNumber(value); } protected virtual void AppendBoolean(Boolean value) { Buff.Append(value ? "true" : "false"); } protected virtual void AppendChar(Char value) { Buff.Append(Quot); switch (value) { case '\\': case '\n': case '\r': case '\t': case '"': Buff.Append('\\'); break; } Buff.Append(value); Buff.Append(Quot); } protected virtual void AppendString(String value) { Buff.Append(Quot); for (int j = 0; j < value.Length; j++) { switch (value[j]) { case '\\': case '\n': case '\r': case '\t': case '"': Buff.Append('\\'); break; } Buff.Append(value[j]); } Buff.Append(Quot); } protected virtual void AppendDateTime(DateTime value) { Buff.Append(Quot); if (value.Year < 1000) { if (value.Year < 100) { if (value.Year < 10) { Buff.Append("000"); } else { Buff.Append("00"); } } else { Buff.Append("0"); } } Buff.Append(value.Year) .Append('-'); if (value.Month < 10) { Buff.Append('0'); } Buff.Append(value.Month).Append('-'); if (value.Day < 10) { Buff.Append('0'); } Buff.Append(value.Day).Append(' '); if (value.Hour < 10) { Buff.Append('0'); } Buff.Append(value.Hour).Append(Colon); if (value.Minute < 10) { Buff.Append('0'); } Buff.Append(value.Minute).Append(Colon); if (value.Second < 10) { Buff.Append('0'); } Buff.Append(value.Second).Append(Quot); } protected virtual void AppendGuid(Guid value) { Buff.Append(Quot).Append(value.ToString()).Append(Quot); } //枚举 protected virtual void AppendEnum(Enum value) { Buff.Append(Quot).Append(value.ToString()).Append(Quot); } protected virtual void AppendNumber(IConvertible number) { Buff.Append(number.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); } //转换数组对象 protected virtual void AppendArray(IEnumerable array) { Buff.Append('['); var ee = array.GetEnumerator(); if (ee.MoveNext()) { AppendObject(ee.Current); while (ee.MoveNext()) { Buff.Append(Comma); AppendObject(ee.Current); } } Buff.Append(']'); } //转换键值对对象 protected virtual void AppendJson(IDictionary dict) { AppendJson(dict.Keys, dict.Values); } //分别有键值枚举的对象 protected virtual void AppendJson(IEnumerable keys, IEnumerable values) { Buff.Append('{'); var ke = keys.GetEnumerator(); var ve = values.GetEnumerator(); if (ke.MoveNext() && ve.MoveNext()) { AppendKey(ke.Current + "", true); AppendObject(ve.Current); while (ke.MoveNext() && ve.MoveNext()) { Buff.Append(Comma); AppendKey(ke.Current + "", true); AppendObject(ve.Current); } } Buff.Append('}'); } protected virtual void AppendArray(IEnumerable enumer, Converter<object, object> getVal) { Buff.Append('['); var ee = enumer.GetEnumerator(); if (ee.MoveNext()) { AppendObject(ee.Current); while (ee.MoveNext()) { Buff.Append(Comma); AppendObject(getVal(ee.Current)); } } Buff.Append(']'); } protected virtual void AppendJson(IEnumerable enumer, Converter<object, string> getKey, Converter<object, object> getVal, bool escapekey) { Buff.Append('{'); var ee = enumer.GetEnumerator(); if (ee.MoveNext()) { AppendKey(getKey(ee.Current), escapekey); AppendObject(getVal(ee.Current)); while (ee.MoveNext()) { Buff.Append(Comma); AppendKey(getKey(ee.Current), true); AppendObject(getVal(ee.Current)); } } Buff.Append('}'); } protected virtual void AppendDataSet(DataSet dataset) { Buff.Append('{'); var ee = dataset.Tables.GetEnumerator(); if (ee.MoveNext()) { DataTable table = (DataTable)ee.Current; AppendKey(table.TableName, true); AppendDataTable(table); while (ee.MoveNext()) { Buff.Append(Comma); table = (DataTable)ee.Current; AppendKey(table.TableName, true); AppendDataTable(table); } } Buff.Append('}'); } protected virtual void AppendDataTable(DataTable table) { Buff.Append("{\"columns\":"); AppendArray(table.Columns, o => ((DataColumn)o).ColumnName); Buff.Append(",\"rows\":"); AppendArray(table.Rows, o => ((DataRow)o).ItemArray); Buff.Append('}'); } protected virtual void AppendDataView(DataView tableView) { Buff.Append("{\"columns\":"); AppendArray(tableView.Table.Columns, o => ((DataColumn)o).ColumnName); Buff.Append(",\"rows\":"); AppendArray(tableView, o => ((DataRowView)o).Row.ItemArray); Buff.Append('}'); } } }
完整代码中有部分代码做了调整
调用部分
测试下json格式
灵活扩展
例如 上面的栗子中,枚举转出的是他的字符串形式
如果现在我需要把所有枚举转为对应的数字怎么做呢?
public class EnumValueJsonBuilder : JsonBuilder { protected override void AppendEnum(Enum value) { Buff.Append(value.GetHashCode()); } }
新建一个类,然后重载AppendEnum就可以了
真是超级简单的啦~~~
.
.
.
.
.
不过,如果你以为这样就结束了,那么你就大错特错了
现在才刚刚开始.....未完待续....
我发布的代码,没有任何版权,遵守WTFPL协议(如有引用,请遵守被引用代码的协议)
qq群:5946699 希望各位喜爱C#的朋友可以在这里交流学习,分享编程的心得和快乐