Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class GraphJson

  1. Object
  2. 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 types

interface GraphJson.UserDataCodecTurns the application’s getUserData objects into JSON values and back.

Methods

public static String toJson(GraphModel model)The whole graph as a JSON object, without user data.
public static String toJson(GraphModel model, Collection<GraphNode> nodes, GraphJson.UserDataCodec codec)The graph, or part of it, as a JSON object.
public static Map<String, Object> toMap(GraphModel model, Collection<GraphNode> nodes, GraphJson.UserDataCodec codec)The same tree toJson writes, to embed in a larger document.
public static DefaultGraphModel fromJson(String json) throws IOExceptionReads a graph toJson wrote, without user data.
public static void read(String json, DefaultGraphModel into, GraphJson.UserDataCodec codec) throws IOExceptionReads a graph toJson wrote into a model, adding to what it holds.
public static void readMap(Map<String, Object> root, DefaultGraphModel into, GraphJson.UserDataCodec codec) throws IOExceptionReads the tree toMap built, as a JSON parser returns it.

Inherited methods

Method details

toJson

public static String toJson(GraphModel model)
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 GraphModel
the graph
nodes Collection<GraphNode>
the nodes to write, with the edges that run between two of them and no groups; null writes everything
codec GraphJson.UserDataCodec
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 tree toJson writes, to embed in a larger document.

fromJson

public static DefaultGraphModel fromJson(String json) throws IOException
Reads a graph toJson wrote, without user data.

Throws

IOException
when the text is not a JSON object, or a node or an edge in it has no id

read

public static void read(String json, DefaultGraphModel into, GraphJson.UserDataCodec codec) throws IOException
Reads a graph toJson wrote into a model, adding to what it holds.

Parameters

json String
the document
into DefaultGraphModel
the model that receives the nodes, edges and groups
codec GraphJson.UserDataCodec
reads user data, or null to ignore it

Throws

IOException
when the text is not well-formed JSON or not a graph document, or readMap refuses it; the model is then as it was

readMap

public static void readMap(Map<String, Object> root, DefaultGraphModel into, GraphJson.UserDataCodec codec) throws IOException
Reads the tree toMap built, 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, a Long for a whole number and a Double for 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