private void AddJsonObject() { var key = tokens[currentposition - 1].Value.ToString(); object value; switch (tokens[currentposition + 1].Kind) { case SyntaxKind.OB: currentposition++; value = new Json(GetInBrackets()); break; case SyntaxKind.OBA: currentposition++; value = new JsonObjectArray(GetInBrackets()); break; default: value = tokens[currentposition + 1].Value; break; } Add(new JsonObject(key, value)); currentposition++; }
public static List <object> ConvertArrayFrom(Array array, bool usePrivateFields, string[] exclusion_fields = null) { List <object> list = new List <object>(); foreach (var elem in array) { var type = elem.GetType(); if (IsStructureType(type)) { list.Add(Json.FromStructure(elem, usePrivateFields, exclusion_fields)); } else if (elem is Array) { list.Add(JsonObjectArray.FromArray(elem as Array, usePrivateFields, exclusion_fields)); } else if (elem is IList) { list.Add(JsonObjectArray.FromArray((elem as IList).OfType <object>().ToArray(), usePrivateFields, exclusion_fields)); } else { list.Add(elem); } } return(list); }
public static List <JsonObject> ConvertFrom(object structure, bool usePrivateFields, string[] exclusion_fields = null) { if (structure is Array) { throw new Exception("Use JsonObjectArray.FromArray(Array array)."); } if (!IsStructureType(structure.GetType())) { throw new Exception("Unknown Structure format."); } var structType = structure.GetType(); List <FieldInfo> fields; if (usePrivateFields) { fields = structType.GetRuntimeFields().ToList(); } else { fields = structType.GetFields().ToList(); } return(fields.Select <FieldInfo, JsonObject>(field => { string name = field.Name; if (!(exclusion_fields is null) && exclusion_fields.Contains(name)) { return null; } object value = field.GetValue(structure); if (value is Array) { value = JsonObjectArray.FromArray(value as Array, usePrivateFields, exclusion_fields); } else if (value is IList) { value = JsonObjectArray.FromArray((value as IList).OfType <object>().ToArray(), usePrivateFields, exclusion_fields); } else if (IsStructureType(field.FieldType)) { value = Json.FromStructure(value, usePrivateFields, exclusion_fields); } return new JsonObject(name, value); }).OfType <JsonObject>().ToList()); }
private static object ConvertArrayTo(JsonObjectArray json, Type elemType) { Array list = Array.CreateInstance(elemType, json.Count); for (var i = 0; i < json.Count; i++) { var elem = json[i]; if (elem is Json) { list.SetValue(ConvertTo(elem as Json, elemType), i); } else { list.SetValue(elem, i); } } return(list); }
static void Main2(string[] args) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); var j = Json.FromAnonymous(new { abc = new { hello = "123", h1 = new JsonObjectArray() { false, true, "str", 12785681, Json.FromAnonymous(new { id = 32, name = "egor", arr = new { clas = "base", lol = new[] { new { prikol = "rjaka", ind = 1 }, new { prikol = "rs", ind = 2 } }, hash = "dijnaogsndghin039j\r\nt3y8972gtrb3789r3fb" } }) }, h2 = new[] { "Hi", "Hell" } } }); ((j[0].Value as Json)["h1"].Value as JsonObjectArray).Add(((j[0].Value as Json)["h1"].Value as JsonObjectArray).Clone()); stopWatch.Stop(); Console.WriteLine($"{stopWatch.ElapsedMilliseconds} ms"); Console.WriteLine(j.ToFormatString() + "\r\n\r\n"); var sj = "[{" + " 'guild_experiments': ["+ " [3601750436, null, 1, ["+ " [-1, [{"+ " 's': 1000,"+ " 'e': 10000"+ " }]],"+ " [1, [{"+ " 's': 500,"+ " 'e': 1000"+ " }]]"+ " ],"+ " [],"+ " [{"+ " 'k': ['21683865395396608', '315263844207558671', '392832386079129630', '114560575232671745', '689502507277615175'],"+ " 'b': 1"+ " }]"+ " ],"+ " [3567166443, null, 3, ["+ " [2, [{"+ " 's': 0,"+ " 'e': 10000"+ " }]]"+ " ],"+ " ["+ " [580727696, ["+ " [3399957344, 51326],"+ " [1238858341, null]"+ " ]]"+ " ],"+ " []"+ " ]"+ " ]"+ "}," + "{" + " 'resp': true,"+ " 'resp2': 'true'"+ "}]"; sj = sj.Replace('\'', '"'); stopWatch = new Stopwatch(); stopWatch.Start(); var jj = new JsonObjectArray(sj); stopWatch.Stop(); Console.WriteLine($"{stopWatch.ElapsedMilliseconds} ms"); Console.WriteLine(jj.ToFormatString() + "\r\n\r\n"); Console.ReadLine(); }
public static T[] ConvertArrayTo <T>(JsonObjectArray json) { var test = ConvertArrayTo(json, typeof(T)); return((T[])test); }