in src/Elastic.Apm/Libraries/Newtonsoft.Json/JsonTextReader.cs [689:812]
private JsonReaderException CreateUnexpectedCharacterException(char c) => JsonReaderException.Create(this,
"Unexpected character encountered while parsing value: {0}.".FormatWith(CultureInfo.InvariantCulture, c));
/// <summary>
/// Reads the next JSON token from the underlying <see cref="TextReader" /> as a <see cref="Nullable{T}" /> of
/// <see cref="Boolean" />.
/// </summary>
/// <returns>
/// A <see cref="Nullable{T}" /> of <see cref="Boolean" />. This method will return <c>null</c> at the end of an
/// array.
/// </returns>
public override bool? ReadAsBoolean()
{
EnsureBuffer();
MiscellaneousUtils.Assert(CharBuffer != null);
switch (_currentState)
{
case State.PostValue:
if (ParsePostValue(true)) return null;
goto case State.Start;
case State.Start:
case State.Property:
case State.Array:
case State.ArrayStart:
case State.Constructor:
case State.ConstructorStart:
while (true)
{
var currentChar = CharBuffer[CharPos];
switch (currentChar)
{
case '\0':
if (ReadNullChar())
{
SetToken(JsonToken.None, null, false);
return null;
}
break;
case '"':
case '\'':
ParseString(currentChar, ReadType.Read);
return ReadBooleanString(_stringReference.ToString());
case 'n':
HandleNull();
return null;
case '-':
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
ParseNumber(ReadType.Read);
bool b;
#if HAVE_BIG_INTEGER
if (Value is BigInteger integer)
{
b = integer != 0;
}
else
#endif
{
b = Convert.ToBoolean(Value, CultureInfo.InvariantCulture);
}
SetToken(JsonToken.Boolean, b, false);
return b;
case 't':
case 'f':
var isTrue = currentChar == 't';
var expected = isTrue ? JsonConvert.True : JsonConvert.False;
if (!MatchValueWithTrailingSeparator(expected)) throw CreateUnexpectedCharacterException(CharBuffer[CharPos]);
SetToken(JsonToken.Boolean, isTrue);
return isTrue;
case '/':
ParseComment(false);
break;
case ',':
ProcessValueComma();
break;
case ']':
CharPos++;
if (_currentState == State.Array || _currentState == State.ArrayStart || _currentState == State.PostValue)
{
SetToken(JsonToken.EndArray);
return null;
}
throw CreateUnexpectedCharacterException(currentChar);
case StringUtils.CarriageReturn:
ProcessCarriageReturn(false);
break;
case StringUtils.LineFeed:
ProcessLineFeed();
break;
case ' ':
case StringUtils.Tab:
// eat
CharPos++;
break;
default:
CharPos++;
if (!char.IsWhiteSpace(currentChar)) throw CreateUnexpectedCharacterException(currentChar);
// eat
break;
}
}
case State.Finished:
ReadFinished();
return null;
default:
throw JsonReaderException.Create(this, "Unexpected state: {0}.".FormatWith(CultureInfo.InvariantCulture, CurrentState));
}
}