Class GraphJson

java.lang.Object
com.codename1.graph.GraphJson

public final class GraphJson extends Object

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.

  • 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: the graph
      • nodes: the nodes to write, with the edges that run between two of them and no groups; null writes everything
      • codec: 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
      Throws:
      IOException
    • 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: the document
      • into: the model that receives the nodes, edges and groups
      • codec: 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
      Throws:
      IOException
    • 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
      Throws:
      IOException