示例#1
0
        private void ReadArray(System.IO.StreamReader sr)
        {
            int             i;
            List <JsonNode> array = new List <JsonNode>();

            this.SetValue(array);
            bool isOpen    = false;
            bool allowNext = true;
            char lastChar  = '\0';

            while ((i = sr.Peek()) >= 0)
            {
                char c = (char)i;
                if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
                {
                    if (isOpen)
                    {
                        if (c == ']')
                        {
                            c        = (i = sr.Read()) > 0 ? (char)c : '\0';
                            lastChar = c;
                            break;
                        }
                        else if (allowNext)
                        {
                            JsonNode node = new JsonNode();
                            node.Read(sr);
                            array.Add(node);
                            allowNext = false;
                            lastChar  = '\0';
                            continue;
                        }
                        else if (c == ',')
                        {
                            allowNext = true;
                        }
                        else
                        {
                            throw new Exception("Parsing Object failed: Expected ',' or ']', got '" + c + '\'');
                        }
                    }
                    else
                    {
                        if (c != '[')
                        {
                            throw new Exception("Parsing Object failed: Expected '{', got '" + c + '\'');
                        }
                        isOpen    = true;
                        allowNext = true;
                    }
                }
                c        = (i = sr.Read()) > 0 ? (char)c : '\0';
                lastChar = c;
            }
        }
示例#2
0
        private void ReadObject(System.IO.StreamReader sr)
        {
            int    i;
            bool   isOpen        = false;
            bool   hasLabel      = false;
            bool   getNextValue  = false;
            bool   getNextKeySet = false;
            string label         = "";
            char   lastChar      = '\0';
            Dictionary <string, JsonNode> dict = new Dictionary <string, JsonNode>();

            this.SetValue(dict);
            while ((i = sr.Peek()) >= 0)
            {
                char c = (char)i;
                if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
                {
                    if (isOpen)
                    {         //node is opened ('{' already got read) and ready to be parsed
                        if (hasLabel)
                        {     //Label for current object item was already started and thus can be
                            if (getNextKeySet)
                            { //Next key set needs to be received (value already parsed) or the current object has to be closed
                                if (c == ',')
                                {
                                    hasLabel      = false;
                                    getNextKeySet = false;
                                    getNextValue  = false;
                                    hasLabel      = false;
                                }
                                else if (c == '}')
                                {
                                    c        = (char)sr.Read();
                                    lastChar = c;
                                    break;
                                }
                                else
                                {
                                    throw new Exception("Parsing Object failed: Expected ',' or '}', got '" + c + '\'');
                                }
                            }
                            else if (getNextValue)
                            {//Next value of current label needs to get parsed
                                JsonNode child = new JsonNode();
                                child.Read(sr);
                                dict.Add(label, child);
                                getNextKeySet = true;
                                lastChar      = '\0';
                                continue;
                            }
                            else
                            {//Separator for label and string expected
                                if (c != ':')
                                {
                                    throw new Exception("Parsing Object failed: Expected '\"', got '" + c + '\'');
                                }
                                getNextValue = true;
                            }
                        }
                        else
                        {//No label yet found --> get label of current item
                            if (c != '"')
                            {
                                throw new Exception("Parsing Object failed: Expected '\"', got '" + c + '\'');
                            }
                            label    = ParseStringFromEncoded(sr);
                            hasLabel = true;
                            lastChar = '"';
                            continue;
                        }
                    }
                    else
                    {//node yet has to be opened
                        if (c != '{')
                        {
                            throw new Exception("Parsing Object failed: Expected '{', got '" + c + '\'');
                        }
                        isOpen = true;
                    }
                }
                c        = (i = sr.Read()) > 0 ? (char)c : '\0';
                lastChar = c;
            }
            if (lastChar != '}')
            {
                throw new Exception("Parsing Object failed: Expected '}', got '" + lastChar + '\'');
            }
        }