Highlighted
Absent Member.
Absent Member.
1473 views

How make a Cobol.Net class serializable?

Jump to solution

[Migrated content. Thread originally posted on 16 March 2011]

Hi

We are building a new web solution (VB.NET) on top of an existing COBOL appliaction.

We will use WCF services (VB.NET) as a middle layer to get all data to the web application.
Below the WCF layer we will have classes written in COBOL.NET, that calls the old code written in COBOL.

The problem we are facing is that if we have classes defined in the COBOL.NET layer, and instantiate these objects, fill the objects with data and passes it them up to the wcf service, there will be empty objects when they finally get to the web solution.

I figured it out that it has do with object serialization?!
In VB.NET you can set a class serializable with an attribute
for example
_
Public Class Person
...
End Class

But, how do I do this when having a class written in COBOL.NET?

Regards
Mats Bernerfalk
0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert
You can do the same thing 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.

View solution in original post

0 Likes
1 Reply
Highlighted
Micro Focus Expert
Micro Focus Expert
You can do the same thing 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.

View solution in original post

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.