public void StreamTokenSizeOverflow() { int tokenSize = 1_500_000_000; // 1.5GB byte[] dataUtf8 = new byte[tokenSize]; System.Array.Fill <byte>(dataUtf8, 97); dataUtf8[0] = 34; dataUtf8[dataUtf8.Length - 1] = 34; var stream = new MemoryStream(dataUtf8); var json = new Utf8JsonReaderStream(stream); try { while (json.Read()) { ; } Assert.True(false, "Expected ArgumentException to be thrown since token size larger than 1 GB is not supported."); } catch (ArgumentException) { Assert.Equal(0, json.Consumed); } json.Dispose(); }
public static byte[] JsonLabReaderLoop(int inpuDataLength, out int length, ref Utf8JsonReaderStream json) { byte[] outputArray = new byte[inpuDataLength]; Span <byte> destination = outputArray; while (json.Read()) { JsonTokenType tokenType = json.TokenType; ReadOnlySpan <byte> valueSpan = json.Value; switch (tokenType) { case JsonTokenType.PropertyName: valueSpan.CopyTo(destination); destination[valueSpan.Length] = (byte)','; destination[valueSpan.Length + 1] = (byte)' '; destination = destination.Slice(valueSpan.Length + 2); break; case JsonTokenType.Number: case JsonTokenType.String: case JsonTokenType.Comment: valueSpan.CopyTo(destination); destination[valueSpan.Length] = (byte)','; destination[valueSpan.Length + 1] = (byte)' '; destination = destination.Slice(valueSpan.Length + 2); break; case JsonTokenType.True: // Special casing True/False so that the casing matches with Json.NET destination[0] = (byte)'T'; destination[1] = (byte)'r'; destination[2] = (byte)'u'; destination[3] = (byte)'e'; destination[valueSpan.Length] = (byte)','; destination[valueSpan.Length + 1] = (byte)' '; destination = destination.Slice(valueSpan.Length + 2); break; case JsonTokenType.False: destination[0] = (byte)'F'; destination[1] = (byte)'a'; destination[2] = (byte)'l'; destination[3] = (byte)'s'; destination[4] = (byte)'e'; destination[valueSpan.Length] = (byte)','; destination[valueSpan.Length + 1] = (byte)' '; destination = destination.Slice(valueSpan.Length + 2); break; case JsonTokenType.Null: // Special casing Null so that it matches what JSON.NET does break; default: break; } } length = outputArray.Length - destination.Length; return(outputArray); }
public static byte[] JsonLabStreamReturnBytesHelper(byte[] data, out int length) { Stream stream = new MemoryStream(data); var reader = new Utf8JsonReaderStream(stream); byte[] result = JsonLabReaderLoop(data.Length, out length, ref reader); reader.Dispose(); return(result); }
public void ReaderSystemTextJsonLabStreamTypeEmptyLoop() { _stream.Seek(0, SeekOrigin.Begin); var json = new Utf8JsonReaderStream(_stream); while (json.Read()) { ; } json.Dispose(); }
// [InlineData(1_000_000_000)] // Allocating 1 GB for a test is too high for inner loop (reserved for outerloop) public void StreamMaxTokenSize(int tokenSize) { byte[] dataUtf8 = new byte[tokenSize]; System.Array.Fill <byte>(dataUtf8, 97); dataUtf8[0] = 34; dataUtf8[dataUtf8.Length - 1] = 34; var stream = new MemoryStream(dataUtf8); var json = new Utf8JsonReaderStream(stream); while (json.Read()) { ; } Assert.Equal(dataUtf8.Length, json.Consumed); json.Dispose(); }