private static SObjectInfo GetMembers(string objName, string[] script) { SObjectInfo objRes = new SObjectInfo(); var objBody = script.SkipWhile(s => s != objName).Skip(2) .Take(script.SkipWhile(s => s != objName).Skip(2) .Count(s => s != StgFormat.OBJECT_TAG_END) + 1).ToList(); while (objBody.Count != 0) { switch (objBody[0]) { case StgFormat.STRING_START: { var d = new KeyValuePair <Type, object>(typeof(string), new string(objBody.Skip(3).Take(1).Single().ToArray()).Replace(StgFormat.STRING_FORMAT, "")); objRes.Name = objBody.Skip(1).Take(1).Single(); objRes.Fields.Add(objBody.Skip(1).Take(1).Single(), d); objBody.RemoveRange(0, 5); } break; case StgFormat.VARIABLES_START: { string value = new string(objBody.Skip(3).Take(1).Single() .Where(c => c != char.Parse(StgFormat.FIELD_END)).ToArray()).Replace(StgFormat.STRING_FORMAT, string.Empty); TryGetTypeAndFormat(value, out Type type, out string format); objRes.Name = objBody.Skip(1).Take(1).Single(); if (format != null) { value = value.Replace(format, string.Empty); } if (type != null) { var d = new KeyValuePair <Type, object>(type, value.CastToValueType(type)); objRes.Fields.Add(objBody.Skip(1).Take(1).Single(), d); } objBody.RemoveRange(0, 3); } break; case StgFormat.ARRAY_START: { string arrayValues = new string(objBody.Skip(4).TakeWhile(c => !c.Contains(StgFormat.ARRAY_TAG_END)) .ToArray().Aggregate((res, item) => res + item) .Where(c => c != char.Parse(StgFormat.ARRAY_TAG_START) && c != char.Parse(StgFormat.ARRAY_TAG_END)).ToArray()); string oneValue = new string(arrayValues.TakeWhile(c => c != ',').ToArray()).Replace(StgFormat.STRING_FORMAT, string.Empty); TryGetTypeAndFormat(oneValue, out Type t, out string format, true); if (format != null) { arrayValues = arrayValues.Replace(format, string.Empty); } if (t != null) { var d = new KeyValuePair <Type, object>(t, arrayValues.CastToArray(t)); objRes.Fields.Add(objBody.Skip(1).Take(1).Single(), d); } objBody = objBody.SkipWhile(c => !c.Contains(StgFormat.ARRAY_TAG_END)).Skip(1).ToList(); } break; case StgFormat.COMMENT_FORMAT_START: { objBody = objBody.SkipWhile(c => c != StgFormat.COMMENT_FORMAT_END).Skip(1).ToList(); } break; default: objBody.RemoveAt(0); break; } } return(objRes); }
public static SObject Deserialize(string stgScript) { SObjectInfo result = new SObjectInfo(); List <string> script = stgScript.SplitStgScript(' '); while (script.Count != 0) { switch (script[0]) { case StgFormat.OBJECT_START: { string objTypeName = script.Skip(1).Take(1).Single(); result = GetMembers(objTypeName, script.ToArray()); result.Name = objTypeName; script.RemoveRange(0, script.Count(c => c != StgFormat.OBJECT_TAG_END)); } break; case StgFormat.STRING_START: { string value = new string(script.Skip(3).Take(1).Single(). Where(c => c != char.Parse(StgFormat.STRING_FORMAT) || c != char.Parse(StgFormat.FIELD_END)) .ToArray()); result.Name = script.Skip(1).Take(1).Single(); var d = new KeyValuePair <Type, object>(typeof(string), value.Replace(StgFormat.STRING_FORMAT, string.Empty)); result.Fields.Add(script.Skip(1).Take(1).Single(), d); script.RemoveRange(0, 4); } break; case StgFormat.VARIABLES_START: { string value = new string(script.Skip(3).Take(1).Single() .Where(c => c != char.Parse(StgFormat.FIELD_END)).ToArray()).Replace(StgFormat.STRING_FORMAT, string.Empty); TryGetTypeAndFormat(value, out Type type, out string format); result.Name = script.Skip(1).Take(1).Single(); if (format != null) { value = value.Replace(format, string.Empty); } if (type != null) { var d = new KeyValuePair <Type, object>(type, value.CastToValueType(type)); result.Fields.Add(script.Skip(1).Take(1).Single(), d); } script.RemoveRange(0, 3); } break; case StgFormat.ARRAY_START: { string arrayValues = new string(script.Skip(4).TakeWhile(c => !c.Contains(StgFormat.ARRAY_TAG_END)) .ToArray().Aggregate((res, item) => res + item) .Where(c => c != char.Parse(StgFormat.ARRAY_TAG_START) && c != char.Parse(StgFormat.ARRAY_TAG_END)).ToArray()); string oneValue = new string(arrayValues.TakeWhile(c => c != ',').ToArray()).Replace(StgFormat.STRING_FORMAT, string.Empty); TryGetTypeAndFormat(oneValue, out Type t, out string format, true); result.Name = t.Name; if (format != null) { arrayValues = arrayValues.Replace(format, string.Empty); } if (t != null) { var d = new KeyValuePair <Type, object>(t, arrayValues.CastToArray(t)); result.Fields.Add(script.Skip(1).Take(1).Single(), d); } script = script.SkipWhile(c => !c.Contains(StgFormat.ARRAY_TAG_END)).Skip(1).ToList(); } break; case StgFormat.COMMENT_FORMAT_START: { script = script.SkipWhile(c => c != StgFormat.COMMENT_FORMAT_END).Skip(1).ToList(); } break; default: script.RemoveAt(0); break; } } foreach (var item in result.Fields) { result.AddMember(item.Value.Key, item.Value.Value); } return(result as SObject); }