Exemple #1
0
        private object ParseNumberPrimitiveValue()
        {
            double num2;
            int    num3;
            int    characterCount = 1;

            while (((this.tokenStartIndex + characterCount) < this.storedCharacterCount) || this.ReadInput())
            {
                char c = this.characterBuffer[this.tokenStartIndex + characterCount];
                if (((!char.IsDigit(c) && (c != '.')) && ((c != 'E') && (c != 'e'))) && ((c != '-') && (c != '+')))
                {
                    break;
                }
                characterCount++;
            }
            string s = this.ConsumeTokenToString(characterCount);

            if (int.TryParse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out num3))
            {
                return(num3);
            }
            if (!double.TryParse(s, NumberStyles.Float, (IFormatProvider)NumberFormatInfo.InvariantInfo, out num2))
            {
                throw JsonReaderExtensions.CreateException(Strings.JsonReader_InvalidNumberFormat(s));
            }
            return(num2);
        }
Exemple #2
0
 private bool EndOfInput()
 {
     if (this.scopes.Count > 1)
     {
         throw JsonReaderExtensions.CreateException(Strings.JsonReader_EndOfInputWithOpenScope);
     }
     this.nodeType = JsonNodeType.EndOfInput;
     return(false);
 }
Exemple #3
0
        private object ParseNullPrimitiveValue()
        {
            string a = this.ParseName();

            if (!string.Equals(a, "null"))
            {
                throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnexpectedToken(a));
            }
            return(null);
        }
Exemple #4
0
        private JsonNodeType ParseValue()
        {
            Scope local1 = this.scopes.Peek();

            local1.ValueCount++;
            char c = this.characterBuffer[this.tokenStartIndex];

            switch (c)
            {
            case '"':
            case '\'':
                bool flag;
                this.nodeValue = this.ParseStringPrimitiveValue(out flag);
                if (flag)
                {
                    object obj2 = TryParseDateTimePrimitiveValue((string)this.nodeValue);
                    if (obj2 != null)
                    {
                        this.nodeValue = obj2;
                    }
                }
                break;

            case '[':
                this.PushScope(ScopeType.Array);
                this.tokenStartIndex++;
                return(JsonNodeType.StartArray);

            case 't':
            case 'f':
                this.nodeValue = this.ParseBooleanPrimitiveValue();
                break;

            case '{':
                this.PushScope(ScopeType.Object);
                this.tokenStartIndex++;
                return(JsonNodeType.StartObject);

            case 'n':
                this.nodeValue = this.ParseNullPrimitiveValue();
                break;

            default:
                if ((!char.IsDigit(c) && (c != '-')) && (c != '.'))
                {
                    throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnrecognizedToken);
                }
                this.nodeValue = this.ParseNumberPrimitiveValue();
                break;
            }
            this.TryPopPropertyScope();
            return(JsonNodeType.PrimitiveValue);
        }
Exemple #5
0
        private object ParseBooleanPrimitiveValue()
        {
            string a = this.ParseName();

            if (string.Equals(a, "false"))
            {
                return(false);
            }
            if (!string.Equals(a, "true"))
            {
                throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnexpectedToken(a));
            }
            return(true);
        }
Exemple #6
0
        private JsonNodeType ParseProperty()
        {
            Scope local1 = this.scopes.Peek();

            local1.ValueCount++;
            this.PushScope(ScopeType.Property);
            this.nodeValue = this.ParseName();
            if (string.IsNullOrEmpty((string)this.nodeValue))
            {
                throw JsonReaderExtensions.CreateException(Strings.JsonReader_InvalidPropertyNameOrUnexpectedComma((string)this.nodeValue));
            }
            if (!this.SkipWhitespaces() || (this.characterBuffer[this.tokenStartIndex] != ':'))
            {
                throw JsonReaderExtensions.CreateException(Strings.JsonReader_MissingColon((string)this.nodeValue));
            }
            this.tokenStartIndex++;
            return(JsonNodeType.Property);
        }
Exemple #7
0
        public virtual bool Read()
        {
            this.nodeValue = null;
            if (!this.SkipWhitespaces())
            {
                return(this.EndOfInput());
            }
            Scope scope = this.scopes.Peek();
            bool  flag  = false;

            if (this.characterBuffer[this.tokenStartIndex] == ',')
            {
                flag = true;
                this.tokenStartIndex++;
                if (!this.SkipWhitespaces())
                {
                    return(this.EndOfInput());
                }
            }
            switch (scope.Type)
            {
            case ScopeType.Root:
                if (flag)
                {
                    throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnexpectedComma(ScopeType.Root));
                }
                if (scope.ValueCount > 0)
                {
                    throw JsonReaderExtensions.CreateException(Strings.JsonReader_MultipleTopLevelValues);
                }
                this.nodeType = this.ParseValue();
                break;

            case ScopeType.Array:
                if (flag && (scope.ValueCount == 0))
                {
                    throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnexpectedComma(ScopeType.Array));
                }
                if (this.characterBuffer[this.tokenStartIndex] == ']')
                {
                    this.tokenStartIndex++;
                    if (flag)
                    {
                        throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnexpectedComma(ScopeType.Array));
                    }
                    this.PopScope();
                    this.nodeType = JsonNodeType.EndArray;
                }
                else
                {
                    if (!flag && (scope.ValueCount > 0))
                    {
                        throw JsonReaderExtensions.CreateException(Strings.JsonReader_MissingComma(ScopeType.Array));
                    }
                    this.nodeType = this.ParseValue();
                }
                break;

            case ScopeType.Object:
                if (flag && (scope.ValueCount == 0))
                {
                    throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnexpectedComma(ScopeType.Object));
                }
                if (this.characterBuffer[this.tokenStartIndex] == '}')
                {
                    this.tokenStartIndex++;
                    if (flag)
                    {
                        throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnexpectedComma(ScopeType.Object));
                    }
                    this.PopScope();
                    this.nodeType = JsonNodeType.EndObject;
                }
                else
                {
                    if (!flag && (scope.ValueCount > 0))
                    {
                        throw JsonReaderExtensions.CreateException(Strings.JsonReader_MissingComma(ScopeType.Object));
                    }
                    this.nodeType = this.ParseProperty();
                }
                break;

            case ScopeType.Property:
                if (flag)
                {
                    throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnexpectedComma(ScopeType.Property));
                }
                this.nodeType = this.ParseValue();
                break;

            default:
                throw JsonReaderExtensions.CreateException(Strings.General_InternalError(InternalErrorCodes.JsonReader_Read));
            }
            return(true);
        }
Exemple #8
0
        private string ParseStringPrimitiveValue(out bool hasLeadingBackslash)
        {
            hasLeadingBackslash = false;
            char ch = this.characterBuffer[this.tokenStartIndex];

            this.tokenStartIndex++;
            StringBuilder stringValueBuilder = null;
            int           characterCount     = 0;

            while (((this.tokenStartIndex + characterCount) < this.storedCharacterCount) || this.ReadInput())
            {
                char ch2 = this.characterBuffer[this.tokenStartIndex + characterCount];
                if (ch2 == '\\')
                {
                    int num2;
                    if ((characterCount == 0) && (stringValueBuilder == null))
                    {
                        hasLeadingBackslash = true;
                    }
                    if (stringValueBuilder == null)
                    {
                        if (this.stringValueBuilder == null)
                        {
                            this.stringValueBuilder = new StringBuilder();
                        }
                        else
                        {
                            this.stringValueBuilder.Length = 0;
                        }
                        stringValueBuilder = this.stringValueBuilder;
                    }
                    stringValueBuilder.Append(this.ConsumeTokenToString(characterCount));
                    characterCount = 0;
                    if (!this.EnsureAvailableCharacters(2))
                    {
                        throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnrecognizedEscapeSequence(@"\"));
                    }
                    ch2 = this.characterBuffer[this.tokenStartIndex + 1];
                    this.tokenStartIndex += 2;
                    switch (ch2)
                    {
                    case '/':
                    case '\\':
                    case '"':
                    case '\'':
                    {
                        stringValueBuilder.Append(ch2);
                        continue;
                    }

                    case 'b':
                    {
                        stringValueBuilder.Append('\b');
                        continue;
                    }

                    case 'f':
                    {
                        stringValueBuilder.Append('\f');
                        continue;
                    }

                    case 'r':
                    {
                        stringValueBuilder.Append('\r');
                        continue;
                    }

                    case 't':
                    {
                        stringValueBuilder.Append('\t');
                        continue;
                    }

                    case 'u':
                        if (!this.EnsureAvailableCharacters(4))
                        {
                            throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnrecognizedEscapeSequence(@"\uXXXX"));
                        }
                        break;

                    case 'n':
                    {
                        stringValueBuilder.Append('\n');
                        continue;
                    }

                    default:
                        throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnrecognizedEscapeSequence(@"\" + ch2));
                    }
                    string s = this.ConsumeTokenToString(4);
                    if (!int.TryParse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out num2))
                    {
                        throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnrecognizedEscapeSequence(@"\u" + s));
                    }
                    stringValueBuilder.Append((char)num2);
                    continue;
                }
                if (ch2 == ch)
                {
                    string str2 = this.ConsumeTokenToString(characterCount);
                    this.tokenStartIndex++;
                    if (stringValueBuilder != null)
                    {
                        stringValueBuilder.Append(str2);
                        str2 = stringValueBuilder.ToString();
                    }
                    return(str2);
                }
                characterCount++;
            }
            throw JsonReaderExtensions.CreateException(Strings.JsonReader_UnexpectedEndOfString);
        }