Schema of C # object to JSON object

Source: Internet
Author: User
Tags numeric json

Simple, because there is only one class

Lightweight because the entire class code has only 300 lines

Flexible because it only requires inheritance to override a method

First I call this class Jsonbuilder, and I want it to implement the conversion of the JSON string in a StringBuilder way

public class Jsonbuilder
{
    protected StringBuilder Buff = new StringBuilder (4096);//character buffer public
    string toJSONString (Object obj)
    {
        ...
        .. return buff.tostring ();
    }
    .......
}

Then I do a separate method for each underlying type, and the method can be overridden

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)

To make it easier to rewrite subclasses, I combine numeric types into one

protected virtual void Appendnumber (iconvertible number)

But still retain the original method, just the original method directly call Appendnumber, just like this

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

The advantage is that I can choose to override all the numeric types in the subclass, or just rewrite a particular type

Then, I need to complete some of the known types of conversion methods, such as arrays, sets, dictionaries, datasheets, and so on.

protected virtual void 

Appendarray (IEnumerable array)//array, set
protected virtual void Appendjson (IDictionary DICT)//dictionary
protected virtual void Appenddataset (DataSet DataSet)/data table set
protected virtual void appenddatatable ( DataTable table)//single table
protected virtual void Appenddataview (DataView view)/Table View

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.