Tuesday, October 22, 2019
All About Serializing in Visual Basic
All About Serializing in Visual Basic Serialization is the process of converting an object into a linear sequence of bytes called a byte stream. Deserialization just reverses the process. But why would you want to convert an object into a byte stream? The main reason is so you can move the object around. Consider the possibilities. Since everything is an object in .NET, you can serialize anything and save it to a file. So you could serialize pictures, data files, the current state of a program module (state is like a snapshot of your program at a point in time so you could temporarily suspend execution and start again later) ... whatever you need to do. You can also store these objects on disk in files, send them over the web, pass them to a different program, keep a backup copy for safety or security. The possibilities are quite literally endless. Thats why serialization is such a key process in .NET and Visual Basic. Below is a section on custom serialization by implementing the ISerializable interface and coding a New and a GetObjectData subroutine. As a first example of serialization, lets do one of the easiest programs, but also one of the most useful: serializing data, and then deserializing data in simple class to and from a file. In this example, the data is not only serialized, but the structure of the data is saved too. The structure here is declared in a module to keep things ... well ... structured. Module SerializeParmsSerializable() Public Class ParmExampleà à à Public Parm1Name As String Parm1 Nameà à à Public Parm1Value As Integer 12345à à à Public Parm2Name As Stringà à à Public Parm2Value As DecimalEnd ClassEnd Module Then, individual values can be saved to a file like this: Imports System.Runtime.Serialization.Formatters.BinaryImports System.IOPublic Class Form1à à à Private Sub mySerialize_Click( _à à à à à à ByVal sender As System.Object, _à à à à à à ByVal e As System.EventArgs) _à à à à à à Handles mySerialize.Clickà à à à à à Dim ParmData As New ParmExampleà à à à à à ParmData.Parm2Name Parm2 Nameà à à à à à ParmData.Parm2Value 54321.12345à à à à à à Dim s As New FileStream(ParmInfo, FileMode.Create)à à à à à à Dim f As New BinaryFormatterà à à à à à f.Serialize(s, ParmData)à à à à à à s.Close()à à à End SubEnd Class And those same values can be retrieved like this: Imports System.Runtime.Serialization.Formatters.BinaryImports System.IOPublic Class Form1à à à Private Sub myDeserialize_Click( _à à à à à à ByVal sender As System.Object, _à à à à à à ByVal e As System.EventArgs) _à à à à à à Handles myDeserialize.Clickà à à à à à Dim s New FileStream(ParmInfo, FileMode.Open)à à à à à à Dim f As New BinaryFormatterà à à à à à Dim RestoredParms As New ParmExampleà à à à à à RestoredParms f.Deserialize(s)à à à à à à s.Close()à à à à à à Console.WriteLine(RestoredParms.Parm1Name)à à à à à à Console.WriteLine(RestoredParms.Parm1Value)à à à à à à Console.WriteLine(RestoredParms.Parm2Name)à à à à à à Console.WriteLine(RestoredParms.Parm2Value)à à à End SubEnd Class A Structure or a collection (such as an ArrayList) rather than a Class could also be serialized to a file this same way. Now that we have gone over the basic serializing process, lets look at the specific details that are part of the process on the next page. One of the first things you should notice about this example is the Serializable() attribute in the Class. Attributes are just more information that you can provide to VB.NET about an object and theyre used for a lot of different things.à The attribute in this code tells VB.NET to add extra code so that later on, everything in this class can be serialized. If there are specific items in the Class that you dont want to be serialized, you can use the NonSerialized() attribute to exclude them: NonSerialized() Public Parm3Value As String Whatever In the example, notice is that Serialize and Deserialize are methods of the BinaryFormatter object (f in this example). f.Serialize(s, ParmData) This object takes the FileStream object and the object to be serialized as parameters. Well see that VB.NET offers another object that allows the result to be expressed as XML. And one final note, if your object includes other subordinate objects, theyll be serialized too! But since all objects that are serialized must be marked with the Serializable() attribute, all of these child objects must be marked that way too. Just to be completely clear about what is happening in your program, you might want to display the file named ParmData in Notepad to see what serialized data looks like. (If you followed this code, it should be in the bin.Debug folder in your project.) Since this is a binary file, most of the content isnt readable text, but you should be able to see any strings in your serialized file. Well do an XML version next and you might want to compare the two just to be aware of the difference. Serializing to XML instead of a binary file requires very few changes. XML isnt as fast and cant capture some object information, but its far more flexible. XML can be used by just about any other software technology in the world today. If you want to be sure your file structures dont tie you into Microsoft, this is a good option to look into. Microsoft is emphasizing LINQ to XML to create XML data files in their latest technology but many people still prefer this method. The X in XML stands for eXtensible. In our XML example, were going to use one of those extensions of XML, a technology called SOAP. This used to mean Simple Object Access Protocol but now its just a name. (SOAP has been upgraded so much that the original name doesnt fit that well anymore.) The main thing that we have to change in our subroutines is the declation of the serialization formatter. This has to be changed in both the subroutine that serializes the object and the one that deserializes it again. For the default configuration, this involves three changes to your program. First, you have to add a Reference to the project. Right-click the project and select Add Reference .... Make sure ... System.Runtime.Serialization.Formatters.Soap ... has been added to the project. Then change the two statements in the program that references it. Imports System.Runtime.Serialization.Formatters.SoapDim f As New SoapFormatter This time, if you check out the same ParmData file in Notepad, youll see that the whole thing is in readable XML text such as ... Parm1Name idref-3Parm1 Name/Parm1NameParm1Value12345/Parm1ValueParm2Name idref-4Parm2 Name/Parm2NameParm2Value54321.12345/Parm2Value There is also a lot of additional XML there thats necessary for the SOAP standard in the file as well. If you want to verify what the NonSerialized() attribute does, you can add a variable with that attribute and look at the file to verify that its not included. The example we just coded only serialized the data, but suppose you need to control how the data is serialized. VB.NET can do that too! To accomplish this, you need to get a little deeper into the concept of serialization. VB.NET has a new object to help out here: SerializationInfo. Although you have the ability to code custom serialization behavior, it comes with a cost of extra coding. The basic extra code is shown below. Remember, this class is used instead of the ParmExample class shown in the earlier example. This isnt a complete example. The purpose is to show you the new code that is needed for custom serialization. Imports System.Runtime.SerializationSerializable() _Public Class CustomSerializationà à à Implements ISerializableà à à data to be serialized hereà à à Public SerializedVariable as Typeà à à Public Sub New()à à à default constructor when the classà à à is created - custom code can beà à à added here tooà à à End Subà à à Public Sub New( _à à à à à à ByVal info As SerializationInfo, _à à à à à à ByVal context As StreamingContext)à à à à à à initialize your program variables fromà à à à à à a serialized data storeà à à End Subà à à Public Sub GetObjectData( _à à à à à à ByVal info As SerializationInfo, _à à à à à à ByVal context As StreamingContext) _à à à à à à Implements ISerializable.GetObjectDataà à à à à à update the serialized data storeà à à à à à from program variablesà à à End SubEnd Class The idea is that now you can (and, in fact, you must) do all of the updating and reading of data in the serialized data store in the New and GetObjectData subroutines. You must also include a generic New constructor (no parameter list) because youre implementing an interface. The class will normally have formal properties and methods coded as well ... Generic PropertyPrivate newPropertyValue As StringPublic Property NewProperty() As Stringà à à Getà à à à à à Return newPropertyValueà à à End Getà à à Set(ByVal value As String)à à à à à à newPropertyValue valueà à à End SetEnd Property Generic MethodPublic Sub MyMethod()à à à method codeEnd Sub The resulting serialized class can create unique values in the file based on the code you supply. For example, a real-estate class might update a the value and address of a house but the class would serialize a calculated market classification as well. The New subroutine will look something like this: Public Sub New( _à à à ByVal info As SerializationInfo, _à à à ByVal context As StreamingContext)à à à initialize your program variables fromà à à a serialized data storeà à à Parm1Name info.GetString(a)à à à Parm1Value info.GetInt32(b)à à à New sub continues ... When Deserialize is called on a BinaryFormatter object, this sub is executed and a SerializationInfo object is passed to the New subroutine. New can then do whatever is necessary with the serialized data values. For example ... MsgBox(This is Parm1Value Times Pi: _à à à (Parm1Value * Math.PI).ToString) The reverse happens when Serialize is called, but the BinaryFormatter object calls GetObjectData instead. Public Sub GetObjectData( _à à à ByVal info As SerializationInfo, _à à à ByVal context As StreamingContext) _à à à Implements ISerializable.GetObjectDataà à à update the serialized data storeà à à from program variablesà à à If Parm2Name Test Thenà à à à à à info.AddValue(a, This is a test.)à à à Elseà à à à à à info.AddValue(a, No test this time.)à à à End Ifà à à info.AddValue(b, 2) Notice that the data is added to the serialized file as name/value pairs. A lot of the web pages Ive found in writing this article dont seem to have actual working code. One wonders whether the author actually executed any code before writing the article sometimes.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.