Ejemplo n.º 1
0
 private async Task ParseExactAsync(
     String expected,
     JsonToken?tokenType,
     Object value,
     CancellationToken cancellationToken)
 {
     if (await this.EnsureCharsAsync(expected.Length, cancellationToken))
     {
         var str = new String(this.buffer, this.bufferPosition, expected.Length);
         if (String.Equals(expected, str, StringComparison.OrdinalIgnoreCase))
         {
             this.bufferPosition += expected.Length;
             if (tokenType.HasValue)
             {
                 this.SetToken(tokenType.Value, value);
             }
         }
         else
         {
             throw new JsonReaderException(
                       $"Unexpected value while reading {expected}: {str}"
                       );
         }
     }
     else
     {
         throw new JsonReaderException(
                   $"Unexpected EOF reading expected value: {expected}"
                   );
     }
 }
Ejemplo n.º 2
0
        public IToken?FindClosestEquivalent(TokenCollection tokens)
        {
            List <IToken> matches = new List <IToken>();

            // Try to find exact match, but capture potential non-exact matches along the way.
            foreach (IToken potentialMatch in tokens)
            {
                JsonToken?jsonPotentialMatch = potentialMatch as JsonToken;
                if (jsonPotentialMatch != null)
                {
                    if (jsonPotentialMatch.Path == this.Path)
                    {
                        // Found an 'exact' match.
                        return(jsonPotentialMatch);
                    }
                    else
                    {
                        matches.Add(jsonPotentialMatch);
                    }
                }
            }

            // We didn't find an exact match, but we may have found name matches.
            if (matches.Count > 1)
            {
                Console.WriteLine("WARNING: No exact JSON match, but many name matched. May need to improve hueristics.");
            }
            if (matches.Count >= 1)
            {
                return(matches[0]);
            }

            // No matches. We may have a reproduceability issue.
            return(null);
        }
Ejemplo n.º 3
0
        private JsonToken?ReadExpectedToken()
        {
            if (_expToken == null)
            {
                return(null);
            }
            var token = _expToken.Value;

            _expToken = null;
            switch (token)
            {
            case JsonToken.String:
                return(ReadString(JsonToken.Colon));

            case JsonToken.Colon:
                return(GetNextCharSkipWhitespace() == JsonLiterals.Colon
                        ? JsonToken.Colon
                        : ExceptionHelper.ThrowInvalidJsonException <JsonToken>());

            case JsonToken.Comma:
                return(GetNextCharSkipWhitespace() == JsonLiterals.Comma
                        ? JsonToken.Comma
                        : ExceptionHelper.ThrowInvalidJsonException <JsonToken>());

            default:
                return(null);
            }
        }
Ejemplo n.º 4
0
        public void testCreateGenericInstanceOfNullable()
        {
            var fieldType = this.GetType().GetField("token").FieldType;

            Junit.Framework.Assert.AssertTrue(fieldType.IsGenericType);

            var nullable = fieldType.GetGenericTypeDefinition();

            Assert.NotNull(nullable);
            Assert.IsTrue(nullable.IsGenericTypeDefinition);

            Type createdType = nullable.MakeGenericType(Nullable.GetUnderlyingType(fieldType));

            Assert.IsNotNull(createdType);
            Assert.AreEqual(typeof(JsonToken?), createdType);

            // should actually be the very same type...
            Assert.AreEqual(fieldType, createdType);
            Assert.IsTrue(fieldType.IsAssignableFrom(createdType));

            JsonToken?defaultValue = (JsonToken?)Activator.CreateInstance(createdType);

            Assert.AreEqual(default(JsonToken?), defaultValue);
            Assert.AreEqual(null, defaultValue);
        }
Ejemplo n.º 5
0
        public void testToString()
        {
            JsonToken?t = null;

            Assert.AreEqual(string.Empty, t.ToString());
            t = JsonToken.EndArray;
            Assert.AreNotEqual(string.Empty, t.ToString());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Reads current token without advancing to next token position.
        /// </summary>
        /// <returns>Current token.</returns>
        public JsonToken PeekToken()
        {
            if (!_lookAhead.HasValue)
            {
                _lookAhead = NextToken();
            }

            return(_lookAhead.GetValueOrDefault());
        }
Ejemplo n.º 7
0
        private void _ThrowGeoJsonException(JsonReader reader, JsonToken?expectedToken)
        {
            var li = reader as IJsonLineInfo;

            if ((li != null) && li.HasLineInfo())
            {
                if (expectedToken.HasValue)
                {
                    throw new JsonReaderException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  SR.InvalidGeoJsonLineInfoExpectingException,
                                  li.LineNumber,
                                  li.LinePosition,
                                  expectedToken.Value,
                                  reader.TokenType
                                  )
                              );
                }
                else
                {
                    throw new JsonReaderException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  SR.InvalidGeoJsonLineInfoException,
                                  li.LineNumber,
                                  li.LinePosition
                                  )
                              );
                }
            }
            else
            {
                if (expectedToken.HasValue)
                {
                    throw new JsonReaderException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  SR.InvalidGeoJsonExpectingException,
                                  expectedToken.Value,
                                  reader.TokenType
                                  )
                              );
                }
                else
                {
                    throw new JsonReaderException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  SR.InvalidGeoJsonException
                                  )
                              );
                }
            }
        }
        /// <summary>
        ///     Reads to the next token in the json document, and updates the context
        ///     accordingly.
        /// </summary>
        /// <returns>
        ///     True if a token was read, false if there are no more tokens to read./
        /// </returns>
        public override bool Read()
        {
            bool result = jsonReader.Read();

            if (result)
            {
                currentToken = jsonReader.Token;
                UpdateContext();
            }
            return(result);
        }
        private bool SetEnd(JContainer c)
        {
            JsonToken?endToken = this.GetEndToken(c);

            if (endToken.get_HasValue())
            {
                base.SetToken(endToken.get_Value());
                this._current = c;
                this._parent  = c;
                return(true);
            }
            return(this.ReadOver(c));
        }
Ejemplo n.º 10
0
        // Token: 0x060013CE RID: 5070 RVA: 0x0006929C File Offset: 0x0006749C
        private bool SetEnd(JContainer c)
        {
            JsonToken?endToken = this.GetEndToken(c);

            if (endToken != null)
            {
                base.SetToken(endToken.GetValueOrDefault());
                this._current = c;
                this._parent  = c;
                return(true);
            }
            return(this.ReadOver(c));
        }
Ejemplo n.º 11
0
        private bool method_18(JContainer jcontainer_0)
        {
            JsonToken?nullable = this.method_16(jcontainer_0);

            if (nullable.HasValue)
            {
                base.SetToken(nullable.Value);
                this.jtoken_2 = jcontainer_0;
                this.jtoken_1 = jcontainer_0;
                return(true);
            }
            return(this.method_14(jcontainer_0));
        }
Ejemplo n.º 12
0
        private bool SetEnd(JContainer c)
        {
            JsonToken?endToken = GetEndToken(c);

            if (endToken.HasValue)
            {
                SetToken(endToken.Value);
                _current = c;
                _parent  = c;
                return(true);
            }
            return(ReadOver(c));
        }
Ejemplo n.º 13
0
        private bool SetEnd(JContainer c)
        {
            JsonToken?endToken = this.GetEndToken(c);

            if (!endToken.HasValue)
            {
                return(this.ReadOver((JToken)c));
            }
            base.SetToken(endToken.Value);
            this._current = (JToken)c;
            this._parent  = (JToken)c;
            return(true);
        }
Ejemplo n.º 14
0
 private JsonToken ReadUnexpectedToken_Unspecified()
 {
     while (true)
     {
         var currChar = GetNextCharSkipWhitespace();
         if (currChar == JsonLiterals.QuotationMark)
         {
             RepeatLastChar();
             return(ReadString(null));
         }
         if (currChar == JsonLiterals.ObjectStart)
         {
             if (GetNextCharSkipWhitespace() == JsonLiterals.ObjectEnd)
             {
                 return(JsonToken.ObjectEmpty);
             }
             _modeStack.Push(Mode.Object);
             RepeatLastChar();
             _expToken = JsonToken.String;
             return(JsonToken.ObjectStart);
         }
         if (char.IsDigit(currChar) || currChar == '-')
         {
             RepeatLastChar();
             return(ReadNumber());
         }
         if (currChar == 't' || currChar == 'f')
         {
             RepeatLastChar();
             return(ReadBoolean());
         }
         if (currChar == 'n')
         {
             RepeatLastChar();
             return(ReadNull());
         }
         if (currChar == JsonLiterals.ArrayStart)
         {
             if (GetNextCharSkipWhitespace() == JsonLiterals.ArrayEnd)
             {
                 return(JsonToken.ArrayEmpty);
             }
             _modeStack.Push(Mode.Array);
             RepeatLastChar();
             return(JsonToken.ArrayStart);
         }
         return(ExceptionHelper.ThrowInvalidJsonException <JsonToken>());
     }
 }
Ejemplo n.º 15
0
 private bool SetEnd(JContainer c)
 {
   JsonToken? endToken = GetEndToken(c);
   if (endToken != null)
   {
     SetToken(endToken.Value);
     _current = c;
     _parent = c;
     return true;
   }
   else
   {
     return ReadOver(c);
   }
 }
Ejemplo n.º 16
0
        private bool SetEnd(JContainer c)
        {
            JsonToken?endToken = GetEndToken(c);

            if (endToken != null)
            {
                SetToken(endToken.GetValueOrDefault());
                _current = c;
                _parent  = c;
                return(true);
            }
            else
            {
                return(ReadOver(c));
            }
        }
Ejemplo n.º 17
0
        private bool SetEnd(JContainer c)
        {
            JsonToken?endToken = this.GetEndToken(c);

            if (endToken != null)
            {
                this.SetToken(endToken.Value);
                this._current = c;
                this._parent  = c;
                return(true);
            }
            else
            {
                return(this.ReadOver(c));
            }
        }
Ejemplo n.º 18
0
        private JsonToken ReadUnexpectedToken_Object()
        {
            var currChar = GetNextCharSkipWhitespace();

            if (currChar == JsonLiterals.Comma)
            {
                _expToken = JsonToken.String;
                return(JsonToken.Comma);
            }
            if (currChar == JsonLiterals.ObjectEnd)
            {
                _modeStack.Pop();
                return(JsonToken.ObjectEnd);
            }
            RepeatLastChar();
            return(ReadUnexpectedToken_Unspecified());
        }
Ejemplo n.º 19
0
        public void ParseValue()
        {
            JsonToken?token = null;

            switch (currentSlice.Type)
            {
            case SliceType.Binary:
                token = JsonToken.Bytes;
                break;

            case SliceType.Boolean:
                token = JsonToken.Boolean;
                break;

            case SliceType.Double:
                token = JsonToken.Float;
                break;

            case SliceType.Int:
            case SliceType.UInt:
            case SliceType.SmallInt:
                token = JsonToken.Integer;
                break;

            case SliceType.UtcDate:
                token = JsonToken.Date;
                break;

            case SliceType.String:
                token = JsonToken.String;
                break;

            case SliceType.Null:
                token = JsonToken.Null;
                break;
            }

            if (token.HasValue == false)
            {
                throw new InvalidOperationException($"Error at reading vpack slice parsing value");
            }

            SetToken(token.Value, currentSlice.Value());
        }
Ejemplo n.º 20
0
 public override bool Read()
 {
     if (!wasPeeked)
     {
         bool num = jsonReader.Read();
         if (num)
         {
             currentToken = jsonReader.Token;
             UpdateContext();
         }
         else
         {
             currentToken = null;
         }
         wasPeeked = false;
         return(num);
     }
     wasPeeked = false;
     return(!currentToken.HasValue);
 }
Ejemplo n.º 21
0
        public void testByRef()
        {
            JsonToken?val = default(JsonToken);

            Assert.AreEqual(default(JsonToken), GetValueOrDefault(ref val));
            Assert.AreEqual(default(JsonToken), GetValue(ref val));
            Assert.AreEqual(true, HasValue(ref val));

            SetToNull(ref val);
            Assert.IsFalse(val.HasValue);

            Assert.AreEqual(default(JsonToken), GetValueOrDefault(ref val));
            Assert.AreEqual(false, HasValue(ref val));


            SetToDefault(ref val);
            Assert.AreEqual(JsonToken.Default, val.Value);

            Assert.AreEqual(JsonToken.Default, GetValueOrDefault(ref val));
            Assert.AreEqual(JsonToken.Default, GetValue(ref val));
            Assert.AreEqual(true, HasValue(ref val));
        }
Ejemplo n.º 22
0
        /// <summary>
        ///     Reads to the next token in the json document, and updates the context
        ///     accordingly.
        /// </summary>
        /// <returns>
        ///     True if a token was read, false if there are no more tokens to read.
        /// </returns>
        public override bool Read()
        {
            if (wasPeeked)
            {
                wasPeeked = false;
                return(currentToken == null);
            }

            bool result = jsonReader.Read();

            if (result)
            {
                currentToken = jsonReader.Token;
                UpdateContext();
            }
            else
            {
                currentToken = null;
            }
            wasPeeked = false;
            return(result);
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Advances to next token position.
 /// </summary>
 public void ConsumeToken()
 {
     Debug.Assert(_lookAhead.HasValue);
     _lookAhead = null;
 }
Ejemplo n.º 24
0
 bool HasValue(ref JsonToken?val)
 {
     return(val.HasValue);
 }
Ejemplo n.º 25
0
        public void testNullableEnum()
        {
            Console.WriteLine(typeof(JsonToken?).ToString());
            Console.WriteLine(Nullable.GetUnderlyingType(typeof(JsonToken?)).ToString());

            Assert.AreEqual(0, (int) token.GetValueOrDefault());
            Assert.AreEqual(JsonToken.EndArray, token.GetValueOrDefault(JsonToken.EndArray));
            Assert.IsFalse(token.HasValue);
            Assert.IsTrue(token == null);
            Assert.IsFalse(token != null);

            try
            {
                Console.WriteLine(token.GetType().ToString());
                Assert.Fail("should throw");
            }
            catch (NullReferenceException)
            {
            }

            try
            {
                Console.WriteLine(token.Value.ToString());
                Assert.Fail("should throw");
            }
            catch (InvalidOperationException)
            {
            }

            token = JsonToken.Boolean;

            Assert.AreEqual(typeof(JsonToken), token.GetType());
            Assert.IsNull(Nullable.GetUnderlyingType(token.GetType()));
            Assert.AreEqual(token, JsonToken.Boolean);
            Assert.AreEqual(JsonToken.Boolean, token.GetValueOrDefault());
            Assert.AreEqual(JsonToken.Boolean, token.GetValueOrDefault(JsonToken.EndArray));
            
            Assert.IsFalse(token == null);
            Assert.IsTrue(token != null);
            Assert.IsTrue(token.HasValue);
            

            token = null;

            Assert.AreEqual(0, (int)token.GetValueOrDefault());
            Assert.AreEqual(JsonToken.Bytes, token.GetValueOrDefault(JsonToken.Bytes));
            Assert.IsFalse(token.HasValue);
            Assert.AreEqual(token, null);
            Assert.IsTrue(token == null);
            Assert.IsFalse(token != null);


            try
            {
                Console.WriteLine(token.GetType().ToString());
                Assert.Fail("should throw");
            }
            catch (NullReferenceException)
            {
            }
            try
            {
                Console.WriteLine(token.Value.ToString());
                Assert.Fail("should throw");
            }
            catch (InvalidOperationException)
            {
            }
        }
Ejemplo n.º 26
0
 /// <summary>
 ///     Reads to the next token in the json document, and updates the context
 ///     accordingly.
 /// </summary>
 /// <returns>
 ///     True if a token was read, false if there are no more tokens to read.
 /// </returns>
 public override bool Read()
 {
     if (wasPeeked)
     {
         wasPeeked = false;
         return currentToken == null;
     }
     
     bool result = jsonReader.Read();
     if (result)
     {
         currentToken = jsonReader.Token;
         UpdateContext();
     }
     else
     {
         currentToken = null;
     }
     wasPeeked = false;
     return result;
 }
Ejemplo n.º 27
0
        /// <summary>
        ///     Reads to the next token in the json document, and updates the context
        ///     accordingly.
        /// </summary>
        /// <returns>
        ///     True if a token was read, false if there are no more tokens to read./
        /// </returns>
        public override bool Read()
        {
            bool result = jsonReader.Read();

            if (result)
            {
                currentToken = jsonReader.Token;
                UpdateContext();
            }
            return result;
        }
Ejemplo n.º 28
0
 JsonToken GetValueOrDefault(ref JsonToken?val)
 {
     return(val.GetValueOrDefault());
 }
Ejemplo n.º 29
0
 void SetToDefault(ref JsonToken?val)
 {
     val = JsonToken.Default;
 }
Ejemplo n.º 30
0
 void SetToNull(ref JsonToken?val)
 {
     val = null;
 }
Ejemplo n.º 31
0
        public void testNullableEnum()
        {
            Console.WriteLine(typeof(JsonToken?).ToString());
            Console.WriteLine(Nullable.GetUnderlyingType(typeof(JsonToken?)).ToString());

            Assert.AreEqual(0, (int)token.GetValueOrDefault());
            Assert.AreEqual(JsonToken.EndArray, token.GetValueOrDefault(JsonToken.EndArray));
            Assert.IsFalse(token.HasValue);
            Assert.IsTrue(token == null);
            Assert.IsFalse(token != null);

            try
            {
                Console.WriteLine(token.GetType().ToString());
                Assert.Fail("should throw");
            }
            catch (NullReferenceException)
            {
            }

            try
            {
                Console.WriteLine(token.Value.ToString());
                Assert.Fail("should throw");
            }
            catch (InvalidOperationException)
            {
            }

            token = JsonToken.Boolean;

            Assert.AreEqual(typeof(JsonToken), token.GetType());
            Assert.IsNull(Nullable.GetUnderlyingType(token.GetType()));
            Assert.AreEqual(token, JsonToken.Boolean);
            Assert.AreEqual(JsonToken.Boolean, token.GetValueOrDefault());
            Assert.AreEqual(JsonToken.Boolean, token.GetValueOrDefault(JsonToken.EndArray));

            Assert.IsFalse(token == null);
            Assert.IsTrue(token != null);
            Assert.IsTrue(token.HasValue);


            token = null;

            Assert.AreEqual(0, (int)token.GetValueOrDefault());
            Assert.AreEqual(JsonToken.Bytes, token.GetValueOrDefault(JsonToken.Bytes));
            Assert.IsFalse(token.HasValue);
            Assert.AreEqual(token, null);
            Assert.IsTrue(token == null);
            Assert.IsFalse(token != null);


            try
            {
                Console.WriteLine(token.GetType().ToString());
                Assert.Fail("should throw");
            }
            catch (NullReferenceException)
            {
            }
            try
            {
                Console.WriteLine(token.Value.ToString());
                Assert.Fail("should throw");
            }
            catch (InvalidOperationException)
            {
            }
        }
Ejemplo n.º 32
0
        private JsonToken ReadString(JsonToken?expToken)
        {
            try
            {
                var currChar = GetNextCharSkipWhitespace();
                if (currChar != JsonLiterals.QuotationMark)
                {
                    return(ExceptionHelper.ThrowInvalidJsonException <JsonToken>());
                }
                _expToken = expToken;

                while (true)
                {
                    currChar = GetNextChar();
                    if (currChar == JsonLiterals.ReverseSolidus)
                    {
                        currChar = GetNextChar();
                        switch (currChar)
                        {
                        case JsonLiterals.QuotationMark:
                            _intermediateValue.Append(JsonLiterals.QuotationMark);
                            break;

                        case JsonLiterals.ReverseSolidus:
                            _intermediateValue.Append(JsonLiterals.ReverseSolidus);
                            break;

                        case JsonLiterals.Solidus:
                            _intermediateValue.Append(JsonLiterals.Solidus);
                            break;

                        case 'b':
                            _intermediateValue.Append(JsonLiterals.Backspace);
                            break;

                        case 'f':
                            _intermediateValue.Append(JsonLiterals.Formfeed);
                            break;

                        case 'n':
                            _intermediateValue.Append(JsonLiterals.Newline);
                            break;

                        case 'r':
                            _intermediateValue.Append(JsonLiterals.CarriageReturn);
                            break;

                        case 't':
                            _intermediateValue.Append(JsonLiterals.HorizontalTab);
                            break;

                        case 'u':
                            string tmp = "";
                            for (int i = 0; i < 4; i++)
                            {
                                tmp += GetNextChar();
                            }
                            _intermediateValue.Append(UnicodeEscapedStringToChar(tmp));
                            break;

                        default:
                            return(ExceptionHelper.ThrowInvalidJsonException <JsonToken>());
                        }
                    }
                    else if (currChar == JsonLiterals.QuotationMark)
                    {
                        break;
                    }
                    else
                    {
                        _intermediateValue.Append(currChar);
                    }
                }
                _currValue = _intermediateValue.ToString();
                return(JsonToken.String);
            }
            finally
            {
                _intermediateValue.Clear();
            }
        }
Ejemplo n.º 33
0
 JsonToken GetValue(ref JsonToken?val)
 {
     return(val.Value);
 }