. Net json serialization

Source: Internet
Author: User
Tags tojson

The following describes the application using asp.net json serialization. If you need it, please refer to it.

The Code is as follows: Copy code

/// <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 )));
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.