Try OpenEdge Now
skip to main content
Object-oriented Programming
Programming with Class-based Objects : Serialization and deserialization : Serializing an instance to file
 

Serializing an instance to file

Before an instance can be serialized to file, some basic conditions must be met:
*The class of the instance you want to serialize, and all of that class's super classes, must be defined using the SERIALIZABLE keyword, as described in Usingthe CLASS construct.
*If there are any class members that are themselves class-based objects, those classes must also be defined as SERIALIZABLE. (Alternatively, the class members themselves can be defined as NON-SERIALIZABLE, and the AVM will skip those during serialization. See the entries for each DEFINE statement to learn more.)
*Any class members you want to include in the serialization that are not included by default must be defined using the SERIALIZABLE keyword.
The ABL offers a choice of two formats for a serialized instance: binary and JSON. Aside from the format itself, these two formats differ in default behavior. For example, binary serialization includes all data members, properties, temp-tables, and ProDataSets by default, regardless of their scope, whereas JSON serialization only includes those defined as public. The steps given below for serializing an instance use binary format, but the same general process can be used for JSON.
To serialize an instance:
1. Instantiate a variable of type Progress.IO.BinarySerializer (or Progress.IO.JsonSerializer).
2. Instantiate a variable of type Progress.IO.FileOutputStream with your target filename as a parameter. This class provides functionality for writing to the file.
3. Invoke the Serialize( ) method of your Progress.IO.BinarySerializer instance and pass it the object to be serialized and the Progress.IO.FileOutputStream object.
4. Invoke the Close( ) method of the Progress.IO.FileOutputStream object.
The following code example shows myObj, an instance of Acme.MyClass, being serialized to a binary file MyClass.bin. The corresponding steps are numbered with comments.
DEFINE VARIABLE myFileOutStream AS Progress.IO.FileOutputStream.
DEFINE VARIABLE mySerializer AS Progress.IO.BinarySerializer.

DEFINE VARIABLE myObj AS Acme.MyClass.

myObj = NEW Acme.MyClass().

/* 1 */
mySerializer = NEW Progress.IO.BinarySerializer().

/* 2 */
myFileOutStream = NEW Progress.IO.FileOutputStream("MyClass.bin").

/* 3 */
mySerializer:Serialize(myObj, myFileOutStream).

/* 4 */
myFileOutStream:Close().
Beyond the special cases mentioned above, there are additional restrictions and special behaviors for serializing some types of data. Refer to the full list of these in the Passingobject reference parameters section.