/// <summary> /// Creates GraphJson for a single graph element. /// </summary> private static JObject JsonFromElement(IElement element, GraphJsonSettings settings) { if (element == null) { throw new ArgumentNullException(nameof(element)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } var isEdge = element is IEdge; var map = element.ToDictionary(t => t.Key, t => t.Value); if (isEdge) { var edge = element as IEdge; var source = edge.GetVertex(Direction.In).Id; var target = edge.GetVertex(Direction.Out).Id; var caption = edge.Label; map.Add(settings.SourceProp, source); map.Add(settings.TargetProp, target); map.Add(settings.EdgeCaptionProp, caption); } else { map.Add(settings.IdProp, element.Id); } return(JObject.FromObject(map, Serializer)); }
/// <summary> /// Write the data in a Graph to a GraphJson OutputStream. /// </summary> /// <param name="filename">the JSON file to write the Graph data to</param> /// <param name="graph">the graph to serialize</param> /// <param name="settings">Contains field names that the writer will use to map to BluePrints</param> public static void OutputGraph(IGraph graph, string filename, GraphJsonSettings settings) { if (graph == null) { throw new ArgumentNullException(nameof(graph)); } if (string.IsNullOrWhiteSpace(filename)) { throw new ArgumentNullException(nameof(filename)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } using (var fos = File.Open(filename, FileMode.Create)) { OutputGraph(graph, fos, settings); } }
/// <summary> /// Write the data in a Graph to a GraphJson OutputStream. /// </summary> /// <param name="jsonOutputStream">the OutputStream to write to</param> /// <param name="graph">the graph to serialize</param> /// <param name="settings">Contains field names that the writer will use to map to BluePrints</param> public static void OutputGraph(IGraph graph, Stream jsonOutputStream, GraphJsonSettings settings) { if (graph == null) { throw new ArgumentNullException(nameof(graph)); } if (jsonOutputStream == null) { throw new ArgumentNullException(nameof(jsonOutputStream)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } var sw = new StreamWriter(jsonOutputStream); var jg = new JsonTextWriter(sw); jg.WriteStartObject(); jg.WritePropertyName("nodes"); jg.WriteStartArray(); foreach (var v in graph.GetVertices()) { jg.WriteRawValue(JsonFromElement(v, settings).ToString()); } jg.WriteEndArray(); jg.WritePropertyName("edges"); jg.WriteStartArray(); foreach (var e in graph.GetEdges()) { jg.WriteRawValue(JsonFromElement(e, settings).ToString()); } jg.WriteEndArray(); jg.WriteEndObject(); jg.Flush(); }
/// <summary> /// Input the GraphJson stream data into the graph. /// More control over how data is streamed is provided by this method. /// </summary> /// <param name="inputGraph">the graph to populate with the GraphJson data</param> /// <param name="jsonInputStream">a Stream of GraphJson data</param> /// <param name="bufferSize">the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)</param> /// <param name="settings">Contains field names that the reader will use to parse the graph</param> public static void InputGraph(IGraph inputGraph, Stream jsonInputStream, int bufferSize, GraphJsonSettings settings) { if (inputGraph == null) { throw new ArgumentNullException(nameof(inputGraph)); } if (jsonInputStream == null) { throw new ArgumentNullException(nameof(jsonInputStream)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (bufferSize <= 0) { throw new ArgumentException("bufferSize must be greater than zero"); } StreamReader sr = null; try { sr = new StreamReader(jsonInputStream); using (var jp = new JsonTextReader(sr)) { sr = null; // if this is a transactional graph then we're buffering var graph = BatchGraph.Wrap(inputGraph, bufferSize); var serializer = JsonSerializer.Create(null); while (jp.Read() && jp.TokenType != JsonToken.EndObject) { var fieldname = Convert.ToString(jp.Value); switch (fieldname) { case "nodes": jp.Read(); while (jp.Read() && jp.TokenType != JsonToken.EndArray) { var props = new Dictionary <string, object>(); var node = (JObject)serializer.Deserialize(jp); object id = null; foreach (var val in node) { if (val.Key == settings.IdProp) { id = val.Value.ToObject <object>(); } else { props.Add(val.Key, val.Value.ToObject <object>()); } } var vertex = graph.AddVertex(id); vertex.SetProperties(props); } break; case "edges": jp.Read(); while (jp.Read() && jp.TokenType != JsonToken.EndArray) { var props = new Dictionary <string, object>(); var node = (JObject)serializer.Deserialize(jp); object id = null; object source = null; object target = null; var caption = string.Empty; foreach (var val in node) { if (val.Key == settings.IdProp) { id = val.Value.ToObject <object>(); } else if (val.Key == settings.EdgeCaptionProp) { caption = val.Value.ToString(); } else if (val.Key == settings.SourceProp) { source = val.Value.ToObject <object>(); } else if (val.Key == settings.TargetProp) { target = val.Value.ToObject <object>(); } else { props.Add(val.Key, val.Value.ToObject <object>()); } } if (source == null) { throw new IOException("Edge has no source"); } if (target == null) { throw new IOException("Edge has no target"); } var edge = graph.AddEdge(id, graph.GetVertex(source), graph.GetVertex(target), caption); edge.SetProperties(props); } break; } } graph.Commit(); } } finally { if (sr != null) { sr.Dispose(); } } }