/// <Summary> /// Serialize the object into a Json text stream. /// </Summary> /// <Param name = "source"> object to be converted. </Param> /// <Param name = "inclusive"> select the attribute array to be included. </Param> /// <Param name = "exclusive"> select the attribute array to be excluded. </Param> /// <Returns> </returns> Public static string ToJson (this object source, string [] inclusive = null, string [] exclusive = null) { If (source = null) { Return "null "; } Var type = source. GetType (); Switch (Type. GetTypeCode (type )) { // Numeric type Case TypeCode. Byte: Case TypeCode. Decimal: Case TypeCode. Double: Case TypeCode. Int16: Case TypeCode. Int32: Case TypeCode. Int64: Case TypeCode. SByte: Case TypeCode. Single: Case TypeCode. UInt16: Case TypeCode. UInt32: Case TypeCode. UInt64: Return source. ToString (); Case TypeCode. Object: Break; Case TypeCode. Boolean: Return (bool) source? "True": "false "; Case TypeCode. DBNull: Return "null "; Default: Return "+ source + """; } Var sb = new StringBuilder (); Var flag = new AssertFlag (true ); // DataSet If (source is DataSet) { Var ds = source as DataSet; Sb. Append ("["); Foreach (var table in ds. Tables) { If (! Flag. AssertTrue ()) { Sb. Append (","); } Sb. Append (table. ToJson ()); } Sb. Append ("]"); } // DataTable Else if (source is DataTable) { Var tb = source as DataTable; Sb. Append ("["); Foreach (var row in tb. Rows) { If (! Flag. AssertTrue ()) { Sb. Append (","); } Sb. Append (row. ToJson ()); } Sb. Append ("]"); } // DataRow Else if (source is DataRow) { Var row = source as DataRow; Sb. Append ("{"); Foreach (DataColumn column in row. Table. Columns) { If (PropertyNotToJson (column. ColumnName, inclusive, exclusive )) { Continue; } If (! Flag. AssertTrue ()) { Sb. Append (","); } Sb. AppendFormat ("" {0} ": {1}", column. ColumnName, row [column]. ToJson ()); } Sb. Append ("}"); } // Array Else if (type. IsArray) { Var array = source as Array; Sb. Append ("["); For (var I = 0; I <array. Length; I ++) { If (! Flag. AssertTrue ()) { Sb. Append (","); } Sb. Append (array. GetValue (I). ToJson ()); } Sb. Append ("]"); } // IEnumerable Else if (source is IEnumerable) { Var enu = (source as IEnumerable). GetEnumerator (); Sb. Append ("["); While (enu. MoveNext ()) { If (! Flag. AssertTrue ()) { Sb. Append (","); } Sb. Append (enu. Current. ToJson ()); } Sb. Append ("]"); } Else if (type! = Typeof (object )) { Sb. Append ("{"); Var pps = from s in type. GetProperties (BindingFlags. Public | BindingFlags. Instance) where s. CanRead select s; Foreach (var pro in pps) { If (PropertyNotToJson (pro. Name, sive Sive, exclusive )) { Continue; } Var value = pro. GetValue (source, null ); If (! Flag. AssertTrue ()) { Sb. Append (","); } Sb. AppendFormat ("" {0} ": {1}", pro. Name, value. ToJson ()); } Sb. Append ("}"); } Return sb. ToString (); } /// <Summary> /// Attributes are not serialized. /// </Summary> /// <Param name = "propertyName"> attribute name. </Param> /// <Param name = "inclusive"> select the attribute array to be included. </Param> /// <Param name = "exclusive"> select the attribute array to be excluded. </Param> /// <Returns> </returns> Private static bool PropertyNotToJson (string propertyName, IEnumerable <string> random sive = null, IEnumerable <string> exclusive = null) { Return (intrusive! = Null &&! Inclusive. Contains (propertyName) | (Exclusive! = Null & exclusive. Contains (propertyName ))); } |