示例#1
0
        public void GetFilledOptions(object configModel, string text, IConfigurationParser parser)
        {
            ParsedObject parsedObject = null;

            try
            {
                parsedObject = parser.Parse(text);
            }
            catch
            {
                throw new Exception("Couldn't parse file");
            }

            FillOptions(configModel, parsedObject);
        }
示例#2
0
        private Tuple <ParsedObject, int> ParseJsonArray(string text, int currentPoint)
        {
            ParsedObject parsedObject = new ParsedObject(ParsedObject.Type.Array);
            int          i            = 0;

            while (true)
            {
                Tuple <ParsedObject, int> nextValue = GetJsonValue(text, currentPoint);
                currentPoint = nextValue.Item2;

                parsedObject.AddSubObjectByIndex(i++, nextValue.Item1);
                while (gapSymbols.Contains(Convert.ToString(text[currentPoint])))
                {
                    currentPoint++;
                }
                if (text[currentPoint] == ']')
                {
                    break;
                }
                currentPoint++;
            }
            return(new Tuple <ParsedObject, int>(parsedObject, currentPoint));
        }
示例#3
0
        private Tuple <ParsedObject, int> ParseJsonObject(string text, int currentPoint)
        {
            ParsedObject parsedObject = new ParsedObject(ParsedObject.Type.NestedType);

            while (true)
            {
                Tuple <string, int> nextKey = GetJsonKey(text, currentPoint);
                currentPoint = nextKey.Item2 + 1;
                Tuple <ParsedObject, int> nextValue = GetJsonValue(text, currentPoint);
                currentPoint = nextValue.Item2;
                parsedObject.AddSubObjectByKey(nextKey.Item1, nextValue.Item1);
                while (gapSymbols.Contains(Convert.ToString(text[currentPoint])))
                {
                    currentPoint++;
                }
                if (text[currentPoint] == '}')
                {
                    break;
                }
                currentPoint++;
            }

            return(new Tuple <ParsedObject, int>(parsedObject, currentPoint));
        }
示例#4
0
        private Tuple <ParsedObject, int> GetJsonValue(string text, int currentPoint)
        {
            string jsonValue = "";
            string symbols   = ",[]{}";
            bool   quotationMarkWasClosed = true;

            while (!symbols.Contains(Convert.ToString(text[currentPoint])) || (symbols.Contains(Convert.ToString(text[currentPoint])) && !quotationMarkWasClosed))
            {
                if (text[currentPoint] == '\"')
                {
                    quotationMarkWasClosed ^= true;
                }
                jsonValue += text[currentPoint++];
            }
            if (text[currentPoint] == '{' || text[currentPoint] == '[')
            {
                Tuple <ParsedObject, int> nextItem = ParseJsonEntity(text, currentPoint);
                return(new Tuple <ParsedObject, int>(nextItem.Item1, nextItem.Item2 + 1));
            }
            ParsedObject response = new ParsedObject(ParsedObject.Type.SingleValue);

            response.SetValue(myTrim(jsonValue));
            return(new Tuple <ParsedObject, int>(response, currentPoint));
        }
示例#5
0
        private ParsedObject Go()
        {
            ParsedObject newObject;

            if (!isNextSymbolIsBracket())
            {
                newObject = new ParsedObject(ParsedObject.Type.SingleValue);
                newObject.SetValue(ReadTillBracket());
                return(newObject);
            }

            newObject = new ParsedObject(ParsedObject.Type.NestedType);
            while (true)
            {
                string tag = ReadTag();
                newObject.AddSubObjectByKey(tag, Go());
                ReadTag(); // closing
                if (isNextTagIsClosing())
                {
                    break;
                }
            }
            return(newObject);
        }
示例#6
0
 public void AddSubObjectByIndex(int index, ParsedObject subObject)
 {
     subObjects.Add(index.ToString(), subObject);
 }
示例#7
0
        public void FillOptions(object configModel, ParsedObject parsedObject)
        {
            var fields = configModel.GetType().GetFields();

            foreach (var field in fields)
            {
                ParsedObject subObject = parsedObject.GetSubObjectByKey(field.Name);

                if (subObject == null)
                {
                    continue;
                }

                if (subObject.myType == ParsedObject.Type.SingleValue)
                {
                    try
                    {
                        field.SetValue(configModel, Convert.ChangeType(subObject.GetValue(), field.FieldType));
                    }
                    catch { }

                    continue;
                }

                if (subObject.myType == ParsedObject.Type.NestedType && field.FieldType.IsNested)
                {
                    FillOptions(field.GetValue(configModel), subObject);

                    continue;
                }

                if (subObject.myType == ParsedObject.Type.Array && field.FieldType.IsArray)
                {
                    var array = (Array)field.GetValue(configModel);

                    if (array.GetType().GetElementType().IsNested || array.GetType().GetElementType().IsArray)
                    {
                        for (int i = 0; i < array.Length; i++)
                        {
                            ParsedObject subsubObject = subObject.GetSubObjectByIndex(i);

                            if (subsubObject == null)
                            {
                                continue;
                            }

                            if (subsubObject.myType != ParsedObject.Type.NestedType && subsubObject.myType != ParsedObject.Type.Array)
                            {
                                continue;
                            }

                            FillOptions(array.GetValue(i), subObject.GetSubObjectByIndex(i));
                        }

                        continue;
                    }

                    if (!array.GetType().IsNested&& !array.GetType().GetElementType().IsArray)
                    {
                        for (int i = 0; i < array.Length; i++)
                        {
                            try
                            {
                                array.SetValue(Convert.ChangeType(subObject.GetSubObjectByIndex(i).GetValue(), array.GetType().GetElementType()), i);
                            }
                            catch { }
                        }
                    }
                }
            }
        }