Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

serialize and deserialize, is it really necessary?

Posted on 2010-11-23
8
Medium Priority
?
575 Views
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)
        {
            try
            {
                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;
            }
            catch
            {
                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?
0
Comment
Question by:enrique_aeo
8 Comments
 
LVL 5

Expert Comment

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

Author Comment

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

Accepted Solution

by:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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.
0
 
LVL 29

Assisted Solution

by:Nightman
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.
0
 

Author Comment

by:enrique_aeo
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.
0
 

Author Comment

by:enrique_aeo
ID: 34234468
the file with method fentListarBandejaWFGDH
2-fentListarBandejaWFGDH.txt
0
 
LVL 16

Assisted Solution

by:kris_per
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

926 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