예제 #1
0
        //get rect xy
        private static string GetRectXY(JObject json, JsonPathContext context, int i, string node)
        {
            string x = "";

            try
            {
                x = context.SelectNodes(json, "$['faces'][" + i + "]['name_coordinates'][" + node + "]['x']").Single().Value.ToString();
            }
            catch (Exception)
            {
            }
            string y = "";

            try
            {
                y = context.SelectNodes(json, "$['faces'][" + i + "]['name_coordinates'][" + node + "]['y']").Single().Value.ToString();
            }
            catch (Exception)
            {
            }
            string width = "";

            try
            {
                width = context.SelectNodes(json, "$['faces'][" + i + "]['name_coordinates'][" + node + "]['width']").Single().Value.ToString();
            }
            catch (Exception)
            {
            }
            string height = "";

            try
            {
                height = context.SelectNodes(json, "$['faces'][" + i + "]['name_coordinates'][" + node + "]['height']").Single().Value.ToString();
            }
            catch (Exception)
            {
            }
            string coordinate = "";

            try
            {
                if (!"".Equals(x) && !"".Equals(y) && !"".Equals(width) && !"".Equals(height))
                {
                    coordinate = String.Format("{0},{1},{2},{3},0,2277,3201", x, y, double.Parse(width) + double.Parse(x), double.Parse(height) + double.Parse(y));
                }
            }
            catch (Exception)
            {
            }
            return(coordinate);
        }
예제 #2
0
        public void ApplyPath()
        {
            if (String.IsNullOrEmpty(RequestResponse) || String.IsNullOrEmpty(JsonPath))
            {
                return;
            }

            JsonPath = JsonPath.Trim();

            try
            {
                var json    = JObject.Parse(RequestResponse);
                var context = new JsonPathContext {
                    ValueSystem = new JsonNetValueSystem()
                };
                var values           = context.SelectNodes(json, JsonPath).Select(node => node.Value);
                var newResponseValue = JsonConvert.SerializeObject(values);

                JsonPathResult = newResponseValue;
            }
            catch (JsonException je)
            {
                Dev2Logger.Error(je, GlobalConstants.WarewolfError);
            }
        }
예제 #3
0
        public void Evaluate(int spreadMax)
        {
            if (FJObjectIn[0] == null)
            {
                FDataOut.SliceCount = 0;
                return;
            }

            FDataOut.SliceCount = spreadMax;

            for (int i = 0; i < spreadMax; i++)
            {
                try
                {
                    var             values = FParser.SelectNodes(FJObjectIn[i], FQueryIn[i]).Select(node => node.Value);
                    Spread <Object> list   = values.ToSpread();

                    int count = values.Count();
                    FDataOut[i].SliceCount = count;

                    for (int j = 0; j < count; j++)
                    {
                        FDataOut[i][j] = list[j].ToString();
                    }
                }
                catch (Exception ex)
                {
                    FLogger.Log(LogType.Error, ex.ToString());
                }
            }
        }
예제 #4
0
        public static object Select(this JObject JObject, string selector)
        {
            JsonPathContext context = new JsonPathContext { ValueSystem = new JsonNetValueSystem() };
            var selected = context.SelectNodes(JObject, selector).Select(node => node.Value.ToString());

            if (selected.Any())
                return selected.Count() > 1 ? (object)selected.ToArray() : (object)selected.First();
            return null;
        }
예제 #5
0
        /// <seealso cref="https://github.com/json-path/JsonPath"/>
        public string[] GetMultipleNodes(string jsonString, string expression)
        {
            jsonString.ThrowIfNullOrEmpty(nameof(jsonString));
            expression.ThrowIfNullOrEmpty(nameof(expression));
            var json  = JsonConvert.DeserializeObject(jsonString);
            var nodes = context.SelectNodes(json, expression);

            return(Array.ConvertAll(JsonPathNode.ValuesFrom(nodes), (p) => p.ToString()));
        }
예제 #6
0
        public void JsonPathContext_SelectNodes_Obj_IsNull_AreEqual_ExpectArgumentNullException()
        {
            //--------------------------Arrange---------------------------
            var jsonPathContext = new JsonPathContext();

            //--------------------------Act-------------------------------
            //--------------------------Assert----------------------------
            Assert.ThrowsException <ArgumentNullException>(() => jsonPathContext.SelectNodes(null, "$;"));
        }
        private string[] JsonPathParser(string jsonInput, string ExprJson)
        {
            var json    = JObject.Parse(jsonInput);
            var context = new JsonPathContext {
                ValueSystem = new JsonNetValueSystem()
            };
            var values  = context.SelectNodes(json, ExprJson).Select(node => node.Value);
            var JsonOut = JsonConvert.SerializeObject(values);
            var Arr     = JArray.Parse(JsonOut);
            var items   = Arr.Select(jv => (string)jv).ToArray();

            return(items);
        }
예제 #8
0
        public void JsonPathContext_SelectNodes_Expr_Index1_NotSemiColon_AreEqual_ExpectTrue()
        {
            //--------------------------Arrange---------------------------
            var obj = new object();

            obj = "testObject";

            var jsonPathContext = new JsonPathContext();

            //--------------------------Act-------------------------------
            //--------------------------Assert----------------------------
            Assert.ThrowsException <NullReferenceException>(() => jsonPathContext.SelectNodes(obj, "$test"));
        }
예제 #9
0
        public void JsonPathContext_SelectNodes_Obj_NotNull_AreEqual_ExpectTrue()
        {
            //--------------------------Arrange---------------------------
            var obj = new object();

            var jsonPathContext = new JsonPathContext();
            //--------------------------Act-------------------------------
            var testList = jsonPathContext.SelectNodes(obj, "$;");

            //--------------------------Assert----------------------------
            Assert.AreEqual(1, testList.Length);
            Assert.AreEqual("$", testList[0].Path);
            Assert.AreEqual(obj, testList[0].Value);
        }
예제 #10
0
        public void JsonPathContext_SelectNodes_JsonPathNodeArray_IsNull_AreEqual_ExpectTrue()
        {
            //--------------------------Arrange---------------------------
            var mockJsonPathValueSystem = new Mock <IJsonPathValueSystem>();

            var obj = new object();

            var jsonPathContext = new JsonPathContext()
            {
                ValueSystem = mockJsonPathValueSystem.Object,
            };
            //--------------------------Act-------------------------------
            var testList = jsonPathContext.SelectNodes(obj, "$T;");

            //--------------------------Assert----------------------------
            mockJsonPathValueSystem.VerifyAll();
            Assert.AreEqual(0, testList.Length);
        }
예제 #11
0
        protected override void SolveInstance(IGH_DataAccess da)
        {
            JObject jsonObject = null;
            string  jpathQuery = null;

            if (!da.GetData(0, ref jsonObject) || !da.GetData(1, ref jpathQuery))
            {
                return;
            }

            if (jsonObject == null || String.IsNullOrEmpty(jpathQuery))
            {
                return;
            }

            var values = FParser.SelectNodes(jsonObject, jpathQuery).Select(node => node.Value.ToString()).ToList();

            da.SetDataList(0, values);
        }
예제 #12
0
        public IEnumerable <JsonPathExpression> ParseExpression(JsonPathModel model, ScriptEvaluatorFactory.ScriptEvaluatorTypes evaluatorType = ScriptEvaluatorFactory.ScriptEvaluatorTypes.Basic, IJsonPathValueSystem valueSystem = null)
        {
            IList <JsonPathExpression> result = new List <JsonPathExpression>(model.Expressions.Count());
            var json = JsonConvert.DeserializeObject(model.SourceJson);

            var context = new JsonPathContext
            {
                ScriptEvaluator = new JsonPathScriptEvaluator(ScriptEvaluatorFactory.Create(evaluatorType).EvaluateScript),
                ValueSystem     = valueSystem
            };

            foreach (JsonPathExpression expression in model.Expressions)
            {
                var nodes = context.SelectNodes(json, expression.Expression);
                Array.Copy(nodes, expression.Nodes.ToArray(), nodes.Length);
                result.Add(expression);
            }

            return(result.AsEnumerable());
        }
예제 #13
0
        public void JsonPathContext_SelectNodes_ValueSystem_NotNull_AreEqual_ExpectTrue()
        {
            //--------------------------Arrange---------------------------
            var mockJsonPathValueSystem = new Mock <IJsonPathValueSystem>();

            var obj = new object();

            var jsonPathContext = new JsonPathContext()
            {
                ValueSystem = mockJsonPathValueSystem.Object
            };
            //--------------------------Act-------------------------------
            var testList = jsonPathContext.SelectNodes(obj, "$;");

            //--------------------------Assert----------------------------
            Assert.AreEqual(1, testList.Length);
            Assert.AreEqual("$", testList[0].Path);
            Assert.AreEqual(obj, testList[0].Value);
            Assert.AreEqual(mockJsonPathValueSystem.Object, jsonPathContext.ValueSystem);
        }
예제 #14
0
        //handle json file
        private static void handleFile(FileInfo file)
        {
            if (!file.Name.Contains("40146_b079845-00039.jpg.Completed"))
            {
                return;
            }
            string fileName = file.Name.Substring(0, file.Name.IndexOf("."));

            ////如果文件夹中已经生成了xml,则需要跳过
            //string xmlFileName = fileName + ".xml";
            //FileInfo xmlFile = new FileInfo(file.Directory + "\\" + xmlFileName);
            //if (xmlFile.Exists)
            //{
            //    Dao.DaoUtil.writeLog(xmlFileName + "已经生成过,故跳过生成", 0);
            //    return;
            //}
            Dao.DaoUtil.writeLog("开始读取:" + file.Name, 0);
            StreamReader  sr = new StreamReader(file.FullName, Encoding.UTF8);
            String        line;
            StringBuilder sBuilder = new StringBuilder();

            while ((line = sr.ReadLine()) != null)
            {
                sBuilder.Append(line);
            }


            //获取根节点对象
            XDocument document = new XDocument();
            XElement  root     = new XElement("StandardJob");

            root.SetAttributeValue("JobName", "48381");
            root.SetAttributeValue("BatchName", file.Directory.Name);
            root.SetAttributeValue("FileName", fileName);
            XElement stats = new XElement("Stats");
            XElement stat  = new XElement("Stat");

            stat.SetAttributeValue("id", "1");
            stat.SetAttributeValue("UpdateTime", "2017-04-17 15:37:34");

            XElement Status = new XElement("Status");

            Status.SetValue("Key");
            stat.Add(Status);

            XElement Operator = new XElement("Operator");

            Operator.SetValue("B01953");
            stat.Add(Operator);

            XElement TemplateID = new XElement("TemplateID");

            TemplateID.SetValue("0");
            stat.Add(TemplateID);

            XElement ClientVersion = new XElement("ClientVersion");

            ClientVersion.SetValue("LiFT v6.0.17.0413A");
            stat.Add(ClientVersion);

            XElement Records = new XElement("Records");

            for (int i = 0; i < 2; i++)
            {
                XElement Element = new XElement("Element");
                Element.SetAttributeValue("id", i);
                Element.SetAttributeValue("type", "Table");
                Element.SetAttributeValue("rows", "1");
                Records.Add(Element);
            }

            stat.Add(Records);

            XElement KeyStrokes = new XElement("KeyStrokes");

            KeyStrokes.SetValue("59");
            stat.Add(KeyStrokes);

            stats.Add(stat);
            root.Add(stats);


            ////////////////////关键代码//////////////////////////////////
            string  doctype = "";
            JObject json    = JObject.Parse(sBuilder.ToString());
            var     context = new JsonPathContext {
                ValueSystem = new JsonNetValueSystem()
            };

            try
            {
                doctype = context.SelectNodes(json, "$..doctype").Single().Value.ToString();
            }
            catch (Exception) { }

            XElement Groups   = new XElement("Groups");
            XElement Group    = new XElement("Group");
            XElement Elements = new XElement("Elements");

            Group.SetAttributeValue("id", "0");


            var values1 = context.SelectNodes(json, "$..faces");

            Dictionary <String, XElement> dic = new Dictionary <string, XElement>();
            var      values    = context.SelectNodes(json, "$..faces.*").Select(node => node.Value);
            XElement rows_body = null;
            XElement ele_body  = null;
            int      count     = 0;

            if (values.Count() == 0)
            {
                //title
                XElement Rows_f = new XElement("Rows");
                XElement Row_f  = new XElement("Row");
                Row_f.SetAttributeValue("id", "1");
                Row_f.Add(NewColumn("Comments", doctype, null));


                //Grade对应grade(如果grade为null,该xml栏位作空白处理)
                string grade    = "";
                string xy_grade = "";

                if (!dic.Keys.Contains(grade))
                {
                    XElement Element_f = new XElement("Element");
                    Element_f.SetAttributeValue("id", ++count);
                    if (rows_body != null)
                    {
                        ele_body.Add(rows_body);
                        Elements.Add(ele_body);
                        rows_body = new XElement("Rows");
                    }
                    Row_f.Add(NewColumn("Grade", "null".Equals(grade) ? "" : grade, xy_grade));
                    Rows_f.Add(Row_f);
                    Element_f.Add(Rows_f);
                    Elements.Add(Element_f);

                    //body
                    ele_body = new XElement("Element");
                    ele_body.SetAttributeValue("id", ++count);
                    rows_body = new XElement("Rows");

                    dic[grade] = rows_body;
                }


                #region

                XElement Row = new XElement("Row");
                Row.SetAttributeValue("id", "1");

                Row.Add(NewColumn("Prefix", "", ""));

                Row.Add(NewColumn("Given", "", ""));

                Row.Add(NewColumn("Surname", "", ""));

                Row.Add(NewColumn("Suffix", "", ""));

                Row.Add(NewColumn("Facial_Coordinates", "", null));

                Row.Add(NewColumn("Box_Coordinates", "", null));


                Row.Add(NewColumn("Picture_Coordinates", "", null));
                #endregion
                rows_body.Add(Row);
            }
            else
            {
                //所有的faces values
                for (int i = 0; i < values.Count(); i++)
                {
                    //title
                    XElement Rows_f = new XElement("Rows");
                    XElement Row_f  = new XElement("Row");
                    Row_f.SetAttributeValue("id", "1");
                    if (i == 0)
                    {
                        Row_f.Add(NewColumn("Comments", doctype, null));
                    }

                    //Grade对应grade(如果grade为null,该xml栏位作空白处理)
                    string grade = "";
                    try
                    {
                        grade = context.SelectNodes(json, "$['faces'][" + i + "]['name_coordinates']['grade']['text']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    string xy_grade = GetRectXY(json, context, i, "grade");

                    if (!dic.Keys.Contains(grade))
                    {
                        XElement Element_f = new XElement("Element");
                        Element_f.SetAttributeValue("id", ++count);
                        if (rows_body != null)
                        {
                            ele_body.Add(rows_body);
                            Elements.Add(ele_body);
                            rows_body = new XElement("Rows");
                        }
                        Row_f.Add(NewColumn("Grade", "null".Equals(grade) ? "" : grade, xy_grade));
                        Rows_f.Add(Row_f);
                        Element_f.Add(Rows_f);
                        Elements.Add(Element_f);

                        //body
                        ele_body = new XElement("Element");
                        ele_body.SetAttributeValue("id", ++count);
                        rows_body = new XElement("Rows");

                        dic[grade] = rows_body;
                    }


                    #region

                    XElement Row = new XElement("Row");
                    Row.SetAttributeValue("id", "1");
                    string prefix = "";
                    try
                    {
                        prefix = context.SelectNodes(json, "$['faces'][" + i + "]['name_coordinates']['prefix']['text']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    string xy_prefix = GetRectXY(json, context, i, "prefix");
                    Row.Add(NewColumn("Prefix", prefix, xy_prefix));

                    string given = "";
                    try
                    {
                        given = context.SelectNodes(json, "$['faces'][" + i + "]['name_coordinates']['given']['text']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    string xy_given = GetRectXY(json, context, i, "given");
                    Row.Add(NewColumn("Given", given, xy_given));

                    string surname = "";
                    try
                    {
                        surname = context.SelectNodes(json, "$['faces'][" + i + "]['name_coordinates']['surname']['text']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    string xy_surname = GetRectXY(json, context, i, "surname");
                    Row.Add(NewColumn("Surname", surname, xy_surname));

                    string suffix = "";
                    try
                    {
                        suffix = context.SelectNodes(json, "$['faces'][" + i + "]['name_coordinates']['suffix']['text']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    string xy_suffix = GetRectXY(json, context, i, "suffix");
                    Row.Add(NewColumn("Suffix", suffix, xy_suffix));


                    string x1, x2, x3, y1, y2, y3;
                    try
                    {
                        x1 = context.SelectNodes(json, "$['faces'][" + i + "]['metadata']['leftEyeCenterX']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                        x1 = "";
                    }
                    try
                    {
                        y1 = context.SelectNodes(json, "$['faces'][" + i + "]['metadata']['leftEyeCenterY']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                        y1 = "";
                    }
                    try
                    {
                        x2 = context.SelectNodes(json, "$['faces'][" + i + "]['metadata']['rightEyeCenterX']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                        x2 = "";
                    }

                    try
                    {
                        y2 = context.SelectNodes(json, "$['faces'][" + i + "]['metadata']['rightEyeCenterY']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                        y2 = "";
                    }
                    try
                    {
                        x3 = context.SelectNodes(json, "$['faces'][" + i + "]['metadata']['chinTipX']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                        x3 = "";
                    }
                    try
                    {
                        y3 = context.SelectNodes(json, "$['faces'][" + i + "]['metadata']['chinTipY']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                        y3 = "";
                    }
                    string facial = "";
                    if (!"".Equals(x1) && !"".Equals(x2) && !"".Equals(x3) && !"".Equals(y1) && !"".Equals(y2) && !"".Equals(y3))
                    {
                        facial = String.Format("{0},{1},{2},{3},{4},{5}", x1, y1, x2, y2, x3, y3);
                    }
                    Row.Add(NewColumn("Facial_Coordinates", facial, null));

                    try
                    {
                        x1 = context.SelectNodes(json, "$['faces'][" + i + "]['face_boundingBox']['left']").Single().Value.ToString();
                    }
                    catch (Exception) { }
                    try
                    {
                        y1 = context.SelectNodes(json, "$['faces'][" + i + "]['face_boundingBox']['top']").Single().Value.ToString();
                    }
                    catch (Exception) { }

                    string faceWidth = "";
                    try
                    {
                        faceWidth = context.SelectNodes(json, "$['faces'][" + i + "]['face_boundingBox']['width']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    string faceLeft = "";
                    try
                    {
                        faceLeft = context.SelectNodes(json, "$['faces'][" + i + "]['face_boundingBox']['left']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    string faceHeight = "";
                    try
                    {
                        faceHeight = context.SelectNodes(json, "$['faces'][" + i + "]['face_boundingBox']['height']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    string faceTop = "";
                    try
                    {
                        faceTop = context.SelectNodes(json, "$['faces'][" + i + "]['face_boundingBox']['top']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    faceWidth  = faceWidth == null || "".Equals(faceWidth.Trim()) ? "0" : faceWidth;
                    faceLeft   = faceLeft == null || "".Equals(faceLeft.Trim()) ? "0" : faceLeft;
                    faceHeight = faceHeight == null || "".Equals(faceHeight.Trim()) ? "0" : faceHeight;
                    faceTop    = faceTop == null || "".Equals(faceTop.Trim()) ? "0" : faceTop;

                    x2 = (double.Parse(faceWidth)
                          + double.Parse(faceLeft)) + "";
                    y2 = (double.Parse(faceHeight)
                          + double.Parse(faceTop)) + "";

                    string box = "";
                    if (!"".Equals(x1) && !"".Equals(x2) && !"".Equals(y1) && !"".Equals(y2))
                    {
                        box = String.Format("{0},{1},{2},{3}", x1, y1, x2, y2);
                    }
                    Row.Add(NewColumn("Box_Coordinates", box, null));

                    x1 = context.SelectNodes(json, "$['faces'][" + i + "]['extended_coordinate']['left']").Single().Value.ToString();
                    y1 = context.SelectNodes(json, "$['faces'][" + i + "]['extended_coordinate']['top']").Single().Value.ToString();

                    string extWidth = "";
                    try
                    {
                        extWidth = context.SelectNodes(json, "$['faces'][" + i + "]['extended_coordinate']['width']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }

                    string extLeft = "";
                    try
                    {
                        extLeft = context.SelectNodes(json, "$['faces'][" + i + "]['extended_coordinate']['left']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    string extHeight = "";
                    try
                    {
                        extHeight = context.SelectNodes(json, "$['faces'][" + i + "]['extended_coordinate']['height']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    string extTop = "";
                    try
                    {
                        extTop = context.SelectNodes(json, "$['faces'][" + i + "]['extended_coordinate']['top']").Single().Value.ToString();
                    }
                    catch (Exception)
                    {
                    }
                    extWidth  = extWidth == null || "".Equals(extWidth.Trim()) ? "0" : extWidth;
                    extLeft   = extLeft == null || "".Equals(extLeft.Trim()) ? "0" : extLeft;
                    extHeight = extHeight == null || "".Equals(extHeight.Trim()) ? "0" : extHeight;
                    extTop    = extTop == null || "".Equals(extTop.Trim()) ? "0" : extTop;

                    x2 = (double.Parse(extWidth)
                          + double.Parse(extLeft)) + "";
                    y2 = (double.Parse(extHeight)
                          + double.Parse(extTop)) + "";

                    string pic = "";
                    if (!"".Equals(x1) && !"".Equals(x2) && !"".Equals(y1) && !"".Equals(y2))
                    {
                        pic = String.Format("{0},{1},{2},{3}", x1, y1, x2, y2);
                    }
                    Row.Add(NewColumn("Picture_Coordinates", pic, null));
                    #endregion
                    rows_body.Add(Row);
                }
            }
            if (rows_body != null)
            {
                ele_body.Add(rows_body);
                Elements.Add(ele_body);
            }


            Group.Add(Elements);
            Groups.Add(Group);
            root.Add(Groups);
            ////////////////////关键代码//////////////////////////////////
            string   newPath     = file.Directory.FullName + "\\" + fileName + ".xml";
            FileInfo newFileInfo = new FileInfo(newPath);
            if (newFileInfo.Exists)
            {
                newFileInfo.Delete();
            }
            else
            {
                //if (timer2.Enabled)
                //{
                //写入已经生成xml的json中
                Dao.DaoUtil.writeLog(file.FullName.Replace(rootPath, "") + "|" + file.CreationTime.Ticks, 3);
                //}
                root.Save(newPath);
            }
            Dao.DaoUtil.writeLog("读取完毕...", 0);
        }