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