Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing a jagged byte array efficiently

I have a jagged array DataMember that returns byte[800][10000][1] which translates to ~8MB of data. When serialized using DataContractSerializer it turns into a file size 120MB for Binary and 300MB for Text. Looking at the text serialization, it indicates that it puts a tag around every byte value to indicate that its a base64 encoded value etc., which might be the reason for this size enlargement.

What is an efficient way to serialize a large jagged blob using DataContractSerializer (or any other serializer for that matter)

snippet XML output when serialized using Text:

<a:ArrayOfbase64Binary><a:base64Binary>CQ==</a:base64Binary><a:base64Binary>Bw==</a:base64Binary><a:base64Binary>BQ==</a:base64Binary><a:base64Binary>AA==</a:base64Binary><a:base64Binary>AQ==</a:base64Binary><a:base64Binary>AA==</a:base64Binary><a:base64Binary>BA==</a:base64Binary><a:base64Binary>Bg==</a:base64Binary><a:base64Binary>CA==</a:base64Binary><a:base64Binary>AA==</a:base64Binary><a:base64Binary>AA==</a:base64Binary><a:base64Binary>AA==</a:base64Binary><a:base64Binary>AA==</a:base64Binary><a:base64Binary>AA==</a:base64Binary><a:base64Binary>AA==</a:base64Binary><a:base64Binary>AA==</a:base64Binary>
like image 367
LadderLogic Avatar asked Feb 15 '13 21:02

LadderLogic


1 Answers

You could try using a BinaryFormatter if you don't care about interoperability with other systems but only about performance:

byte[800][10000][1] foo = ... fetch it
BinaryFormatter formatter = new BinaryFormatter();
using (var stream = File.OpenWrite("foobar.dat"))
{
    formatter.Serialize(stream, foo);
}
like image 173
Darin Dimitrov Avatar answered Oct 05 '22 20:10

Darin Dimitrov