Created On:  06 April 2011

Problem:

In Visual COBOL for Visual Studio 2010, how does one define a class as being serializable and how is the serialization accomplished?

Resolution:

You can create a serializable class in Visual COBOL by using the following attribute:

class-id. mySerializableType
attribute System.Serializable().

The following is a sample that will serialize a COBOL object and write it to an XML file and then deserialize it to a new object and display the results.

      $set ilusing"System"
      $set ilusing"System.Collections.Generic"
      $set ilusing"System.Linq"
      $set ilusing"System.Text"
      $set ilusing"System.Xml"
      $set ilusing"System.Xml.Serialization"
      $set ilusing"System.IO"
      $set ilusing"System.Collections"
       program-id. Program1 as "vcSerial.Program1".
       working-storage section.
       01 instance1 type mySerializableType.
       01 serializer type XmlSerializer.
       01 streamWriter type StreamWriter.
       01 streamReader type StreamReader.
       01 deserializedInstance type mySerializableType.
       01 obj object.
       01 myexception type Exception.
       procedure division.
           *> Create and initialize a mySerializableType instance.
       
           set instance1 to new type mySerializableType
           set instance1::BoolValue to true
           set instance1::IntValue to 1
           set instance1::StringValue to "Test String"
           invoke instance1::ListValue::Add("List Item 1")
           invoke instance1::ListValue::Add("List Item 2")
           invoke instance1::ListValue::Add("List Item 3")
           set instance1::AnotherTypeValue to new type AnotherType
           set instance1::AnotherTypeValue::IntValue to 2
           set instance1::AnotherTypeValue::StringValue to "Inner Test String"

           *> Create the serializer
           set serializer to new type XmlSerializer(type of mySerializableType)

           *> Serialize the object to an XML file
           set streamwriter to type File::CreateText("VCXmlSerialization.xml")
           invoke serializer::Serialize(streamWriter, instance1)
           invoke streamwriter::Close()
           
           *>
           *> Deserialize from a XML file to an object instance.
           *>

           *> Deserialize the object
           set streamReader to type File::OpenText("VCXmlSerialization.xml")
             
           set deserializedInstance to serializer::Deserialize(streamReader) as type mySerializableType
           
           *> Dump the object
           display "BoolValue: ", deserializedInstance::BoolValue
           display "IntValue: ", deserializedInstance::IntValue
           display "StringValue: ", deserializedInstance::StringValue
           display "AnotherTypeValue.IntValue: ", deserializedInstance::AnotherTypeValue::IntValue
           display "AnotherTypeValue.StringValue: ", deserializedInstance::AnotherTypeValue::StringValue
           display "ListValue: "
           perform varying obj thru deserializedInstance::ListValue
              display obj::ToString
           end-perform
                       
           stop run.
       end program.     
       
       class-id. mySerializableType
       attribute System.Serializable().
       working-storage section.
       01 stringvalue string private.
       01 boolvalue   condition-value.       
       01 intvalue    binary-long.
       01 anothertypevalue type AnotherType.
       01 listvalue type List[string] value new type List[string].
       01 ignoredField  binary-long value 1 attribute System.NonSerialized().
       method-id get property StringValue.
       procedure division returning return-value as string.
            set return-value to stringvalue
            goback.
       end method.
       method-id set property StringValue.
       procedure division using set-value as string.
            set stringvalue to set-value
            goback.
       end method.
       method-id get property BoolValue.
       procedure division returning return-value as condition-value.
            set return-value to boolvalue
            goback.
       end method.
       method-id set property BoolValue.
       procedure division using set-value as condition-value.
            set boolvalue to set-value
            goback.
       end method.
       method-id get property IntValue.
       procedure division returning return-value as binary-long.
            set return-value to intvalue
            goback.
       end method.
       method-id set property IntValue.
       procedure division using set-value as binary-long.
            set intvalue to set-value
            goback.
       end method.
       method-id get property AnotherTypeValue.
       procedure division returning return-value as type AnotherType.
            set return-value to anothertypevalue
            goback.
       end method.
       method-id set property AnotherTypeValue.
       procedure division using set-value as type AnotherType.
            set anothertypevalue to set-value
            goback.
       end method.
       method-id get property ListValue.
       procedure division returning return-value as type List[string].
            set return-value to listvalue
            goback.
       end method.
       method-id set property ListValue.
       procedure division using set-value as type List[string].
            set listvalue to set-value
            goback.
       end method.
       
       end class.
       
       class-id. AnotherType
       attribute System.Serializable().
       working-storage section.
       01 stringvalue string.
       01 intvalue    binary-long.
       method-id get property StringValue.
       procedure division returning set-value as string.
            set set-value to stringvalue
            goback.
       end method.
       method-id set property StringValue.
       procedure division using set-value as string.
            set stringvalue to set-value
            goback.
       end method.
       
       method-id get property IntValue.
       procedure division returning return-value as binary-long.
            set return-value to intvalue
            goback.
       end method.
       method-id set property IntValue.
       procedure division using set-value as binary-long.
            set intvalue to set-value
            goback.
       end method.
       
       end class.