documentationfor yFiles for HTML 3.0.0.1

Automatically Serialized Types

The I/O mechanism in yFiles for HTML is designed to serialize objects automatically, where possible, without requiring custom serialization code. The following types can be (de)serialized automatically:

  • Primitive data types (boolean, string, number)
  • Enums
  • JSON-serializable values:
    • For these, the JSON representation will be embedded or parsed from the GraphML.
  • Classes that follow these rules:
    • They provide a public default constructor (without arguments).
    • All their relevant properties are readable and writable.
    • They are yFiles for HTML classes, i.e., extend a yFiles for HTML class or implement one or more yFiles for HTML interfaces.
    • There must exist a mapping between the type and the fully qualified element name. When using a markup extension, only the markup extension type needs to be mapped.

Therefore, you typically don’t need to worry about serializing these types. The graph I/O mechanism can automatically construct objects that follow the rules above using reflection.

When designing your custom data, keep automatic serializability in mind.

Map the Namespace of Your Custom Types

When serializing custom data consisting of structured types using yFiles for HTML’s default XML serialization, you must provide a custom XML namespace using a namespace mapping. The mapping, which associates a type with an XML namespace (and optionally an XML tag name), is defined by registering a namespace mapping with GraphMLIOHandler.

The following example demonstrates how to define an XML namespace mapping:

graphMLIOHandler.addTypeInformation(Employee, {
  name: 'Employee',
  xmlNamespace: 'http://www.yworks.com/xml/yfiles-for-html/orgchart/1.0'
})

The XML namespace should be a unique namespace URI, ideally owned by the application developer. It does not need to point to an existing or valid document URL. It is best practice to include a version number in the URI.

You can also register multiple types at once using the addXamlNamespaceMapping convenience method. This method accepts an object where the keys are XML tag names and the values are the corresponding types.

graphMLIOHandler.addXamlNamespaceMapping(
  'http://www.yworks.com/xml/yfiles-for-html/orgchart/1.0',
  {
    Employee: Employee,
    Department: Department
  }
)