Example #1
0
        /// <summary>
        ///     Reads an individual Edge from JSON.  The edge must match the accepted GraphSON format.
        /// </summary>
        /// <param name="json">a single edge in GraphSON format as a Stream</param>
        /// <param name="out_"></param>
        /// <param name="in_"></param>
        /// <param name="factory">the factory responsible for constructing graph elements</param>
        /// <param name="mode">the mode of the GraphSON</param>
        /// <param name="propertyKeys">a list of keys to include when reading of element properties</param>
        public static IEdge EdgeFromJson(JObject json, IVertex out_, IVertex in_,
                                         IElementFactory factory, GraphSonMode mode,
                                         IEnumerable <string> propertyKeys)
        {
            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }
            if (out_ == null)
            {
                throw new ArgumentNullException(nameof(out_));
            }
            if (in_ == null)
            {
                throw new ArgumentNullException(nameof(in_));
            }
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            var graphson = new GraphSonUtility(mode, factory, null, propertyKeys);

            return(graphson.EdgeFromJson(json, out_, in_));
        }
Example #2
0
        /// <summary>
        ///     Reads an individual Vertex from JSON.  The vertex must match the accepted GraphSON format.
        /// </summary>
        /// <param name="json">a single vertex in GraphSON format as Jackson JsonNode</param>
        /// <param name="factory">the factory responsible for constructing graph elements</param>
        /// <param name="mode">the mode of the GraphSON</param>
        /// <param name="propertyKeys">a list of keys to include on reading of element properties</param>
        public static IVertex VertexFromJson(JObject json, IElementFactory factory, GraphSonMode mode,
                                             IEnumerable <string> propertyKeys)
        {
            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            var graphson = new GraphSonUtility(mode, factory, propertyKeys, null);

            return(graphson.VertexFromJson(json));
        }
        /// <summary>
        ///     Write the data in a Graph to a JSON OutputStream.
        /// </summary>
        /// <param name="jsonOutputStream">the JSON OutputStream to write the Graph data to</param>
        /// <param name="vertexPropertyKeys">the keys of the vertex elements to write to JSON</param>
        /// <param name="edgePropertyKeys">the keys of the edge elements to write to JSON</param>
        /// <param name="mode">determines the format of the GraphSON</param>
        public void OutputGraph(Stream jsonOutputStream, IEnumerable <string> vertexPropertyKeys,
                                IEnumerable <string> edgePropertyKeys, GraphSonMode mode)
        {
            if (jsonOutputStream == null)
            {
                throw new ArgumentNullException(nameof(jsonOutputStream));
            }

            var sw = new StreamWriter(jsonOutputStream);
            var jg = new JsonTextWriter(sw);

            var graphson = new GraphSonUtility(mode, null, vertexPropertyKeys, edgePropertyKeys);

            jg.WriteStartObject();

            jg.WritePropertyName(GraphSonTokens.Mode);
            jg.WriteValue(mode.ToString());

            jg.WritePropertyName(GraphSonTokens.Vertices);
            jg.WriteStartArray();
            foreach (var v in _graph.GetVertices())
            {
                jg.WriteRawValue(graphson.JsonFromElement(v).ToString());
            }

            jg.WriteEndArray();

            jg.WritePropertyName(GraphSonTokens.Edges);
            jg.WriteStartArray();
            foreach (var e in _graph.GetEdges())
            {
                jg.WriteRawValue(graphson.JsonFromElement(e).ToString());
            }

            jg.WriteEndArray();

            jg.WriteEndObject();
            jg.Flush();
        }
        /// <summary>
        ///     Input the JSON 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 JSON data</param>
        /// <param name="jsonInputStream">a Stream of JSON data</param>
        /// <param name="bufferSize">the amount of elements to hold in memory before committing a transactions (only valid for TransactionalGraphs)</param>
        /// <param name="edgePropertyKeys"></param>
        /// <param name="vertexPropertyKeys"></param>
        public static void InputGraph(IGraph inputGraph, Stream jsonInputStream, int bufferSize,
                                      IEnumerable <string> edgePropertyKeys, IEnumerable <string> vertexPropertyKeys)
        {
            if (inputGraph == null)
            {
                throw new ArgumentNullException(nameof(inputGraph));
            }
            if (jsonInputStream == null)
            {
                throw new ArgumentNullException(nameof(jsonInputStream));
            }
            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 elementFactory = new GraphElementFactory(graph);

                    // ReSharper disable PossibleMultipleEnumeration
                    var graphson = new GraphSonUtility(GraphSonMode.NORMAL, elementFactory, vertexPropertyKeys,
                                                       edgePropertyKeys);
                    // ReSharper restore PossibleMultipleEnumeration

                    var serializer = JsonSerializer.Create(null);

                    while (jp.Read() && jp.TokenType != JsonToken.EndObject)
                    {
                        var fieldname = Convert.ToString(jp.Value);
                        switch (fieldname)
                        {
                        case GraphSonTokens.Mode:
                        {
                            var mode = (GraphSonMode)Enum.Parse(typeof(GraphSonMode), jp.ReadAsString());
                            // ReSharper disable PossibleMultipleEnumeration
                            graphson = new GraphSonUtility(mode, elementFactory, vertexPropertyKeys,
                                                           edgePropertyKeys);
                            // ReSharper restore PossibleMultipleEnumeration
                        }
                        break;

                        case GraphSonTokens.Vertices:
                            jp.Read();
                            while (jp.Read() && jp.TokenType != JsonToken.EndArray)
                            {
                                var node = (JObject)serializer.Deserialize(jp);
                                graphson.VertexFromJson(node);
                            }
                            break;

                        case GraphSonTokens.Edges:
                            jp.Read();
                            while (jp.Read() && jp.TokenType != JsonToken.EndArray)
                            {
                                var node  = (JObject)serializer.Deserialize(jp);
                                var idIn  = GraphSonUtility.GetTypedValueFromJsonNode(node[GraphSonTokens.InV]);
                                var idOut = GraphSonUtility.GetTypedValueFromJsonNode(node[GraphSonTokens.OutV]);
                                if (idIn == null || idOut == null)
                                {
                                    continue;
                                }
                                var inV  = graph.GetVertex(idIn);
                                var outV = graph.GetVertex(idOut);
                                graphson.EdgeFromJson(node, outV, inV);
                            }
                            break;
                        }
                    }

                    graph.Commit();
                }
            }
            finally
            {
                if (sr != null)
                {
                    sr.Dispose();
                }
            }
        }