public List <JsonEvent> ProcJson(string InputObject, string ParentObjectKey, bool DoInputString) { Stopwatch sw = new Stopwatch(); //Stopwatch sw2 = new Stopwatch(); sw.Start(); List <JsonEvent> jsonEvents = new List <JsonEvent>(); //JsonEvent je = new JsonEvent(); //SortedList<int, string> tmpFieldOrder = new SortedList<int, string>(); //SortedList<int, JsonField> tmpFields = new SortedList<int, JsonField>(); string jsonStr = ""; if (DoInputString) { jsonStr = InputObject; } else { using (FileStream fs = new FileStream(InputObject, FileMode.Open, FileAccess.ReadWrite)) { StreamReader sr = new StreamReader(fs); jsonStr = sr.ReadToEnd(); sr.Close(); fs.Close(); } } JObject jpobj = (JObject)JsonConvert.DeserializeObject(jsonStr); JToken jptk = jpobj[ParentObjectKey]; if (jptk.Type.ToString() != "Array") { Util.DateLog(@"ERROR: Parent object is not an array!"); throw new Exception(@"Parent object is not an array!"); } int totChilds = jptk.Count(); Util.DateLog(String.Format(@"Utilizing parent object: {0}", ParentObjectKey)); Util.DateLog(String.Format(@"Found {0} child objects to process", totChilds)); //foreach (JToken jtk in jptk.Values()) //foreach (JToken jtk in jptk) // Good foreach (JObject jobj in jptk) { //sw2.Start(); JsonEvent je = new JsonEvent(); //SortedList<int, string> tmpFieldOrder = new SortedList<int, string>(); SortedList <int, JsonField> tmpFields = new SortedList <int, JsonField>(); int tmpFieldCounter = 1; foreach (JProperty jp in jobj.Properties()) { JsonField jf = new JsonField(tmpFieldCounter, jp.Name, jp.Value.Type.ToString(), null); switch (jp.Value.Type.ToString()) { case "String": jf.Value = (string)((JValue)jp.Value.ToString()); break; case "Null": jf.Value = "null"; break; case "Object": // Method 1 - leave as string //jf.Value = new object(); //jf.Value = (object)jp.Value; // Method 2 - Second order conversion jf.Value = (string)@"object"; JObject tmpChildJson = (JObject)JsonConvert.DeserializeObject(jp.Value.ToString()); this.ProcJsonChild2(tmpChildJson, jp.Name, ref tmpFieldCounter, ref tmpFields); break; case "Int": jf.Value = new int(); jf.Value = (int)jp.Value; break; default: jf.Value = new object(); jf.Value = (object)jp.Value; break; } tmpFields.Add(tmpFieldCounter, jf); //tmpFieldOrder.Add(tmpFieldCounter, jp.Name); tmpFieldCounter++; } je.ImportFields(tmpFields); je.GetLogOutput2(false); jsonEvents.Add(je); //sw2.Stop(); //Util.DateLog(String.Format(@"Conversion time: {0}", sw2.Elapsed)); //sw2.Reset(); } sw.Stop(); Util.DateLog(String.Format(@"Total conversion time: {0}", sw.Elapsed)); double dEventRate = (double)(totChilds / sw.Elapsed.TotalSeconds); //Util.DateLog(String.Format(@"Conversion rate: {1}{0:F2} logs/sec{2}", dEventRate, "\x1B[92m", "\x1B[0m")); Util.DateLog(String.Format(@"Conversion rate: {0:F2} logs/sec", dEventRate)); return(jsonEvents); }
public JsonEvent ProcJson(string InputFile) { Stopwatch sw = new Stopwatch(); sw.Start(); JsonEvent je = new JsonEvent(); SortedList <int, string> tmpFieldOrder = new SortedList <int, string>(); SortedList <int, JsonField> tmpFields = new SortedList <int, JsonField>(); string jsonStr = ""; using (FileStream fs = new FileStream(InputFile, FileMode.Open, FileAccess.ReadWrite)) { StreamReader sr = new StreamReader(fs); jsonStr = sr.ReadToEnd(); sr.Close(); fs.Close(); } JObject jobj = (JObject)JsonConvert.DeserializeObject(jsonStr); int tmpFieldCounter = 1; // ========== Property Enumeration/Processing, rev 1: DEPRECATED, using rev 2 below /*foreach (JProperty jp in jobj.Properties()) * { * //string tmpPropOut = @""; * string tmpPropName = jp.Name; * string tmpPropType = jp.Value.Type.ToString(); * //string tmpPropVal = @""; * object tmpPropVal = null; * Type tmpTypeType = Type.GetType(tmpPropType.ToLower()); * * if (tmpPropType == "Object" || tmpPropType == "Array") * { * tmpPropVal = new object(); * tmpPropVal = jp.Value; * * if (jp.Value != null) * { * string propPrefix = tmpPropName; * JObject jo = (JObject)tmpPropVal; * foreach (JProperty jpp in jo.Properties()) * { * * } * } * } * else if (tmpPropType == "Null") * { * tmpPropVal = @"none"; * } * else * { * tmpPropVal = jp.Value.ToString(); * } * * //tmpPropVal = jp.Value; * * JsonField jf = new JsonField(tmpFieldCounter, tmpPropName, tmpPropType, null); * switch (tmpPropType) * { * case "String": * //jf.Value = (string)tmpPropVal; * jf.Value = (string)((JValue)tmpPropVal.ToString()); * break; * case "Null": * //jf.Value = (string)((JValue)tmpPropVal.ToString()); * jf.Value = "none"; * break; * case "Object": * jf.Value = new object(); * jf.Value = (object)tmpPropVal; * break; * case "Int": * jf.Value = new int(); * jf.Value = (int)tmpPropVal; * break; * default: * jf.Value = new object(); * jf.Value = (object)tmpPropVal; * break; * } * tmpFields.Add(tmpFieldCounter, jf); * * tmpFieldOrder.Add(tmpFieldCounter, jp.Name); * tmpFieldCounter++; * }*/ // ========== Property Enumeration/Processing, rev 2: Current 8-31-20 foreach (JProperty jp in jobj.Properties()) { JsonField jf = new JsonField(tmpFieldCounter, jp.Name, jp.Value.Type.ToString(), null); switch (jp.Value.Type.ToString()) { case "String": jf.Value = (string)((JValue)jp.Value.ToString()); break; case "Null": jf.Value = "null"; break; case "Object": jf.Value = new object(); jf.Value = (object)jp.Value; break; case "Int": jf.Value = new int(); jf.Value = (int)jp.Value; break; default: jf.Value = new object(); jf.Value = (object)jp.Value; break; } tmpFields.Add(tmpFieldCounter, jf); tmpFieldOrder.Add(tmpFieldCounter, jp.Name); tmpFieldCounter++; } je.ImportFields(tmpFields); sw.Stop(); Util.DateLog(String.Format(@"Conversion time: {0}", sw.Elapsed)); return(je); }