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}" ); } }
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); }
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); } }
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); }
public void testToString() { JsonToken?t = null; Assert.AreEqual(string.Empty, t.ToString()); t = JsonToken.EndArray; Assert.AreNotEqual(string.Empty, t.ToString()); }
/// <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()); }
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)); }
// 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)); }
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)); }
private bool SetEnd(JContainer c) { JsonToken?endToken = GetEndToken(c); if (endToken.HasValue) { SetToken(endToken.Value); _current = c; _parent = c; return(true); } return(ReadOver(c)); }
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); }
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>()); } }
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); } }
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)); } }
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)); } }
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()); }
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()); }
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); }
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)); }
/// <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); }
/// <summary> /// Advances to next token position. /// </summary> public void ConsumeToken() { Debug.Assert(_lookAhead.HasValue); _lookAhead = null; }
bool HasValue(ref JsonToken?val) { return(val.HasValue); }
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) { } }
/// <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; }
/// <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; }
JsonToken GetValueOrDefault(ref JsonToken?val) { return(val.GetValueOrDefault()); }
void SetToDefault(ref JsonToken?val) { val = JsonToken.Default; }
void SetToNull(ref JsonToken?val) { val = null; }
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) { } }
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(); } }
JsonToken GetValue(ref JsonToken?val) { return(val.Value); }