static void create_json(String directory, Graph graph, int index) { List <json_shape> json_shapes = new List <json_shape>(); List <json_connection> json_connections = new List <json_connection>(); foreach (Microsoft.Msagl.Drawing.Node node in graph.Nodes) { var bb = new bounding_box(node.BoundingBox.Left, node.BoundingBox.Right, node.BoundingBox.Top, node.BoundingBox.Bottom); json_shapes.Add(new json_shape(bb, node.Attr.Id.Split()[0])); } foreach (Microsoft.Msagl.Drawing.Edge edge in graph.Edges) { var bb_src = new bounding_box(edge.SourceNode.BoundingBox.Left, edge.SourceNode.BoundingBox.Right, edge.SourceNode.BoundingBox.Top, edge.SourceNode.BoundingBox.Bottom); var bb_dst = new bounding_box(edge.TargetNode.BoundingBox.Left, edge.TargetNode.BoundingBox.Right, edge.TargetNode.BoundingBox.Top, edge.TargetNode.BoundingBox.Bottom); json_connections.Add(new json_connection( new json_shape(bb_src, edge.SourceNode.Id.Split()[0]), new json_shape(bb_dst, edge.TargetNode.Id.Split()[0]) )); } JavaScriptSerializer jss = new JavaScriptSerializer(); string Json_Shape = new JavaScriptSerializer().Serialize(json_shapes); string Json_connection = new JavaScriptSerializer().Serialize(json_connections); string tmp1 = $"{{ \"Shapes\" : {Json_Shape} }}"; string tmp2 = $"{{ \"Connections\" : {Json_connection} }}"; string tmp4 = $"{{ \"Height\" : {(int)graph.Height} }}"; string tmp5 = $"{{ \"Width\" : {(int)graph.Width} }}"; string tmp3 = "[" + tmp1 + "," + tmp2 + "," + tmp4 + "," + tmp5 + "]"; string json = $"{{ \"JSON\" : {tmp3} }}"; string jsonPath = System.IO.Path.Combine(directory, index + ".txt"); System.IO.File.WriteAllText(jsonPath, json); }
public json_shape(bounding_box boundingBox, String shapeType) { BoundingBox = boundingBox; ShapeType = shapeType; }