473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Serializing?

Hi, just wondering what serializing really is, and howto use it?
Thanks.

Tobias

__________________________________________________ ________________ Tobias
ICQ#: 55986339 Current ICQ status: + More ways to contact me
__________________________________________________ ________________
Nov 15 '05 #1
2 3488
Serializing is the ability to take an object in memory (with all of the
objects that it owns) and change it to a stream of bytes. These bytes can
be stored or sent anywhere a stream of bytes can go. Any application that
understands the structure of the objects that are stored or sent can
"deserialize" the stream and reconstruct the object to its previous state.
This makes is extremely easy to take a complicated object graph and dump it
to disk (save file) and latter retieve the object for further processing
(open file).

How to use it? I think you will have spend some time on msdn.microsoft.com.
Search for "serialziation .Net". There is plenty of documenation there.

Mike P.

"Tobias Zimmergren" <to***************@bredband.net> wrote in message
news:ea**************@TK2MSFTNGP09.phx.gbl...
Hi, just wondering what serializing really is, and howto use it?
Thanks.

Tobias

__________________________________________________ ________________ Tobias
ICQ#: 55986339 Current ICQ status: + More ways to contact me
__________________________________________________ ________________

Nov 15 '05 #2
Hi Tobias,

In english, I like to think of serializing as zipping the contents
of an object and then unzipping those contents at a later date. In the
interim the serialized (zipped) object can be stored on a disk or a
database.

The official version is worth reading so, I've cut & pasted some
relevant entries from the documentation. There are plenty of examples
online and within the MSDN website in addition to the VSNET help.

The official version:
Serialization is the process of converting the state of an object into
a form that can be persisted or transported. The complement of
serialization is deserialization, which converts a stream into an
object. Together, these processes allow data to be easily stored and
transferred.

The .NET Framework features two serializing technologies:

Binary serialization preserves type fidelity, which is useful for
preserving the state of an object between different invocations of an
application. For example, you can share an object between different
applications by serializing it to the Clipboard. You can serialize an
object to a stream, to a disk, to memory, over the network, and so
forth. Remoting uses serialization to pass objects "by value" from one
computer or application domain to another.
XML serialization serializes only public properties and fields and
does not preserve type fidelity. This is useful when you want to
provide or consume data without restricting the application that uses
the data. Because XML is an open standard, it is an attractive choice
for sharing data across the Web. SOAP is likewise an open standard,
which makes it an attractive choice.

Why would you want to use serialization?

The two most important reasons are to persist the state of an object
to a storage medium so an exact copy can be re-created at a later
stage, and to send the object by value from one application domain to
another. For example, serialization is used to save session state in
ASP.NET and to copy objects to the Clipboard in Windows Forms. It is
also used by remoting to pass objects by value from one application
domain to another.

You should consider serialization when designing new classes, because
a class cannot be made serializable after it has been compiled. Some
questions to ask are: Will this class need to be sent across
application domains? Will this class ever be used with remoting? What
will users do with this class — might they derive a new class from
mine that needs to be serialized? When in doubt, mark the class as
serializable. It is probably better to mark all classes as
serializable unless any of the following are true:

The class will never cross an application domain. If serialization is
not required and the class needs to cross an application domain,
derive the class from MarshalByRefObject.
The class stores special pointers that are only applicable to the
current instance of the class. If a class contains unmanaged memory or
file handles, for example, ensure these files are marked as
NonSerialized, or don't serialize the class at all.
Class data members contain sensitive information. In this case, it is
advisable to mark the class as serializable, but to mark the
individual data members that contain sensitive information as
NonSerialized. Another alternative is to implement the ISerializable
interface and serialize only the required fields.
Be aware of the security implications of marking a class as
serializable. A Link Demand or an Inheritance Demand for a
CodeAccessPermission on a class or class constructor can be bypassed
by default or custom serialization that implements a corresponding
demand for the same CodeAccessPermission. If a class has a Link Demand
for a permission, the runtime checks only the immediate caller to
verify that the caller has been granted the permission. The .NET
Framework class library code is signed with the Microsoft strong name
and is always granted full trust. Any code can use code that is
granted full trust to bypass link-time security checks. For example,
in the case of serialization, malicious code that does not have the
required serialization permission can call one of the fully trusted
..NET Framework formatters, such as BinaryFormatter, and bypass the
link-demand check for the permission.

[************** Why,Why,Why?? **************]

It is often necessary to store the value of fields of an object to
disk and then retrieve this data at a later stage. Although this is
easy to achieve without relying on serialization, this approach is
often cumbersome and error prone, and becomes progressively more
complex when you need to track a hierarchy of objects. Imagine writing
a large business application containing many thousands of objects and
having to write code to save and restore the fields and properties to
and from disk for each object. Serialization provides a convenient
mechanism for achieving this objective with minimal effort.

The common language runtime manages how objects are laid out in memory
and provides an automated serialization mechanism by using reflection.
When an object is serialized, the name of the class, the assembly, and
all the data members of the class instance are written to storage.
Objects often store references to other instances in member variables.
When the class is serialized, the serialization engine keeps track of
all referenced objects already serialized to ensure that the same
object is not serialized more than once. The serialization
architecture provided with the .NET Framework correctly handles object
graphs and circular references automatically. The only requirement
placed on object graphs is that all objects referenced by the object
that is being serialized must also be marked as Serializable (see
Basic Serialization). If this is not done, an exception will be thrown
when the serializer attempts to serialize the unmarked object.

When the serialized class is deserialized, the class is re-created and
the values of all the data members are automatically restored.

--
~~~~~~~~~~~~~
Tommie Carter
tcarternyc(at)hotmail(dot)com
--
"Tobias Zimmergren" <to***************@bredband.net> wrote in message news:<ea**************@TK2MSFTNGP09.phx.gbl>...
Hi, just wondering what serializing really is, and howto use it?
Thanks.

Tobias

__________________________________________________ ________________ Tobias
ICQ#: 55986339 Current ICQ status: + More ways to contact me
__________________________________________________ ________________

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ryan Hubbard | last post by:
I've have been trying to serialize an array of objects. I can serialize and set a session variable and retrieve it successfully for a serialize string of any length within the contents of a single...
2
by: Aleksei Guzev | last post by:
Imagine one writing a class library CL1 for data storage. He defines classes ‘DataItem’ and ‘DataRecord’ so that the latter contains a collection of the former. And he derives class ‘IntItem’ from...
1
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I...
10
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b)...
1
by: Chris | last post by:
I'm having trouble Serializing a System.Data.DataColumn object. When I try to serialize it, I get the following: System.NotSupportedException: Cannot serialize member...
4
by: Jason Shohet | last post by:
We are thinking of serializing an object & passing it toseveral functions on web service. This will happen about 35 times as the page loads. The class has about 20 attributes. We're not sure...
2
by: Simon | last post by:
I'm developing a new application and want to use serialization as a way to save my data. But as I add new variables to my classes, how will serializing cope with that? For example, suppose I have...
7
by: fjlaga | last post by:
I have written an Office Add-in for Excel using VB.NET and the .NET 1.1 Framework (I have Visual Studio 2003 .NET ). All works great. I want to add a User Settings/Prefereneces dialog and allow...
12
by: Cagdas Ozgenc | last post by:
Greetings, When directly serializing C++ structures to a file with the standard library functions giving the address of the data and length of structure using the sizeof operator, do I risk...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.