}
[Fact]
+ public static void InitialStateSimpleCtorMultiSegment()
+ {
+ byte[] utf8 = Encoding.UTF8.GetBytes("1");
+ ReadOnlySequence<byte> sequence = JsonTestHelper.GetSequence(utf8, 1);
+ var json = new Utf8JsonReader(sequence);
+
+ Assert.Equal(0, json.BytesConsumed);
+ Assert.Equal(0, json.TokenStartIndex);
+ Assert.Equal(0, json.CurrentDepth);
+ Assert.Equal(JsonTokenType.None, json.TokenType);
+ Assert.NotEqual(default, json.Position);
+ Assert.False(json.HasValueSequence);
+ Assert.True(json.ValueSpan.SequenceEqual(default));
+ Assert.True(json.ValueSequence.IsEmpty);
+
+ Assert.Equal(64, json.CurrentState.Options.MaxDepth);
+ Assert.False(json.CurrentState.Options.AllowTrailingCommas);
+ Assert.Equal(JsonCommentHandling.Disallow, json.CurrentState.Options.CommentHandling);
+
+ Assert.True(json.Read());
+ Assert.False(json.Read());
+ }
+
+ [Fact]
public static void StateRecoveryMultiSegment()
{
byte[] utf8 = Encoding.UTF8.GetBytes("[1]");
Assert.True(sequence.Slice(json.Position).IsEmpty);
}
+
+ for (int i = 0; i < sequences.Count; i++)
+ {
+ ReadOnlySequence<byte> sequence = sequences[i];
+ var json = new Utf8JsonReader(sequence);
+ while (json.Read())
+ ;
+ Assert.Equal(sequence.Length, json.BytesConsumed);
+
+ Assert.True(sequence.Slice(json.Position).IsEmpty);
+ }
}
[Theory]
Assert.True(json.ValueSpan == default);
Assert.True(json.ValueSequence.IsEmpty);
}
+
+ foreach (JsonCommentHandling commentHandling in Enum.GetValues(typeof(JsonCommentHandling)))
+ {
+ var json = new Utf8JsonReader(sequence, new JsonReaderOptions { CommentHandling = commentHandling });
+
+ Assert.False(json.HasValueSequence);
+ Assert.True(json.ValueSpan == default);
+ Assert.True(json.ValueSequence.IsEmpty);
+
+ Assert.True(json.Read());
+ Assert.True(json.HasValueSequence);
+ Assert.True(json.ValueSpan == default);
+ Assert.False(json.ValueSequence.IsEmpty);
+
+ // Subsequent calls to Read clears the value properties since Read returned false.
+ Assert.False(json.Read());
+ Assert.False(json.HasValueSequence);
+ Assert.True(json.ValueSpan == default);
+ Assert.True(json.ValueSequence.IsEmpty);
+ }
}
[Theory]
Assert.True(reader.Read());
Assert.Equal(expectedIndex, reader.TokenStartIndex);
}
+
+ foreach (JsonCommentHandling commentHandling in Enum.GetValues(typeof(JsonCommentHandling)))
+ {
+ var reader = new Utf8JsonReader(sequence, new JsonReaderOptions { CommentHandling = commentHandling, AllowTrailingCommas = false });
+ Assert.True(reader.Read());
+ Assert.Equal(expectedIndex, reader.TokenStartIndex);
+
+ reader = new Utf8JsonReader(sequence, new JsonReaderOptions { CommentHandling = commentHandling, AllowTrailingCommas = true });
+ Assert.True(reader.Read());
+ Assert.Equal(expectedIndex, reader.TokenStartIndex);
+ }
}
[Theory]
}
[Fact]
+ public static void InitialStateSimpleCtor()
+ {
+ var json = new Utf8JsonReader(Encoding.UTF8.GetBytes("1"));
+
+ Assert.Equal(0, json.BytesConsumed);
+ Assert.Equal(0, json.TokenStartIndex);
+ Assert.Equal(0, json.CurrentDepth);
+ Assert.Equal(JsonTokenType.None, json.TokenType);
+ Assert.Equal(default, json.Position);
+ Assert.False(json.HasValueSequence);
+ Assert.True(json.IsFinalBlock);
+ Assert.True(json.ValueSpan.SequenceEqual(default));
+ Assert.True(json.ValueSequence.IsEmpty);
+
+ Assert.Equal(64, json.CurrentState.Options.MaxDepth);
+ Assert.False(json.CurrentState.Options.AllowTrailingCommas);
+ Assert.Equal(JsonCommentHandling.Disallow, json.CurrentState.Options.CommentHandling);
+
+ Assert.True(json.Read());
+ Assert.False(json.Read());
+ }
+
+ [Fact]
public static void StateRecovery()
{
byte[] utf8 = Encoding.UTF8.GetBytes("[1]");
]
]";
- byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
- var json = new Utf8JsonReader(dataUtf8, isFinalBlock: true, state: default);
+ {
+ byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
+ var json = new Utf8JsonReader(dataUtf8, isFinalBlock: true, state: default);
- Assert.Equal(0, json.CurrentDepth);
- Assert.True(json.Read());
- Assert.Equal(0, json.CurrentDepth);
- Assert.True(json.Read());
- Assert.Equal(1, json.CurrentDepth);
- Assert.True(json.Read());
- Assert.Equal(2, json.CurrentDepth);
- Assert.True(json.Read());
- Assert.Equal(2, json.CurrentDepth);
- Assert.True(json.Read());
- Assert.Equal(2, json.CurrentDepth);
- Assert.True(json.Read());
- Assert.Equal(1, json.CurrentDepth);
- Assert.True(json.Read());
- Assert.Equal(0, json.CurrentDepth);
- Assert.False(json.Read());
- Assert.Equal(0, json.CurrentDepth);
+ Assert.Equal(0, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(0, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(1, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(2, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(2, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(2, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(1, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(0, json.CurrentDepth);
+ Assert.False(json.Read());
+ Assert.Equal(0, json.CurrentDepth);
+ }
+
+ {
+ byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);
+ var json = new Utf8JsonReader(dataUtf8);
+
+ Assert.Equal(0, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(0, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(1, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(2, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(2, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(2, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(1, json.CurrentDepth);
+ Assert.True(json.Read());
+ Assert.Equal(0, json.CurrentDepth);
+ Assert.False(json.Read());
+ Assert.Equal(0, json.CurrentDepth);
+ }
}
[Fact]
Assert.True(reader.TrySkip());
Assert.Equal(expectedConsumed, reader.BytesConsumed);
}
+
+ {
+ var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(jsonString));
+ for (int i = 0; i < readCount; i++)
+ {
+ reader.Read();
+ }
+
+ reader.Skip();
+ Assert.Equal(expectedConsumed, reader.BytesConsumed);
+ }
+ {
+ var reader = new Utf8JsonReader(Encoding.UTF8.GetBytes(jsonString));
+ for (int i = 0; i < readCount; i++)
+ {
+ reader.Read();
+ }
+
+ Assert.True(reader.TrySkip());
+ Assert.Equal(expectedConsumed, reader.BytesConsumed);
+ }
}
[Theory]
while (json.Read())
{
if (json.TokenType >= JsonTokenType.String && json.TokenType <= JsonTokenType.Null)
+ {
+ actualDepth = json.CurrentDepth;
+ }
+ }
+
+ int expectedDepth = 0;
+ var newtonJson = new JsonTextReader(new StringReader(jsonStr))
+ {
+ MaxDepth = depth
+ };
+ while (newtonJson.Read())
+ {
+ if (newtonJson.TokenType == JsonToken.String)
+ {
+ expectedDepth = newtonJson.Depth;
+ }
+ }
+
+ Assert.Equal(expectedDepth, actualDepth);
+ Assert.Equal(i + 1, actualDepth);
+ }
+ }
+
+ foreach (JsonCommentHandling commentHandling in Enum.GetValues(typeof(JsonCommentHandling)))
+ {
+ for (int i = 0; i < depth; i++)
+ {
+ string jsonStr = JsonTestHelper.WriteDepthObject(i, commentHandling == JsonCommentHandling.Allow);
+ Span<byte> data = Encoding.UTF8.GetBytes(jsonStr);
+
+ var json = new Utf8JsonReader(data, new JsonReaderOptions { CommentHandling = commentHandling, MaxDepth = depth });
+
+ int actualDepth = 0;
+ while (json.Read())
+ {
+ if (json.TokenType >= JsonTokenType.String && json.TokenType <= JsonTokenType.Null)
+ {
actualDepth = json.CurrentDepth;
+ }
}
int expectedDepth = 0;
Assert.Equal(dataUtf8.Length - consumed, json.BytesConsumed);
Assert.Equal(default, json.Position);
}
+
+ foreach (JsonCommentHandling commentHandling in Enum.GetValues(typeof(JsonCommentHandling)))
+ {
+ if (jsonString == "12345")
+ {
+ continue;
+ }
+
+ var json = new Utf8JsonReader(dataUtf8.AsSpan(0, splitLocation), options: new JsonReaderOptions { CommentHandling = commentHandling });
+
+ try
+ {
+ while (json.Read())
+ ;
+ Assert.True(false, "Expected JsonException was not thrown.");
+ }
+ catch (JsonException) {}
+ }
}
[Theory]
Assert.Equal(expectedBytePosition, ex.BytePositionInLine);
}
}
+
+ foreach (JsonCommentHandling commentHandling in Enum.GetValues(typeof(JsonCommentHandling)))
+ {
+ var json = new Utf8JsonReader(dataUtf8, new JsonReaderOptions { CommentHandling = commentHandling, MaxDepth = maxDepth });
+
+ try
+ {
+ while (json.Read())
+ ;
+ Assert.True(false, "Expected JsonException was not thrown.");
+ }
+ catch (JsonException ex)
+ {
+ Assert.Equal(expectedlineNumber, ex.LineNumber);
+ Assert.Equal(expectedBytePosition, ex.BytePositionInLine);
+ }
+ }
}
[Theory]
Assert.Equal(default, json.Position);
}
}
+
+ foreach (JsonCommentHandling commentHandling in Enum.GetValues(typeof(JsonCommentHandling)))
+ {
+ var json = new Utf8JsonReader(dataUtf8, new JsonReaderOptions { CommentHandling = commentHandling, MaxDepth = maxDepth });
+
+ try
+ {
+ while (json.Read())
+ ;
+ Assert.True(false, "Expected JsonException was not thrown with single-segment data.");
+ }
+ catch (JsonException ex)
+ {
+ Assert.Equal(expectedlineNumber, ex.LineNumber);
+ Assert.Equal(expectedBytePosition, ex.BytePositionInLine);
+ Assert.Equal(default, json.Position);
+ }
+ }
}
[Theory]