Exemple #1
0
 private IJSONDocument LoadFromSource(JSONSource source)
 {
     currentSource = source;
     using ( currentSource )
     {
         if (source.ReadNextChar() < 0)
         {
             throw new JSONException("unexpected end met");
         }
         currentDoc = JSONDocument.CreateDocument();
         try
         {
             var root = ReadValue();
             // TODO read til end?
             currentDoc.Root = root;
             var cd = currentDoc;
             currentDoc    = null;
             currentSource = null;
             return(cd);
         } catch (InvalidOperationException e)
         {
             throw new JSONException("Invalid Operation @ " + currentSource.pos, e);
         }
         catch (ArgumentException e)
         {
             throw new JSONException("Invalid Argument @ " + currentSource.pos, e);
         }
     }
 }
Exemple #2
0
        private IJSONItemNumber ReadItemNumber()
        {
            Debug.Assert(currentSource.currentChar == '-' || (currentSource.currentChar >= '0' && currentSource.currentChar <= '9'));
            var num = new StringBuilder();

            if (currentSource.currentChar == '-')
            {
                num.Append('-');
                currentSource.ReadNextCharNoEOF();
            }
            if (currentSource.currentChar == '0')
            {
                num.Append('0');
                currentSource.ReadNextChar();
            }
            else
            {
                if (currentSource.currentChar >= '1' && currentSource.currentChar <= '9')
                {
                    while (currentSource.currentChar >= '0' && currentSource.currentChar <= '9')
                    {
                        num.Append((char)currentSource.currentChar);
                        currentSource.ReadNextChar();
                    }
                }
                else
                {
                    throw new JSONUnexpectedCharException(currentSource.currentChar, currentSource.pos);
                }
            }
            if (currentSource.currentChar == '.')
            {
                num.Append('.');
                currentSource.ReadNextCharNoEOF();
                if (currentSource.currentChar >= '0' && currentSource.currentChar <= '9')
                {
                    while (currentSource.currentChar >= '0' && currentSource.currentChar <= '9')
                    {
                        num.Append((char)currentSource.currentChar);
                        currentSource.ReadNextChar();
                    }
                }
                else
                {
                    throw new JSONUnexpectedCharException(currentSource.currentChar, currentSource.pos);
                }
            }
            if (currentSource.currentChar == 'e' || currentSource.currentChar == 'E')
            {
                num.Append('E');
                currentSource.ReadNextCharNoEOF();
                if (currentSource.currentChar == '+' || currentSource.currentChar == '-')
                {
                    if (currentSource.currentChar == '-')
                    {
                        num.Append((char)currentSource.currentChar);
                    }
                    currentSource.ReadNextCharNoEOF();
                }
                if (currentSource.currentChar >= '0' && currentSource.currentChar <= '9')
                {
                    while (currentSource.currentChar >= '0' && currentSource.currentChar <= '9')
                    {
                        num.Append((char)currentSource.currentChar);
                        currentSource.ReadNextChar();
                    }
                }
                else
                {
                    throw new JSONUnexpectedCharException(currentSource.currentChar, currentSource.pos);
                }
            }
            return(currentDoc.CreateItemNumber(num.ToString()));
        }