Binary serialization of objects in the memory stream

Source: Internet
Author: User
Let's look at msdn. The most common examples of Object serialization are XML serialization in file streams. Because of the development needs, I must serialize objects in binary mode in the internal stream, the original code is as follows: public sealed class serializer
{
Private serializer (){}

Public static string serializeobject (Object OBJ)
{
Iformatter formatter = new binaryformatter ();
String result = string. empty;
Using (memorystream stream = new memorystream ())
{
Formatter. serialize (stream, OBJ );

Byte [] BYT = new byte [stream. Length];
Byt = stream. toarray ();
Result = encoding. utf8.getstring (BYT, 0, byt. Length );

Stream. Flush ();
}
Return result;
}

Public static object deserializeobject (string Str)
{
Iformatter formatter = new binaryformatter ();
Byte [] BYT = encoding. utf8.getbytes (STR );

Object OBJ = NULL;
Using (Stream stream = new memorystream (BYT, 0, byt. Length ))
{
OBJ = formatter. deserialize (Stream );
}
Return OBJ;
}
}

The error message is as follows: serializertest. serializeobjecttest causes an exception:

System. runtime. serialization. serializationexception: the binary stream "0" does not contain valid binaryheader. This may be due to invalid streams or object version changes between serialization and deserialization.

After debugging, we found that in the serializeobject method, "result = encoding. utf8.getstring (BYT, 0, byt. length); "after this line of code is run, the result value is \ 0 \ 0 ...... "It's no wonder that an error will be reported at the beginning of a string.
After that, I changed binaryformatter to xmlserializer and the other ones will not change, but the result will be returned normally, but that is not what I want, the key to the problem is to convert the byte array to the string. Therefore, I will use gb2312, ANSI, Unicode, and Other encoding to convert the string. Then I will find a method in the convert class, the modified code is as follows:

Public sealed class serializer
{
Private serializer (){}

Public static string serializeobject (Object OBJ)
{
Iformatter formatter = new binaryformatter ();
String result = string. empty;
Using (memorystream stream = new memorystream ())
{
Formatter. serialize (stream, OBJ );

Byte [] BYT = new byte [stream. Length];
Byt = stream. toarray ();
// Result = encoding. utf8.getstring (BYT, 0, byt. Length );
Result = convert. tobase64string (BYT );
Stream. Flush ();
}
Return result;
}

Public static object deserializeobject (string Str)
{
Iformatter formatter = new binaryformatter ();
// Byte [] BYT = encoding. utf8.getbytes (STR );
Byte [] BYT = convert. frombase64string (STR );
Object OBJ = NULL;
Using (Stream stream = new memorystream (BYT, 0, byt. Length ))
{
OBJ = formatter. deserialize (Stream );
}
Return OBJ;
}
}

Once the unit test is passed again, the modification is valid. Debug to "result = convert. tobase64string (BYT); ", the value of the result does not start with" \ 0 ". It is a letter, which proves the serialization success. After that, we should pay more attention to 64-Bit String Conversion, which often brings surprising results. This case is finally closed.

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.