Skip to content

Instantly share code, notes, and snippets.

@gsavin
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsavin/f2ecbc3cbd255f4d9262 to your computer and use it in GitHub Desktop.
Save gsavin/f2ecbc3cbd255f4d9262 to your computer and use it in GitHub Desktop.
GraphStream : JSON output
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.util.Map;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.Edge;
import org.graphstream.graph.implementations.AdjacencyListGraph;
import org.graphstream.stream.file.FileSourceDGS;
public class Convert2JSON {
public static void main(String... args) {
FileSourceDGS dgs = new FileSourceDGS();
Graph g = new AdjacencyListGraph("dgs2json");
dgs.addSink(g);
try {
if (args.length > 0) {
dgs.readAll(ConvertData.class.getResourceAsStream(args[0]));
}
else {
dgs.readAll(new InputStreamReader(System.in));
}
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
dgs.removeSink(g);
PrintStream out = null;
if (args.length > 1) {
try {
out = new PrintStream(args[1]);
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
}
}
else {
out = System.out;
}
outputJSON(out, g);
}
public static String getAttributes(Element e) {
StringBuilder buffer = new StringBuilder("{");
String sep = "";
for (String key: e.getAttributeKeySet()) {
buffer.append(sep).append("\"" + key + "\":");
getAttributeValue(buffer, e.getAttribute(key));
sep = ",";
}
return buffer.append("}").toString();
}
public static void getAttributeValue(StringBuilder buffer, Object o) {
if (o == null) {
buffer.append("\"\"");
}
else if (o instanceof Number || o instanceof Boolean) {
buffer.append(o.toString());
}
else if (o.getClass().isArray()) {
buffer.append("[");
for (int i = 0; i < Array.getLength(o); i++) {
buffer.append(i > 0 ? "," : "");
getAttributeValue(buffer, Array.get(o, i));
}
buffer.append("]");
}
else if (o instanceof Map<?,?>) {
Map<?,?> m = (Map<?,?>) o;
String sep = "";
buffer.append("{");
for (Object key: m.keySet()) {
buffer
.append(sep)
.append('"')
.append(key.toString())
.append("\":");
getAttributeValue(buffer, m.get(key));
sep = ",";
}
buffer.append("}");
}
else {
buffer.append('"').append(o.toString()).append('"');
}
}
public static void outputJSON(PrintStream out, Graph g) {
out.printf("{");
out.printf("\"nodes\": [");
for (int i=0; i<g.getNodeCount(); i++) {
Node n = g.getNode(i);
out.printf("%s{\"id\": \"%s\", \"attributes\":\"%s\"}",
i > 0 ? ", " : "",
n.getId(),
getAttributes(s));
}
out.printf("],");
out.printf("\"links\": [");
for (int i=0; i<g.getEdgeCount(); i++) {
Edge e = g.getEdge(i);
String cls = e.hasAttribute("ui.class") ? (String) e.getAttribute("ui.class") : "normal";
out.printf("%s{\"id\": \"%s\", \"source\":%d, \"target\":%d, \"attributes\":\"%s\"}",
i > 0 ? ", " : "",
e.getId(),
e.getSourceNode().getIndex(),
e.getTargetNode().getIndex(),
getAttributes(e));
}
out.printf("]");
out.printf("}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment