Results 1 to 16 of 16

Thread: What is Serialization? [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    What is Serialization? [Resolved]

    What is serialization and why/when is it used?

    How important is it in VB.NET?
    Last edited by mendhak; Jun 1st, 2004 at 11:02 PM.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Its the process of taking an object in memory and persisting it in some way.

    Its most important (I think) for client/server communications. Instead of decomposing things into byte arrays to send them over a socket like you did in VB6, you simply open the socket, grab a reference to the I/O stream that belongs to the socket, and serialize pretty much any whole object you want to the stream. Then on the other end, you just deserialize the object from the stream.

    The SOAP protocol uses XML serialization as a means of transmitting object data.

    You can also write a whole object to a file and just grab it back out again.

    Pretty cool stuff.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Tell me if this is a good example of the usage of serialization:

    I create a class that contains properties such as "news title" and "news description." Everytime I update this object with different, new news items, I serialize it and send it across to some other computer.

    That computer takes this (XML file) object and uses it on its website. Kind of like a news feed.


    ?

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Sounds like a decent candidate.

    Does the other computer just sit there and listen for update calls, or would you just be copying a file over to it?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by crptcblade
    Sounds like a decent candidate.

    Does the other computer just sit there and listen for update calls, or would you just be copying a file over to it?
    I was just giving an example situation. I'm no expert.

    What would be the ideal way to go about it from your paragraph above?

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    I normally use Binary Serialization to accomplish my deeds.

    Particularly useful for cloning things not normally cloneable.

    I have code if you want it.

    Binary Serialization is better than XML because it stores ALL member data not just the public bits. Any bits that you don't want serialized can be ignored with <NonSerialized()> attribute.

    I'm currently using BS (unfortunate abbrev I know) to save an entire heirarchical data structure to a single file. Dead fast, safe and efficient. Cool.
    Last edited by wossname; Jun 1st, 2004 at 08:23 AM.
    I don't live here any more.

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Yes, I'd love to see code fo this!


    You shall get DW Soon!!!

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    OK, basically in a nutshell this is it. I have taken this code straight out of my current app. I've removed tha bits specific to my program that would be irrelevant to you...

    VB Code:
    1. Public Function Save(ByVal path As String) As Boolean
    2.     'serializes this class instance to a file in binary format
    3.     Dim binser As BinaryFormatter
    4.     Dim output As Stream
    5.  
    6.     Try
    7.         binser = New BinaryFormatter
    8.         output = New FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None)
    9.  
    10.         binser.Serialize(output, Me) 'where the instance is actually serialized to the stream
    11.  
    12.         output.Close()
    13.         Return True      'success! :)
    14.     Catch ex As Exception
    15.         If Not IsNothing(output) Then output.Close()
    16.             notify the user of the error if you want.
    17.         Return False        'failure :(
    18.     End Try
    19.  
    20. End Function

    Copy that code into a class and it will then be able to serialize itself to a file!
    I don't live here any more.

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Thanks a lot!

    Much appreciated.

  10. #10
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Sorry, I neglected to supply you with means of loading the file back into the object!

    VB Code:
    1. Shared Function FromFile(ByVal path As String) As !!YOUR_CLASS_NAME_HERE!!
    2.     'this method returns an object loaded from the file specified by path
    3.     'if the file path doesn't exist then a new object is created and returned
    4.  
    5.     Dim temp As !!YOUR_CLASS_NAME_HERE!!
    6.  
    7.     If File.Exists(path) Then
    8.  
    9.         Dim binser As BinaryFormatter
    10.         Dim input As Stream
    11.  
    12.         Try
    13.             'load the file
    14.             binser = New BinaryFormatter   
    15.             input = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)            'open the file
    16.             temp = CType(binser.Deserialize(input), !!YOUR_CLASS_NAME_HERE!!)              'extract instance from stream
    17.             input.Close()
    18.  
    19.         Catch ex As Exception
    20.             If Not IsNothing(input) Then input.Close()
    21.             temp = Nothing
    22.             Return Nothing
    23.         End Try
    24.     Else
    25.         temp = New !!YOUR_CLASS_NAME_HERE!!
    26.         temp.Path = path
    27.     End If
    28.  
    29.     Return temp
    30.  
    31. End Function

    It is a shared function.

    so you would use...
    VB Code:
    1. dim x as MyClass = MyClass.FromFile("Path")
    ...to call it.
    Last edited by wossname; Jun 2nd, 2004 at 07:49 AM.
    I don't live here any more.

  11. #11

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Hmm... I knew I was missing something.

    I swear, if I didn't have a third eye, I wouldn't know about my second.

  12. #12
    Member
    Join Date
    Apr 2004
    Posts
    44
    So the object that you're loading data into in this case is BoundaryDataStorage?

  13. #13
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Oops, my mistake... Good point Yak.

    Mendhak: Check out the top line of the FromFile() code again. I have edited it cos it was wrong. But you probably guessed that anyway.

    EDIT.
    I don't live here any more.

  14. #14
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409
    if any one is wondering you need to import
    Imports System.io
    Imports System.Runtime.Serialization.Formatters.Binary

    for woss's code

  15. #15
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409
    and put <Serializable()> by any classes you will be Serializaing

  16. #16
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Hey its a learning process, I didn't want to give them ALL the answers!
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width