The serialization class in this article is mainly implemented using datacontractserializer, which is particularly useful for Classes labeled with datacontract.
Binary serialization:
Public static class datacontractformatserializer
{
Public static string serializetobase64string <t> (t obj, bool compress)
{
Byte [] ret = serialize <t> (OBJ, compress );
Return convert. tobase64string (RET );
}
Public static byte [] serialize <t> (t obj, bool compress)
{
If (OBJ = NULL)
{
Return NULL;
}
Byte [] Info;
Using (memorystream stream = new memorystream ())
{
Datacontractserializer serializer = new datacontractserializer (obj. GetType ());
Using (xmldictionarywriter binarydictionarywriter = xmldictionarywriter. createbinarywriter (Stream ))
{
Serializer. writeobject (binarydictionarywriter, OBJ );
Binarydictionarywriter. Flush ();
}
Info = stream. toarray ();
If (compress)
{
Info = compresshelper. compressbytes (Info );
}
}
Return Info;
}
Public static t deserializefrombase64string <t> (string basestring, bool decompress)
{
If (string. isnullorempty (basestring ))
Return default (t );
Byte [] buffer = convert. frombase64string (basestring );
Return deserialize <t> (buffer, decompress );
}
Public static t deserialize <t> (byte [] info, bool decompress)
{
T ret = default (t );
If (Info = NULL | info. Length <= 0)
{
Return ret;
}
If (decompress)
{
Info = compresshelper. decompressbytes (Info );
}
Using (memorystream stream = new memorystream (Info ))
{
Datacontractserializer serializer = new datacontractserializer (typeof (t ));
Using (xmldictionaryreader binarydictionaryreader = xmldictionaryreader. createbinaryreader (stream, xmldictionaryreaderquotas. max ))
{
Ret = (t) serializer. readobject (binarydictionaryreader );
}
}
Return ret;
}
}
XML:
Public static class datacontractxmlserializer
{
Public static string serialize <t> (t obj)
{
If (OBJ = NULL)
{
Return NULL;
}
String ret = "";
Using (memorystream stream = new memorystream ())
{
Datacontractserializer serializer = new datacontractserializer (obj. GetType ());
Using (xmldictionarywriter binarydictionarywriter = xmldictionarywriter. createtextwriter (stream, encoding. utf8 ))
{
Serializer. writeobject (binarydictionarywriter, OBJ );
Binarydictionarywriter. Flush ();
}
Ret = encoding. utf8.getstring (stream. toarray ());
}
Return ret;
}
Public static t deserialize <t> (string XML)
{
T ret = default (t );
If (string. isnullorempty (XML ))
{
Return ret;
}
Using (memorystream stream = new memorystream ())
{
Byte [] bytes = encoding. utf8.getbytes (XML );
Stream. Write (bytes, 0, bytes. Length );
Stream. Position = 0l;
Datacontractserializer serializer = new datacontractserializer (typeof (t ));
Using (xmldictionaryreader binarydictionaryreader = xmldictionaryreader. createtextreader (stream, xmldictionaryreaderquotas. max ))
{
Ret = (t) serializer. readobject (binarydictionaryreader );
}
}
Return ret;
}
}
After testing, the serialization class in this article is faster than binary formatter, And the serialized result is smaller. Of course, the premise is that the class should be marked with datacontract. Otherwise, the serialization class is basically the same.