M
M
Mikhail Doshevsky2014-05-17 11:06:04
.NET
Mikhail Doshevsky, 2014-05-17 11:06:04

Serialization in PCL

Hey!
There is a PCL (Portable Class Library) library used in the Windows Store and Windows Phone 8 projects.
This library has structures that have several fields of basic types:

private FieldTypes _type;
private string _name;
private string _value;

you need to serialize these structures and get an array of bytes.
If you use the DataContractSerializer, the output will be XML, which is very redundant and increases the size of the stored data. I will have a lot of data, including binary data (pictures) that I would not like to encode in base64, because the size will grow by another third, therefore XML is not suitable.
Now I found a solution like this (for the data above):
internal byte[] ToBinary()
        {
            byte[] binaryType = BitConverter.GetBytes((int)_type);

            byte[] binaryNameLength = BitConverter.GetBytes(_name.Length);
            byte[] binaryName = new byte[_name.Length * sizeof(char)];
            Buffer.BlockCopy(_name.ToCharArray(), 0, binaryName, 0, binaryName.Length);

            byte[] binaryValueLength = BitConverter.GetBytes(_value.Length);
            byte[] binaryValue = new byte[_value.Length * sizeof(char)];
            Buffer.BlockCopy(_value.ToCharArray(), 0, binaryValue, 0, binaryValue.Length);

            int resultLength = binaryType.Length + binaryNameLength.Length + binaryName.Length + binaryValueLength.Length + binaryValue.Length;
            byte[] result = new byte[resultLength];

            Buffer.BlockCopy(binaryType, 0, result, 0, binaryType.Length);
            Buffer.BlockCopy(binaryName, 0, result, binaryType.Length, binaryName.Length);
            Buffer.BlockCopy(binaryNameLength, 0, result, binaryType.Length + binaryName.Length, binaryNameLength.Length);
            Buffer.BlockCopy(binaryValueLength, 0, result, binaryType.Length + binaryName.Length + binaryNameLength.Length, binaryValueLength.Length);
            Buffer.BlockCopy(binaryValue, 0, result, binaryType.Length + binaryName.Length + binaryNameLength.Length + binaryValueLength.Length, binaryValue.Length);

            return result;
        }

but it is not universal, you will have to write a similar bicycle for each structure.
Are there solutions (preferably non-third party ones) that allow you to serialize an arbitrary structure (with base types) into a binary format?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2014-05-17
@r4tz52

BinaryFormatter ?
UPD: I'm sorry, I didn't notice the phrase about the Windows Store.
Check out UniversalSerializer , it should be able to do that.

O
Oxoron, 2014-08-08
@Oxoron

The problem can be solved by marshaling (marshaling).
habrahabr.ru/post/114953 - here is an excellent article: a balance between theory, practice and size.
www.cyberforum.ru/csharp-net/thread342135.html#a_53 - here is a detailed description.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question