예제 #1
0
 public static GraphModel ParseGraphJson(string json, string root)
 {
     ZLog.Log("ParseGraphJson..." + json.Length);
     if (string.IsNullOrEmpty(json))
     {
         return(null);
     }
     try
     {
         var        obj   = JsonMapper.ToObject(json);
         GraphModel model = new GraphModel(root)
         {
             name = (string)obj["name"],
         };
         if (((IDictionary)obj).Contains("parent"))
         {
             model.Parent = (string)obj["parent"];
         }
         var center = (string)obj["center"];
         model.SetCenter(center);
         var nodes = obj["nodes"];
         if (nodes != null && nodes.IsArray)
         {
             foreach (JsonData item in nodes)
             {
                 NodeInfo node = new NodeInfo
                 {
                     UID    = (string)item["uid"],
                     name   = (string)item["name"],
                     avatar = (string)item["avatar"],
                     type   = (string)item["type"],
                     parent = center
                 };
                 if (string.IsNullOrEmpty(node.avatar))
                 {
                     node.avatar = Path.Combine(root, "cover/" + node.name + ".png");
                 }
                 node.fullPath = Path.Combine(root, "entity/entity_" + node.UID + ".json");
                 model.AddNode(node, null);
             }
         }
         var edges = obj["edges"];
         if (edges != null && edges.IsArray)
         {
             foreach (JsonData item in edges)
             {
                 LinkInfo edge = new LinkInfo
                 {
                     UID       = (string)item["uid"],
                     name      = (string)item["name"],
                     direction = (DirectionType)(int)item["direction"]
                 };
                 var from = (string)item["from"];
                 edge.from = model.GetNode(from);
                 var to = (string)item["to"];
                 edge.to = model.GetNode(to);
                 if (((IDictionary)item).Contains("type"))
                 {
                     edge.type = (string)item["type"];
                 }
                 model.AddEdge(edge);
             }
             model.CheckVirtualNodes();
         }
         return(model);
     }
     catch (Exception e)
     {
         ZLog.Warning(json);
         ZLog.Warning(e.Message);
         return(null);
     }
 }