Search
Serialization of Diagrams

Saving and Loading Diagrams

Diagrams can be serialized in byte sequences wrapped by Java streams. Such streams may represent files, memory buffers, communication pipes, TCP sockets and so on. In serialized form a diagram might be saved in a database record too. To save the current diagram into a stream call the saveTo method, and respectively to load a diagram from a stream call loadFrom. Three shortcut methods are provided for work with files. saveTo creates a disk file and a stream that wraps it and invokes saveTo. loadFrom opens a read-stream on a disk file and invokes loadFrom.

Saving Attached Data

All objects referenced by the diagram, such as images and fonts, are serialized together with the diagram elements. The tag objects that are associated with diagram elements are also saved if they implement the Serializable interface. If a tag is not serializable it won't be saved by the save- methods.

XML Serialization

It is possible to save a diagram as an XML document. To do that, call the saveToXml method of the Diagram class. To deserialize a digram back from its XML representation, call the loadFromXml method.

Custom item classes can implement XML serialization for their additional properties by overriding the saveToXml and loadFromXml methods. In addition, such classes must specify a string to use as their identifier in the XML document by calling registerItemClass.

The control raises serializeTag and deserializeTag to enable serialization of complex Id and Tag objects without overriding the item classes. The PropertyName event argument specifies which property is being serialized.

JSON Serialization

To serialize a diagram as a JSON string, call its saveToJson method. To deserialize a diagram back from its JSON representation, call the loadFromJson method. The saveToJsonFile and loadFromJsonFile methods serialize the JSON strings to / from files.

Custom item classes can implement JSON serialization for their additional properties by overriding the saveToJson and loadFromJson methods. Such classes must be registered for serialization by calling the registerItemClass method and providing a string identifier for the clientClass parameter.

The control raises jsonSerializeTag and jsonDeserializeTag to enable serialization of complex Id and Tag objects without overriding the item classes. The PropertyName event argument specifies which property is being serialized.