Data Serialization (5.0)
Data that is transferred between processes, persisted in the database or exported to a file, must be serialized. Usage Engine implements its own serialization mechanism for input and output streams, where each object to be serialized is referred to as a Storable. The serialization also includes a version control mechanism where an object can populate itself differently depending on the version of the incoming stream.
In the DTK there is only one programmatic interface for handling object serialization. Classes for this are located in the package devkit.storable
. However, depending on the target, the system handles serialization in two different ways:
Plain streams
Plain streams are used in a running system. As with regular Java serialization, each element must be written and read in the exact same order. For performance reasons, these stream types are used within a running system in inter-process communication.
XML and JSON streams
In XML and JSON streams, each element is associated with a user-defined name tag. The tag is used to locate the value. XML and JSON streams are used for database persistence and when objects are exported to a file, imported into another Usage Engine system, and during upgrades. The XML and JSON streams provide the ability to make use of a supplied version number to be able to automatically upgrade a configuration in a running system.
An object to be serialized must implement the DRStorable
interface. This interface contains two methods that serialize the object: readFrom
and writeTo
: The methods operate on a DRInputStream
and DROutputStream
respectively, where data of various types may be obtained or set using a name tag.
Note!
All storables that are to be persisted, in either the database or exported/imported, should have names defined using DRStorableId
. Other storables may skip this, however, it is encouraged to always use unique storable ids.
The readFrom
method is handed a version parameter. When storables are deserialized, this parameter may be used to detect if the input stream is of the same version as when it was serialized. If not, different actions may be taken to populate values correctly.