private static void ReadPartialSegmentSizeOne(bool compactData, TestCaseType type, string jsonString) { // Remove all formatting/indendation if (compactData) { jsonString = JsonTestHelper.GetCompactString(jsonString); } byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString); Stream stream = new MemoryStream(dataUtf8); TextReader reader = new StreamReader(stream, Encoding.UTF8, false, 1024, true); string expectedStr = JsonTestHelper.NewtonsoftReturnStringHelper(reader); ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(dataUtf8, 1); for (int j = 0; j < dataUtf8.Length; j++) { var utf8JsonReader = new Utf8JsonReader(sequence.Slice(0, j), isFinalBlock: false, default); byte[] resultSequence = JsonTestHelper.ReaderLoop(dataUtf8.Length, out int length, ref utf8JsonReader); string actualStrSequence = Encoding.UTF8.GetString(resultSequence, 0, length); long consumed = utf8JsonReader.BytesConsumed; Assert.Equal(consumed, utf8JsonReader.CurrentState.BytesConsumed); utf8JsonReader = new Utf8JsonReader(sequence.Slice(consumed), isFinalBlock: true, utf8JsonReader.CurrentState); resultSequence = JsonTestHelper.ReaderLoop(dataUtf8.Length, out length, ref utf8JsonReader); actualStrSequence += Encoding.UTF8.GetString(resultSequence, 0, length); string message = $"Expected consumed: {dataUtf8.Length - consumed}, Actual consumed: {utf8JsonReader.BytesConsumed}, Index: {j}"; Assert.Equal(utf8JsonReader.BytesConsumed, utf8JsonReader.CurrentState.BytesConsumed); Assert.True(dataUtf8.Length - consumed == utf8JsonReader.BytesConsumed, message); Assert.Equal(expectedStr, actualStrSequence); } }
public static void TestPartialJsonReaderMultiSegment(bool compactData, TestCaseType type, string jsonString) { // Remove all formatting/indendation if (compactData) { jsonString = JsonTestHelper.GetCompactString(jsonString); } byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString); ReadOnlyMemory <byte> dataMemory = dataUtf8; List <ReadOnlySequence <byte> > sequences = JsonTestHelper.GetSequences(dataMemory); for (int i = 0; i < sequences.Count; i++) { ReadOnlySequence <byte> sequence = sequences[i]; var json = new Utf8JsonReader(sequence, isFinalBlock: true, default); while (json.Read()) { ; } Assert.Equal(sequence.Length, json.BytesConsumed); Assert.Equal(sequence.Length, json.CurrentState.BytesConsumed); Assert.True(sequence.Slice(json.Position).IsEmpty); Assert.True(sequence.Slice(json.CurrentState.Position).IsEmpty); } }
public static void TestPartialJsonReaderSlicesMultiSegment(bool compactData, TestCaseType type, string jsonString) { // Remove all formatting/indendation if (compactData) { jsonString = JsonTestHelper.GetCompactString(jsonString); } byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString); ReadOnlyMemory <byte> dataMemory = dataUtf8; List <ReadOnlySequence <byte> > sequences = JsonTestHelper.GetSequences(dataMemory); for (int i = 0; i < sequences.Count; i++) { ReadOnlySequence <byte> sequence = sequences[i]; for (int j = 0; j < dataUtf8.Length; j++) { var json = new Utf8JsonReader(sequence.Slice(0, j), isFinalBlock: false, default); while (json.Read()) { ; } long consumed = json.BytesConsumed; JsonReaderState jsonState = json.CurrentState; byte[] consumedArray = sequence.Slice(0, consumed).ToArray(); Assert.Equal(consumedArray, sequence.Slice(0, json.Position).ToArray()); Assert.True(json.Position.Equals(jsonState.Position)); json = new Utf8JsonReader(sequence.Slice(consumed), isFinalBlock: true, jsonState); while (json.Read()) { ; } Assert.Equal(dataUtf8.Length - consumed, json.BytesConsumed); Assert.Equal(json.BytesConsumed, json.CurrentState.BytesConsumed); } } }
private static void ReadFullySegmentSizeOne(bool compactData, TestCaseType type, string jsonString) { // Remove all formatting/indendation if (compactData) { jsonString = JsonTestHelper.GetCompactString(jsonString); } byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString); Stream stream = new MemoryStream(dataUtf8); TextReader reader = new StreamReader(stream, Encoding.UTF8, false, 1024, true); string expectedStr = JsonTestHelper.NewtonsoftReturnStringHelper(reader); ReadOnlySequence <byte> sequence = JsonTestHelper.GetSequence(dataUtf8, 1); var utf8JsonReader = new Utf8JsonReader(sequence, isFinalBlock: true, default); byte[] resultSequence = JsonTestHelper.ReaderLoop(dataUtf8.Length, out int length, ref utf8JsonReader); string actualStrSequence = Encoding.UTF8.GetString(resultSequence, 0, length); Assert.Equal(expectedStr, actualStrSequence); }