Experience in data serialization (2)-datacontract formatter

Source: Internet
Author: User

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.

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.