<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.IO;
using System.Xml.Schema;
using sones.GraphQL.Result;
using sones.Library.Settings;
using sones.Library.VersionedPluginManager;
using System.Xml;
using System.Reflection;
using Newtonsoft.Json.Linq;
using System.Text;

namespace sones.Plugins.GraphDS.IO.JSON_IO
{
    public sealed class JSON_IO : IOInterface
    {
        #region Data

        private readonly ContentType _contentType;

        #endregion

        #region Constructors

        public JSON_IO()
        {
            _contentType = new ContentType("application/json") { CharSet = "UTF-8" };
        }

        #endregion

        #region IPluginable

        public string PluginName
        {
            get { return "sones.json_io"; }
        }

        public Dictionary<string, Type> SetableParameters
        {
            get { return new Dictionary<string, Type>(); }
        }

        /* ASK: whats this?
        public IPluginable InitializePlugin(Dictionary<string, object> myParameters, GraphApplicationSettings myApplicationSetting)
        {
            return InitializePlugin();
        }
        */
        public IPluginable InitializePlugin(String myUniqueString, Dictionary<string, object> myParameters = null)
        {
            var result = new JSON_IO();

            return (IPluginable)result;
        }

        #endregion

        #region IOInterface

        #region Generate Output from Query Result
        public string GenerateOutputResult(QueryResult myQueryResult)
        {
            // root element...
            var _Query = new JObject();
            // query --------------------------------
            _Query.Add(new JProperty("query", myQueryResult.Query));
            // result -------------------------------
            _Query.Add(new JProperty("result", myQueryResult.TypeOfResult.ToString()));
            // duration -----------------------------
            _Query.Add(new JProperty("duration", new JArray(myQueryResult.Duration, "ms")));
            // warnings -----------------------------
            // currently not in 2.0
            //_Query.Add(new JProperty("warnings", new JArray(
            //    from _Warning in myQueryResult.Warnings
            //    select new JObject(
            //             new JProperty("code", _Warning.GetType().ToString()),
            //             new JProperty("description", _Warning.ToString())
            //           ))));
            // errors -------------------------------
            if (myQueryResult.Error != null)
            { 
                _Query.Add(new JProperty("errors", new JArray(
                             new JObject(
                             new JProperty("code", myQueryResult.Error.GetType().ToString()),
                             new JProperty("description", HandleQueryExceptions(myQueryResult))
                           ))));
            }
            // results ------------------------------
            JArray _resultsArray = new JArray();
            // fill the results array
            if (myQueryResult.Vertices != null)
            {
                foreach (IVertexView _vertex in myQueryResult.Vertices)
                {
                    _resultsArray.Add(GenererateVertexViewJSON(_vertex));
                }
            }
            // add the results to the query....
            _Query.Add(new JProperty("results", _resultsArray));

            return _Query.ToString();
        }

        private String HandleQueryExceptions(QueryResult queryresult)
        {
            StringBuilder SB = new StringBuilder();

            SB.Append(queryresult.Error.ToString());
            if (queryresult.Error.InnerException != null)
                SB.Append(" InnerException: "+queryresult.Error.InnerException.Message);

            return SB.ToString();
        }

        #region private toJSON Extensions
        private JArray GenererateVertexViewJSON(IVertexView aVertex)
        {
            JArray _results = new JArray();

            // take one IVertexView and traverse through it
            #region Vertex Properties
            JObject _properties = new JObject();
            foreach (var _property in aVertex.GetAllProperties())
            {
                if (_property.Item2 == null)
                    _properties.Add(new JProperty(_property.Item1, ""));
                else
                    if (_property.Item2 is Stream)
                        _properties.Add(new JProperty(_property.Item1, "BinaryProperty"));
                    else
                        _properties.Add(new JProperty(_property.Item1, _property.Item2.ToString()));
            }
            // add to the results...
            _results.Add(new JObject(new JProperty("properties", new JObject(_properties))));
            #endregion

            #region Edges
            JArray _edges = new JArray();
            foreach (var _edge in aVertex.GetAllEdges())
            {
                if (_edge.Item2 == null)
                {
                    _edges.Add(new JObject(new JProperty(_edge.Item1, "")));             
                }
                else
                {
                    JArray _newEdge = GenerateEdgeViewJSON(_edge.Item2);
                    _edges.Add(new JObject(new JProperty(_edge.Item1, _newEdge)));
                }
            }
            // add to the results...
            _results.Add(new JObject(new JProperty("edges", _edges)));
            #endregion
            return _results;
        }

        private JArray GenerateEdgeViewJSON(IEdgeView aEdge)
        {
            JArray Output = new JArray();
            #region Edge Properties
            foreach (var _property in aEdge.GetAllProperties())
            {
                JProperty _newEdge = null;
                if (_property.Item2 == null)
                    _newEdge = new JProperty(_property.Item1, "");
                else
                    if (_property.Item2 is Stream)
                    {
                        _newEdge = new JProperty(_property.Item1, "BinaryProperty");
                    }
                    else
                        _newEdge = new JProperty(_property.Item1, _property.Item2.ToString());

                Output.Add(new JObject(_newEdge));
            }
            #endregion

            #region Target Vertices
            JArray _resultsArray = new JArray();
            // fill the results array
            foreach (IVertexView _vertex in aEdge.GetTargetVertices())
            {
                _resultsArray.Add(GenererateVertexViewJSON(_vertex));
            }
            #endregion
            Output.Add(new JObject(new JProperty("targetvertices", _resultsArray)));

            return Output;
        }<commit_msg>changed json output<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.IO;
using System.Xml.Schema;
using sones.GraphQL.Result;
using sones.Library.Settings;
using sones.Library.VersionedPluginManager;
using System.Xml;
using System.Reflection;
using Newtonsoft.Json.Linq;
using System.Text;

namespace sones.Plugins.GraphDS.IO.JSON_IO
{
    public sealed class JSON_IO : IOInterface
    {
        #region Data

        private readonly ContentType _contentType;

        #endregion

        #region Constructors

        public JSON_IO()
        {
            _contentType = new ContentType("application/json") { CharSet = "UTF-8" };
        }

        #endregion

        #region IPluginable

        public string PluginName
        {
            get { return "sones.json_io"; }
        }

        public Dictionary<string, Type> SetableParameters
        {
            get { return new Dictionary<string, Type>(); }
        }

        /* ASK: whats this?
        public IPluginable InitializePlugin(Dictionary<string, object> myParameters, GraphApplicationSettings myApplicationSetting)
        {
            return InitializePlugin();
        }
        */
        public IPluginable InitializePlugin(String myUniqueString, Dictionary<string, object> myParameters = null)
        {
            var result = new JSON_IO();

            return (IPluginable)result;
        }

        #endregion

        #region IOInterface

        #region Generate Output from Query Result
        public string GenerateOutputResult(QueryResult myQueryResult)
        {
            // root element...
            var _Query = new JObject();
            // query --------------------------------
            _Query.Add(new JProperty("Query", myQueryResult.Query));
            // result -------------------------------
            _Query.Add(new JProperty("Result", myQueryResult.TypeOfResult.ToString()));
            // duration -----------------------------
            _Query.Add(new JProperty("Duration", new JArray(myQueryResult.Duration, "ms")));
            // warnings -----------------------------
            // currently not in 2.0
            //_Query.Add(new JProperty("warnings", new JArray(
            //    from _Warning in myQueryResult.Warnings
            //    select new JObject(
            //             new JProperty("code", _Warning.GetType().ToString()),
            //             new JProperty("description", _Warning.ToString())
            //           ))));
            // errors -------------------------------
            if (myQueryResult.Error != null)
            { 
                _Query.Add(new JProperty("Errors", new JArray(
                             new JObject(
                             new JProperty("Code", myQueryResult.Error.GetType().ToString()),
                             new JProperty("Description", HandleQueryExceptions(myQueryResult))
                           ))));
            }
            // results ------------------------------
            JArray _resultsArray = new JArray();
            // fill the results array
            if (myQueryResult.Vertices != null)
            {
                foreach (IVertexView _vertex in myQueryResult.Vertices)
                {
                    _resultsArray.Add(GenerateVertexViewJSON(_vertex));
                }
            }
            // add the results to the query....
            _Query.Add(new JProperty("Results", _resultsArray));

            return _Query.ToString();
        }

        private String HandleQueryExceptions(QueryResult queryresult)
        {
            StringBuilder SB = new StringBuilder();

            SB.Append(queryresult.Error.ToString());
            if (queryresult.Error.InnerException != null)
                SB.Append(" InnerException: "+queryresult.Error.InnerException.Message);

            return SB.ToString();
        }

        #region private toJSON Extensions
        private JArray GenerateVertexViewJSON(IVertexView aVertex)
        {
            JArray _results = new JArray();

            // take one IVertexView and traverse through it
            #region Vertex Properties
            JObject _properties = new JObject();
            foreach (var _property in aVertex.GetAllProperties())
            {
                if (_property.Item2 == null)
                    _properties.Add(new JProperty(_property.Item1, ""));
                else
                    if (_property.Item2 is Stream)
                        _properties.Add(new JProperty(_property.Item1, "BinaryProperty"));
                    else
                        _properties.Add(new JProperty(_property.Item1, _property.Item2.ToString()));
            }
            // add to the results...
            _results.Add(new JObject(new JProperty("Properties", new JObject(_properties))));
            #endregion

            #region Edges
            JArray _edges = new JArray();
            foreach (var _edge in aVertex.GetAllEdges())
            {
                if (_edge.Item2 == null)
                {
                    _edges.Add(new JObject(new JProperty(_edge.Item1, "")));             
                }
                else
                {
                    JArray _newEdge = GenerateEdgeViewJSON(_edge.Item2);
                    _edges.Add(new JObject(new JProperty(_edge.Item1, _newEdge)));
                }
            }
            // add to the results...
            _results.Add(new JObject(new JProperty("Edges", _edges)));
            #endregion
            return _results;
        }

        private JArray GenerateEdgeViewJSON(IEdgeView aEdge)
        {
            JArray Output = new JArray();
            #region Edge Properties
            foreach (var _property in aEdge.GetAllProperties())
            {
                JProperty _newEdge = null;
                if (_property.Item2 == null)
                    _newEdge = new JProperty(_property.Item1, "");
                else
                    if (_property.Item2 is Stream)
                    {
                        _newEdge = new JProperty(_property.Item1, "BinaryProperty");
                    }
                    else
                        _newEdge = new JProperty(_property.Item1, _property.Item2.ToString());

                Output.Add(new JObject(new JProperty("EdgeProperties", new JObject(_newEdge))));
            }
            #endregion

            if (aEdge is IHyperEdgeView)
            {                
                var edgeProperties = new JArray();                  

                foreach (var singleEdge in ((IHyperEdgeView)aEdge).GetAllEdges())
                {
                    foreach (var singleEdgeProp in singleEdge.GetAllProperties())
                    {
                        edgeProperties.Add(new JObject(new JProperty(singleEdgeProp.Item1, singleEdgeProp.Item2.ToString())));
                    }

                    if (edgeProperties.Count == 0)
                    {
                        Output.Add(new JObject(new JProperty("SingleEdge", new JObject(new JProperty("Targetvertex", GenerateVertexViewJSON(singleEdge.GetTargetVertex()))))));
                    }
                    else
                    {
                        Output.Add(new JObject(new JProperty("SingleEdge", new JObject(new JProperty("EdgeProperties", new JArray(edgeProperties))), new JObject(new JProperty("Targetvertex", GenerateVertexViewJSON(singleEdge.GetTargetVertex()))))));
                    }
                    
                    edgeProperties.Clear();                    
                }                
                
            }
            else
            {                
                var edgeProperties = new JArray();                

                foreach (var property in aEdge.GetAllProperties())
                {
                    edgeProperties.Add(new JObject(new JProperty(property.Item1, property.Item2)));
                }

                if (edgeProperties.Count == 0)
                {
                    Output.Add(new JObject(new JProperty("SingleEdge", new JObject(new JProperty("TargetVertex", GenerateVertexViewJSON(((ISingleEdgeView)aEdge).GetTargetVertex()))))));
                }
                else
                {
                    Output.Add(new JObject(new JProperty("SingleEdge", new JObject(new JProperty("EdgeProperties", edgeProperties)), new JObject(new JProperty("TargetVertex", GenerateVertexViewJSON(((ISingleEdgeView)aEdge).GetTargetVertex()))))));
                }
            }

            return Output;
        }
{'fraction_non_alphanumeric': 0.11231087546877021, 'fraction_numerical': 0.002844950213371266, 'mean_word_length': 1.4227756892230576, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2554498', 'n_tokens_mistral': 3655, 'n_tokens_neox': 3519, 'n_words': 805}