serialize and deserialize, is it really necessary?

Posted on 2010-11-23
Medium Priority
Last Modified: 2012-05-10
I have a method in which sets the value of an object with some properties objBandeja
1. Serialize this object: strobjBandeja = CFunciones.SerializeObject (Of EBandeja) (objBandeja), whose code is
public static string SerializeObject<T>(T obj)
                string xmlString = null;
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
                System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
                System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(memoryStream, System.Text.Encoding.UTF8);
                xs.Serialize(xmlTextWriter, obj);
                memoryStream = (System.IO.MemoryStream)xmlTextWriter.BaseStream;
                xmlString = UTF8ByteArrayToString(memoryStream.ToArray());
                return xmlString;
                return string.Empty;
2. The following line of code is this
objEGeneral = WFGDHUtilitario.DeserializeObject(Of EGeneral)(oLSolicitud.fentListarBandejaWFGDH(strobjBandeja))

in which sending the serialized object. FentListarBandejaWFGDH method code is on file: 2 fentListarBandejaWFGDH.txt
3. analyzing the code does the following
3.1 deserialize the xml variable objEBandeja = CFunciones.DeserializeObject (Of EBandeja) (pstrOBJEBandeja)
3.2 gets the value of a property and that value calculates the value of some variables that will be sent to a stored procedure

MY QUESTION IS, in this case in order to serialize and deserialize is to calculate the parameter of the stored procedure?
Question by:enrique_aeo

Expert Comment

ID: 34203187
I could not understand exactly what your question is.

Author Comment

ID: 34204324
do not understand why serialize and deserialize. I understand the concept but I do not understand its use
LVL 16

Accepted Solution

kris_per earned 1336 total points
ID: 34204911

I think Serialize/Deserialize is used to create another copy of the object objBandeja. If some operations change the internal state of an object and if you dont want keep this change in the object, then the way is to create a copy of the object and do the operation on the copy; so internal changes will be only in the copy and first object will remain the same.

In your question:
step 1: serialize orginal object to get it as xml string
step 3.1: deserialize the xml string to get an object which will be a copy of the original object
step 3.2: operation in this step I think changes some value in the second object.

Thus by doing step 3.2 on the second object, the original object remain unchanged I think which is the purpose.

See if this fits.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

LVL 21

Expert Comment

by:Alpesh Patel
ID: 34205113
It is useful when you are passing object over network to improve performance. Because serialization reduce the use of bandwidth.
LVL 29

Assisted Solution

Nightman earned 664 total points
ID: 34207745
In this case, the purpose of deserialization is most likely the transfer of the data object between two dissimilar applications or systems, or incompatible languages. Since the object can be represented in xml, the provider or client simply provides it in a textual representation of the class.

The consuming application simply deserializes the stream back into an object that it can consume (which should implicitly validate the properties based on type) and has probably implemented some validation as well (and as you say, calculations in the code based on other properties) before it calls a stored procedure on the database. This allows them to re-use the same class in multiple locations in their code without having to write a parser and without the overhead of the XML DOM.

"I think Serialize/Deserialize is used to create another copy of the object "
That would appear to be a particularly expensive way of creating a copy. Simpler to implement an ICloneable interface and copy the properties in memory.

"It is useful when you are passing object over network to improve performance"
Not with XML serialization - it's effectively string parsing, which is notoriuosly slow. For a performant solution, binary would be significantly better.

Author Comment

ID: 34234462
Thanks experts, i travel and recently can respond. Their responses are very interesting, i am atachando the method that lack, with that we are going to have a better idea, i hope their support.

Author Comment

ID: 34234468
the file with method fentListarBandejaWFGDH
LVL 16

Assisted Solution

kris_per earned 1336 total points
ID: 34238831
This function does the following:

   Deserialize the xml string parameter to get objEBandeja(EBandeja type) object.

   Use the Filtro member of objEBandeja object to use in sql parameters in the stored proc.

   Results of stored proc is stored in objEGeneral(EGeneral type) object.

   Serialize the objEGeneral object to get xml string and return xml string.

I think the reason why it uses Deserialize/Serialize depends on the requirement of the caller and what input the caller has.

If the caller already has objEBandeja(EBandeja type) object, then this function can be changed to take objEBandeja(EBandeja type) object as parameter; then there is no need to do the first Deserialize.

If the caller needs the return result as objEGeneral(EGeneral type) object, then the function can just return objEGeneral(EGeneral type) object instead of xml string; then there is no need for the final serialize.

It looks this function is a helper fuction for the caller to suit its need. I dont see any other special use here (like to create a copy or to transfer the object across network or systems)

Hope this helps.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question