Class Codec
Index
Constructors
constructor
Parameters
- document: XMLDocument = ...
Returns Codec
Properties
document
The owner document of the codec.
elements
Lookup table for resolving IDs to elements.
encode Defaults
Specifies if default values should be encoded.
objects
Maps from IDs to objects.
Methods
add Element
decode
Decodes the given XML node. The optional "into" argument specifies an existing object to be used. If no object is given, then a new instance is created using the constructor from the codec.
The function returns the passed in object or the new instance if no object was given.
Parameters
- node: null | Element
XML node to be decoded.
Optionalinto: objectOptional object to be decoded into.
Returns null | object
- node: null | Element
decode Cell
Decodes cells that have been encoded using inversion, ie. where the user object is the enclosing node in the XML, and restores the group and graph structure in the cells. Returns a new Cell instance that represents the given node.
Parameters
- node: Element
XML node that contains the cell data.
- restoreStructures: boolean = true
Optional boolean indicating whether the graph structure should be restored by calling insert and insertEdge on the parent and terminals, respectively. Default is
true.
Returns null | Cell
- node: Element
encode
Encodes the specified object and returns the resulting XML node.
Parameters
- obj: any
Object to be encoded.
Returns null | Element
- obj: any
encode Cell
Encoding of cell hierarchies is built-into the core, but is a higher-level function that needs to be explicitly used by the respective object encoders (e.g. ModelCodec, ChildChangeCodec and RootChangeCodec). This implementation writes the given cell and its children as a (flat) sequence into the given node. The children are not encoded if the optional includeChildren is false. The function is in charge of adding the result into the given node and has no return value.
Parameters
Returns void
get Element By Id
Returns the element with the given ID from document.
Parameters
- id: string
String that contains the ID.
Returns Element
- id: string
get Id
Returns the ID of the specified object. This implementation calls reference first and if that returns null handles the object as an Cell by returning their IDs using Cell.getId. If no ID exists for the given cell, then an on-the-fly ID is generated using CellPath.create.
Parameters
- obj: any
Object to return the ID for.
Returns string
- obj: any
get Object
insert Into Graph
is Cell Codec
Returns true if the given codec is a cell codec. This uses CellCodec.isCellCodec to check if the codec is of the given type.
Parameters
- codec: null | ObjectCodec
Returns boolean
lookup
Hook for subclassers to implement a custom lookup mechanism for cell IDs. This implementation always returns null.
Example:
const codec = new Codec();
codec.lookup(id)
{
return model.getCell(id);
};Parameters
- id: string
ID of the object to be returned.
Returns any
- id: string
put Object
Associates the given object with the given ID and returns the given object.
Parameters
- id: string
ID for the object to be associated with.
- obj: any
Object to be associated with the ID.
Returns any
- id: string
reference
Hook for subclassers to implement a custom method for retrieving IDs from objects. This implementation always returns null.
Example:
const codec = new Codec();
codec.reference(obj)
{
return obj.getCustomId();
};Parameters
- obj: any
Object whose ID should be returned.
Returns any
- obj: any
set Attribute
Sets the attribute on the specified node to value. This is a helper method that makes sure the attribute and value arguments are not null.
Parameters
- node: Element
XML node to set the attribute for.
- attribute: string
The name of the attribute to be set.
- value: any
New value of the attribute.
Returns void
- node: Element
update Elements
Returns void

XML codec for JavaScript object graphs. See ObjectCodec for a description of the general encoding/decoding scheme. This class uses the codecs registered in CodecRegistry for encoding/decoding each object.
References
In order to resolve references, especially forward references, the Codec constructor must be given the document that contains the referenced elements.
Examples
The following code is used to encode a graph model.
WARN: as of version 0.6.0, the codecs provided by maxGraph are no longer registered by default, they MUST be registered before performing
encodeordecode. For instance, you can use the registerAllCodecs function (or other related functions) to register the codecs.Example
Using the code below, an XML document is decoded into an existing model. The document may be obtained using parseXml for parsing an XML string.
Example
This example demonstrates parsing a list of isolated cells into an existing graph model. Note that the cells do not have a parent reference so they can be added anywhere in the cell hierarchy after parsing.
Example
Using the following code, the selection cells of a graph are encoded and the output is displayed in a dialog box.
Newlines in the XML can be converted to
, in which case a '
' argument must be passed to getXml as the second argument.
Debugging
For debugging, I/O you can use the following code to get the sequence of encoded objects:
Note that the I/O system adds object codecs for new object automatically. For decoding those objects, the constructor should be written as follows: