Class GraphJson
Writes a graph as JSON and reads it back, to save a diagram or send it.
DefaultGraphModel model = new DefaultGraphModel();
model.addNode(new GraphNode("a", "Start", 20, 20, 100, 50));
NodeGraph graph = new NodeGraph(model);
String saved = GraphJson.toJson(model);
Storage.getInstance().writeObject("diagram", saved);
// later
DefaultGraphModel loaded = GraphJson.fromJson(saved);
graph.setModel(loaded);
Everything the model classes hold is written: nodes with their bounds, shape, ports and port values, edges with their line, arrows and route, and groups and lanes. Values left at their default are omitted, so a small graph is a small document.
getUserData is the application's, and is written only through a
UserDataCodec. A port value is written when it is a string, a number or
a boolean.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceTurns the application'sgetUserDataobjects into JSON values and back. -
Method Summary
Modifier and TypeMethodDescriptionstatic DefaultGraphModelReads a graphtoJsonwrote, without user data.static voidread(String json, DefaultGraphModel into, GraphJson.UserDataCodec codec) Reads a graphtoJsonwrote into a model, adding to what it holds.static voidreadMap(Map<String, Object> root, DefaultGraphModel into, GraphJson.UserDataCodec codec) Reads the treetoMapbuilt, as a JSON parser returns it.static StringtoJson(GraphModel model) The whole graph as a JSON object, without user data.static StringtoJson(GraphModel model, Collection<GraphNode> nodes, GraphJson.UserDataCodec codec) The graph, or part of it, as a JSON object.toMap(GraphModel model, Collection<GraphNode> nodes, GraphJson.UserDataCodec codec) The same treetoJsonwrites, to embed in a larger document.
-
Method Details
-
toJson
The whole graph as a JSON object, without user data. -
toJson
public static String toJson(GraphModel model, Collection<GraphNode> nodes, GraphJson.UserDataCodec codec) The graph, or part of it, as a JSON object.
Parameters
model: the graphnodes: the nodes to write, with the edges that run between two of them and no groups; null writes everythingcodec: writes user data, or null to leave it out
-
toMap
public static Map<String,Object> toMap(GraphModel model, Collection<GraphNode> nodes, GraphJson.UserDataCodec codec) The same treetoJsonwrites, to embed in a larger document. -
fromJson
Reads a graph
toJsonwrote, without user data.Throws
IOException: when the text is not a JSON object, or a node or an edge in it has no id
- Throws:
IOException
-
read
public static void read(String json, DefaultGraphModel into, GraphJson.UserDataCodec codec) throws IOException Reads a graph
toJsonwrote into a model, adding to what it holds.Parameters
json: the documentinto: the model that receives the nodes, edges and groupscodec: reads user data, or null to ignore it
Throws
IOException: when the text is not well-formed JSON or not a graph document, orreadMaprefuses it; the model is then as it was
- Throws:
IOException
-
readMap
public static void readMap(Map<String, Object> root, DefaultGraphModel into, GraphJson.UserDataCodec codec) throws IOExceptionReads the tree
toMapbuilt, as a JSON parser returns it. The whole tree is checked before anything is added: an id taken twice, or an edge between nodes that are nowhere, refuses all of it. Each addition then tells the model's listeners as it is made. A node's group and an edge's ports are taken as written, since a group or a port may be added later. A port value comes back as JSON has it: a string, a boolean, aLongfor a whole number and aDoublefor any other.Throws
IOException: when a node, an edge or a group has no id or one that is taken, or an edge names a node that is neither in the tree nor in the model
- Throws:
IOException
-