private object ReadObject(StringBuilder text) { int openIndex = 0; int closeIndex = 0; GetBracketIndexes(text, '{', '}', ref openIndex, ref closeIndex); text = CutText(text, openIndex, closeIndex); MaximFormatterObject mfObj = new MaximFormatterObject(); mfObj.Obj = null; int id; Type type; for (int i = 0; i < text.Length; i++) { // "$ if ((text[i] == '"') && (text[i + 1] == '$')) { i += 2; // "$i if (text[i] == 'i') { i += 6; string stringId = ""; while (text[i] != '"') { stringId += text[i]; i++; } id = int.Parse(stringId); mfObj.Id = id; } // "$t else if (text[i] == 't') { i += 8; string typeName = ""; while (text[i] != '"') { typeName += text[i]; i++; } type = Type.GetType(typeName); mfObj.Obj = CreateObject(type); mainObjectList.Add(mfObj); } // "$v else if (text[i] == 'v') { IList list = (IList)mfObj.Obj; GetBracketIndexes(text, '{', '}', ref openIndex, ref closeIndex); StringBuilder elements = CutText(text, openIndex, closeIndex); while (!IsEmpty(elements)) { GetBracketIndexes(elements, '{', '}', ref openIndex, ref closeIndex); StringBuilder element = CutText(elements, openIndex - 1, closeIndex + 1); var listElement = ReadObject(element); list.Add(listElement); elements.Remove(0, closeIndex + 1); } mfObj.Obj = list; break; } // "$r else if (text[i] == 'r') { string stringId = ""; i += 7; while (text[i] != '"') { stringId += text[i]; i++; } id = int.Parse(stringId); return(FindObject(id)); } } else if ((text[i] == '"') && (text[i + 1] != '$')) { i++; string propertyName = ""; while (text[i] != '"') { propertyName += text[i]; i++; } i += 4; object objPropertyValue = null; string strPropertyValue = ""; if (text[i - 1] == '{') { i -= 1; int difference = 0; while (true) { if (text[i] == '{') { if (difference == 0) { openIndex = i; } difference++; } else if (text[i] == '}') { difference--; if (difference == 0) { closeIndex = i; break; } } i++; } StringBuilder sb = CutText(text, openIndex - 1, closeIndex + 1); objPropertyValue = ReadObject(sb); } else { while (text[i] != '"') { strPropertyValue += text[i]; i++; } } foreach (PropertyInfo property in mfObj.Obj.GetType().GetProperties()) { if (property.Name == propertyName) { if (property.PropertyType == typeof(int)) { property.SetValue(mfObj.Obj, int.Parse(strPropertyValue)); } else if (property.PropertyType == typeof(string)) { property.SetValue(mfObj.Obj, strPropertyValue); } else if (property.PropertyType == typeof(float)) { property.SetValue(mfObj.Obj, float.Parse(strPropertyValue)); } else if (property.PropertyType.IsEnum) { property.SetValue(mfObj.Obj, Enum.Parse(property.PropertyType, strPropertyValue)); } else { try { property.SetValue(mfObj.Obj, objPropertyValue); } catch { //MessageBox.Show(mfObj.Obj.GetType().ToString() + "-" + property.PropertyType.ToString() + "-" + objPropertyValue.ToString()); } } break; } } } } return(mfObj.Obj); }
private void WriteObject(object obj, int currIndex, int maxIndex) { Type objectType = obj.GetType(); Write("{\n"); spaceCount += 2; WriteSpaces(); if (!IsAlreadyExist(obj)) { if (!IsBaseType(objectType)) { if (objectType.IsGenericType) { MaximFormatterObject mfObject = new MaximFormatterObject(obj, objectId); mainObjectList.Add(mfObject); Write($"\"$id\": \"{objectId.ToString()}\",\n"); objectId++; WriteSpaces(); Write($"\"$type\": \"{objectType.ToString()}\",\n"); WriteSpaces(); Write($"\"$values\": {{\n"); spaceCount += 2; WriteSpaces(); var objectList = (IList)obj; int index = 0; foreach (object listElement in objectList) { WriteObject(listElement, index, objectList.Count - 1); if (index != objectList.Count - 1) { WriteSpaces(); } index++; } spaceCount -= 2; WriteSpaces(); Write("}\n"); } else { MaximFormatterObject mfObject = new MaximFormatterObject(obj, objectId); mainObjectList.Add(mfObject); Write($"\"$id\": \"{objectId.ToString()}\",\n"); objectId++; WriteSpaces(); Write($"\"$type\": \"{objectType.ToString()}\",\n"); PropertyInfo[] properties = objectType.GetProperties(); int index = 0; foreach (PropertyInfo property in properties) { if (!IsBaseType(property.PropertyType)) { if (property.PropertyType.IsEnum) { WriteSpaces(); Write($"\"{property.Name}\": \"{property.GetValue(obj)}\"\n"); } else { WriteSpaces(); Write($"\"{property.Name}\": "); var someObject = (object)property; WriteObject(property.GetValue(obj), index, properties.Length - 1); } } else { WriteSpaces(); Write($"\"{property.Name}\": \"{property.GetValue(obj).ToString()}\""); if (index != properties.Length - 1) { Write(","); } Write("\n"); } index++; } } } } else { int id = FindId(obj); Write($"\"$ref\": \"{id.ToString()}\"\n"); } spaceCount -= 2; WriteSpaces(); Write("}"); if (currIndex < maxIndex) { Write(","); } Write("\n"); }