_nextPosition = default;
_currentPosition = jsonData.Start;
_isLastSegment = isFinalBlock;
- _isSingleSegment = true;
+ _isMultiSegment = false;
}
else
{
}
_isLastSegment = !jsonData.TryGet(ref _nextPosition, out _, advance: true) && isFinalBlock; // Don't re-order to avoid short-circuiting
- _isSingleSegment = false;
+ _isMultiSegment = true;
}
}
ReadOnlyMemory<byte> memory = default;
while (true)
{
- Debug.Assert(_isSingleSegment || _currentPosition.GetObject() != null);
+ Debug.Assert(!_isMultiSegment || _currentPosition.GetObject() != null);
SequencePosition copy = _currentPosition;
_currentPosition = _nextPosition;
bool noMoreData = !_sequence.TryGet(ref _nextPosition, out memory, advance: true);
// _currentPosition needs to point to last non-empty segment
// Since memory.Length == 0, we need to revert back to previous.
_currentPosition = copy;
- Debug.Assert(_isSingleSegment || _currentPosition.GetObject() != null);
+ Debug.Assert(!_isMultiSegment || _currentPosition.GetObject() != null);
}
if (_isFinalBlock)
private long _totalConsumed;
private bool _isLastSegment;
internal bool _stringHasEscaping;
- private readonly bool _isSingleSegment;
+ private readonly bool _isMultiSegment;
private SequencePosition _nextPosition;
private SequencePosition _currentPosition;
private ReadOnlySequence<byte> _sequence;
- private bool IsLastSpan => _isFinalBlock && (_isSingleSegment || _isLastSegment);
+ private bool IsLastSpan => _isFinalBlock && (!_isMultiSegment || _isLastSegment);
/// <summary>
/// Gets the value of the last processed token as a ReadOnlySpan<byte> slice
_consumed = 0;
_totalConsumed = 0;
_isLastSegment = _isFinalBlock;
- _isSingleSegment = true;
+ _isMultiSegment = false;
ValueSpan = ReadOnlySpan<byte>.Empty;
/// </exception>
public bool Read()
{
- bool retVal = _isSingleSegment ? ReadSingleSegment() : ReadMultiSegment();
+ bool retVal = _isMultiSegment ? ReadMultiSegment() : ReadSingleSegment();
if (!retVal)
{
{
public static partial class Utf8JsonReaderTests
{
+ [Fact]
+ public static void DefaultUtf8JsonReader()
+ {
+ Utf8JsonReader json = default;
+
+ Assert.Equal(0, json.BytesConsumed);
+ Assert.Equal(0, json.CurrentDepth);
+ Assert.Equal(JsonTokenType.None, json.TokenType);
+ Assert.Equal(default, json.Position);
+ Assert.True(json.ValueSpan.SequenceEqual(default));
+ Assert.False(json.HasValueSequence);
+ Assert.True(json.ValueSequence.IsEmpty);
+
+ Assert.Equal(0, json.CurrentState.BytesConsumed);
+ Assert.Equal(default, json.CurrentState.Position);
+ Assert.Equal(0, json.CurrentState.Options.MaxDepth);
+ Assert.Equal(JsonCommentHandling.Disallow, json.CurrentState.Options.CommentHandling);
+
+ Assert.False(json.Read());
+
+ json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TextEquals("".AsSpan()));
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TextEquals(default(ReadOnlySpan<char>)));
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TextEquals(default(ReadOnlySpan<byte>)));
+
+ TestGetMethodsOnDefault();
+ }
+
+ private static void TestGetMethodsOnDefault()
+ {
+ Utf8JsonReader json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TryGetDateTime(out _));
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetDateTime());
+
+ json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TryGetDateTimeOffset(out _));
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetDateTimeOffset());
+
+ json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TryGetDecimal(out _));
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetDecimal());
+
+ json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TryGetDouble(out _));
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetDouble());
+
+ json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TryGetInt32(out _));
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetInt32());
+
+ json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TryGetInt64(out _));
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetInt64());
+
+ json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TryGetSingle(out _));
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetSingle());
+
+ json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TryGetUInt32(out _));
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetUInt32());
+
+ json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.TryGetUInt64(out _));
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetUInt64());
+
+ json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetString());
+
+ json = default;
+ JsonTestHelper.AssertThrows<InvalidOperationException>(json, (jsonReader) => jsonReader.GetBoolean());
+ }
+
// TestCaseType is only used to give the json strings a descriptive name.
[Theory]
[MemberData(nameof(TestCases))]