Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Formatter" and "Serializer" - any difference between the terms?

Some things in .NET are called "formatters" - BinaryFormatter, SoapFormatter.

Others are called "serializers" - XmlSerializer, DataContractSerializer.

Why the difference?

like image 551
Stefan Monov Avatar asked Sep 09 '10 14:09

Stefan Monov


3 Answers

A bit tenuous, but there is a subtle difference. There are 17 concrete classes in the .NET framework that format XML. These formatters are all hidden, you get an instance to them with a method like XmlWriter.Create(). Same for DataContractSerializer, the actual formatting is done by, say, an XmlDictionaryWriter instance.

No such indirection for BinaryFormatter or SoapFormatter, they take care of the formatting themselves. In other words, a Formatter formats, a Serializer uses a formatter.

like image 153
Hans Passant Avatar answered Oct 22 '22 22:10

Hans Passant


No, they are synonyms. They do the same: convert CLR object to transferable sequence of bytes.

like image 29
Andrey Avatar answered Oct 22 '22 22:10

Andrey


The differences in the formatters is crucial - BinaryFormatter as it suggests, the data are in native binary fashion, whereas SoapFormatter is in Xml text fashion, throw in the different ways of serializing is actually dependent on the type of formatters, binary data using BinaryFormatter tend to be a lot smaller and faster than soap formatters.

It is for that reason, if you want to take a "memory dump" it is better to use BinaryFormatter and serialize/deserialize away, at the cost of data interoperability between different architectures - meaning it may not be compatible if exchanging data between different platforms BUT faster processing...

Whereas with SoapFormatter it is protected from such binary incompatibilities as it is text based on either Unicode or ASCII, but much slower!

like image 20
t0mm13b Avatar answered Oct 22 '22 20:10

t0mm13b