示例#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 + '\'');
 }
示例#3
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 + '\'');
            }
        }
示例#4
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;
     }
 }