Exemplo n.º 1
0
        //The starting point of fun
        private void InitializeStructure()
        {
            JKeyValuePair newPair = new JKeyValuePair {
                Parent = null
            };
            JItem Root = new JItem {
                root = true, isObject = true, isArray = false, Parent = null
            };

            keyValueStack.Push(newPair);
            itemStack.Push(Root);

            indexOfTheChar = -1;
            syntaxChars.Clear();
            syntaxChars.Push(',');
            currentItem = new JItem();
            InnerParse();
        }/// <summary>
Exemplo n.º 2
0
        }/// <summary>

        /// The Main Big Parsing Method
        /// </summary>
        private void StartParse()
        {
            //index of top secret pentagon data
            ++indexOfTheChar;
            try
            {
                if (syntaxChars?.Peek() == '\"' && insideQuotes)
                {
                    if (source[indexOfTheChar] == '\"')
                    {
                        buffer      += source[indexOfTheChar];
                        insideQuotes = false;
                        return;
                    }
                    buffer += source[indexOfTheChar];
                    return;
                }
            }
            catch { /*MessageBox.Show("Дирижабль");*/ }

            switch (source[indexOfTheChar])
            {
            case '{':
                currentItem = itemStack.Peek();
                currentKV   = keyValueStack.Peek();
                //Проверка на ожидание инициализации значения Value пары KeyValue
                if (pending4Value)
                {
                    JItem item = new JItem {
                        isObject = true
                    };
                    item.Parent = currentItem;
                    ((JKeyValuePair)currentKV).Value = item;
                    buffer = "";
                    keyValueStack.Push(item);
                    itemStack.Push(item);
                }
                else
                {
                    JItem nItm = new JItem {
                        isObject = true
                    };
                    currentItem.Items.Add(nItm);
                    nItm.Parent = currentItem;
                    itemStack.Push(nItm);
                }

                pending4Value = false;
                break;

            case '}':
                currentKV   = keyValueStack.Peek();
                currentItem = itemStack.Peek();
                if (pending4Value)
                {
                    JItem item = new JItem();
                    item.Parent   = currentItem;
                    item.contents = buffer;
                    ((JKeyValuePair)currentKV).Value = item;
                    buffer = "";
                    keyValueStack.Pop();
                }
                currentItem   = itemStack.Pop();
                pending4Value = false;
                break;

            case '[':
                currentItem = itemStack.Peek();
                currentKV   = keyValueStack.Peek();
                //Проверка на ожидание инициализации значения Value пары KeyValue
                if (pending4Value)
                {
                    JItem item = new JItem {
                        isObject = false, isArray = true
                    };
                    item.Parent = currentItem;
                    ((JKeyValuePair)currentKV).Value = item;
                    buffer = "";
                    keyValueStack.Push(item);
                    itemStack.Push(item);
                }
                else
                {
                    JItem nItm = new JItem {
                        isObject = false, isArray = true
                    };
                    currentItem.Items.Add(nItm);
                    nItm.Parent = currentItem;
                    itemStack.Push(nItm);
                }

                pending4Value = false;
                break;

            case ']':
                currentKV   = keyValueStack.Peek();
                currentItem = itemStack.Peek();
                if (pending4Value)
                {
                    JItem item = new JItem {
                        singleValue = true
                    };
                    item.Parent   = currentItem;
                    item.contents = buffer;
                    ((JKeyValuePair)currentKV).Value = item;
                    buffer = "";
                    keyValueStack.Pop();
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(buffer))
                    {
                        currentItem.Items.Add(new JItem {
                            contents = buffer, singleValue = true
                        });
                        buffer = "";
                    }
                }
                //currentItem = currentItem.Parent;
                currentItem   = itemStack.Pop();
                pending4Value = false;
                break;

            case ':':
                currentKV   = keyValueStack.Peek();
                currentItem = itemStack.Peek();
                syntaxChars.Push(':');
                if (!pending4Value)
                {
                    JKeyValuePair pair = new JKeyValuePair();
                    pair.Key = new JItem {
                        singleValue = true
                    };
                    pair.Key.contents = buffer;
                    pair.Parent       = currentItem;
                    currentItem.Items.Add(pair);
                    keyValueStack.Push(pair);
                }
                else
                {
                    //throw new InvalidEnumArgumentException(); //не было более подходящего, лел
                }
                pending4Value = true;
                buffer        = "";
                break;

            case ',':
                currentKV = keyValueStack.Peek();
                try { currentItem = itemStack.Peek(); }
                catch { }
                if (pending4Value)
                {
                    JItem jI = new JItem {
                        singleValue = true, contents = buffer, Parent = currentItem
                    };
                    ((JKeyValuePair)currentKV).Value = jI;
                    keyValueStack.Pop();
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(buffer))
                    {
                        currentItem.Items.Add(new JItem {
                            contents = buffer, singleValue = true
                        });
                        buffer = ""; return;
                    }
                }
                pending4Value = false;
                syntaxChars.Push(',');
                buffer = "";

                break;

            case '\"':
                buffer += source[indexOfTheChar];
                syntaxChars.Push('\"');
                insideQuotes = !insideQuotes;
                break;

            case ' ':
            case '\t':
            case '\r':
            case '\n':
                break;

            default:
                buffer += source[indexOfTheChar];
                break;
            }
        }