Some more about serialization

T

Tony Johansson

Hi!

I'm reading a book from Microsoft Press(MCTS). It says
*If you just needed to store a single string in a file, you wouldn't need to
use serialization
you could simply write the string directly to a text file. Serialization
becomes useful when storing more complex
information, such as the current date and time. As the following code sample
demonstates, serializing complex objects is as simple as serializing a
string."

Can anybode tell me the reason why to use serialization when I want to store
current date and time as the text above claims
when it's even simpler to store it directly in a file like I have done below
in example 2 here ?

Example 1
FileStream fs = new FileStream("Test1.data", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.serialize(fs,System.DateTime.Now);
fs.Close();

Example 2
StreamWriter sw = new StreamWriter("Test2.data");
sw.WriteLine(System.DateTime.Now.ToString());
sw.Close();

//Tony
 
W

Willem van Rumpt

Tony said:
Can anybode tell me the reason why to use serialization when I want to store
current date and time as the text above claims
when it's even simpler to store it directly in a file like I have done below
in example 2 here ?

Example 1
FileStream fs = new FileStream("Test1.data", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.serialize(fs,System.DateTime.Now);
fs.Close();

Example 2
StreamWriter sw = new StreamWriter("Test2.data");
sw.WriteLine(System.DateTime.Now.ToString());
sw.Close();

//Tony

In your second example you're still writing a string value.
The resulting string depends on the current culture. People
who might need to read back that date, need to know your culture
settings, in order to be guaranteed that they can read back the
date you wrote.

Serialization on the other hand does not have that drawback. It
serializes the *state* of an object, not some arbitrary string
representation.
 
P

Peter Duniho

Willem said:
[...]
Serialization on the other hand does not have that drawback. It
serializes the *state* of an object, not some arbitrary string
representation.

Noting, of course, that simply by changing the second example Tony
posted so that it uses, for example, the invariant culture it can be
made to work just like the built-in serialization.

Pete
 
A

Arne Vajhøj

Hi!

I'm reading a book from Microsoft Press(MCTS). It says
*If you just needed to store a single string in a file, you wouldn't need to
use serialization
you could simply write the string directly to a text file. Serialization
becomes useful when storing more complex
information, such as the current date and time. As the following code sample
demonstates, serializing complex objects is as simple as serializing a
string."

Can anybode tell me the reason why to use serialization when I want to store
current date and time as the text above claims
when it's even simpler to store it directly in a file like I have done below
in example 2 here ?

Example 1
FileStream fs = new FileStream("Test1.data", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.serialize(fs,System.DateTime.Now);
fs.Close();

Example 2
StreamWriter sw = new StreamWriter("Test2.data");
sw.WriteLine(System.DateTime.Now.ToString());
sw.Close();

You do not necessarily want to store date+time using serialization
instead of string.

string - you need to document the format
works with any technology

serialization - you do not need to document the format
only works with .NET

(we assume traditional binary serialization here - XML
serialization is different)

If you want to store date+time then you can either
write some documentation about a specific format
like ISO 8601 and write a string or you can
use serialization.

In general I will only suggest serialization for real
time communication like one .NET app talking to another
..NET app over sockets - never for long term persistence
of data.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top