Serialize an instance to file
- Last Updated: March 19, 2024
- 2 minute read
- OpenEdge
- Version 12.8
- Documentation
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
SERIALIZABLEkeyword, as described in Use the 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 asNON-SERIALIZABLE, and the AVM will skip those during serialization. See the entries for eachDEFINEstatement to learn more.) - Any class members you want to include in the serialization that are not included by
default must be defined using the
SERIALIZABLEkeyword.
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:
- Instantiate a variable of type
Progress.IO.BinarySerializer(orProgress.IO.JsonSerializer). - Instantiate a variable of type
Progress.IO.FileOutputStreamwith your target filename as a parameter. This class provides functionality for writing to the file. - Invoke the
Serialize( )method of yourProgress.IO.BinarySerializerinstance and pass it the object to be serialized and theProgress.IO.FileOutputStreamobject. - Invoke the
Close( )method of theProgress.IO.FileOutputStreamobject.
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.
|
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 Pass object reference parameters section.