public void Setup() { string jsonString = JsonStrings.ResourceManager.GetString(TestCase.ToString()); // Remove all formatting/indendation if (IsDataCompact) { using (var jsonReader = new JsonTextReader(new StringReader(jsonString))) { JToken obj = JToken.ReadFrom(jsonReader); var stringWriter = new StringWriter(); using (var jsonWriter = new JsonTextWriter(stringWriter)) { obj.WriteTo(jsonWriter); jsonString = stringWriter.ToString(); } } } _dataUtf8 = Encoding.UTF8.GetBytes(jsonString); _stream = new MemoryStream(_dataUtf8); _reader = new StreamReader(_stream, Encoding.UTF8, false, 1024, true); _output = new byte[_dataUtf8.Length]; _outputStream = new MemoryStream(_output); _writer = new StreamWriter(_outputStream, new UTF8Encoding(), 1024, true); // Do not output the BOM _arrayOutput = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); }
private static void WriterSystemTextJsonArrayOnlyUtf8(bool formatted, ArrayFormatterWrapper output, ReadOnlySpan <int> data) { Utf8JsonWriter <ArrayFormatterWrapper> json = Utf8JsonWriter.Create(output, formatted); json.WriteArrayUtf8(ExtraArray, data); json.Flush(); }
public static void TestDepthBeyondLimit(int depth) { var output = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(output); WriteDepth(ref jsonUtf8, depth - 1); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Span <byte> data = formatted.Array.AsSpan(formatted.Offset, formatted.Count); var json = new Utf8JsonReader(data) { MaxDepth = depth - 1 }; try { int maxDepth = 0; while (json.Read()) { if (maxDepth < json.Depth) { maxDepth = json.Depth; } } Assert.True(false, $"Expected JsonReaderException was not thrown. Max depth allowed = {json.MaxDepth} | Max depth reached = {maxDepth}"); } catch (JsonReaderException) { } }
public void WriteBasicJsonUtf8(bool prettyPrint) { int[] data = GetData(ExtraArraySize, 42, -10000, 10000); byte[] ExtraArray = Encoding.UTF8.GetBytes("ExtraArray"); string expectedStr = GetExpectedString(prettyPrint, isUtf8: true, data); var output = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(output, prettyPrint); jsonUtf8.WriteObjectStart(); jsonUtf8.WriteAttribute("age", 42); jsonUtf8.WriteAttribute("first", null); jsonUtf8.WriteAttribute("last", "Smith"); jsonUtf8.WriteArrayStart("phoneNumbers"); jsonUtf8.WriteValue("425-000-1212"); jsonUtf8.WriteValue("425-000-1213"); jsonUtf8.WriteArrayEnd(); jsonUtf8.WriteObjectStart("address"); jsonUtf8.WriteAttribute("street", "1 Microsoft Way"); jsonUtf8.WriteAttribute("city", "Redmond"); jsonUtf8.WriteAttribute("zip", 98052); jsonUtf8.WriteObjectEnd(); // Add a large array of values jsonUtf8.WriteArrayUtf8(ExtraArray, data); jsonUtf8.WriteObjectEnd(); jsonUtf8.Flush(); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expectedStr, actualStr); }
private static void WriterSystemTextJsonBasicUtf8(bool formatted, ArrayFormatterWrapper output, ReadOnlySpan <int> data) { Utf8JsonWriter <ArrayFormatterWrapper> json = Utf8JsonWriter.Create(output, formatted); json.WriteObjectStart(); json.WriteAttribute("age", 42); json.WriteAttribute("first", "John"); json.WriteAttribute("last", "Smith"); json.WriteArrayStart("phoneNumbers"); json.WriteValue("425-000-1212"); json.WriteValue("425-000-1213"); json.WriteArrayEnd(); json.WriteObjectStart("address"); json.WriteAttribute("street", "1 Microsoft Way"); json.WriteAttribute("city", "Redmond"); json.WriteAttribute("zip", 98052); json.WriteObjectEnd(); json.WriteArrayStart("ExtraArray"); for (var i = 0; i < data.Length; i++) { json.WriteValue(data[i]); } json.WriteArrayEnd(); json.WriteObjectEnd(); json.Flush(); }
private static void WriterSystemTextJsonHelloWorldUtf8(bool formatted, ArrayFormatterWrapper output) { Utf8JsonWriter <ArrayFormatterWrapper> json = Utf8JsonWriter.Create(output, formatted); json.WriteObjectStart(); json.WriteAttribute("message", "Hello, World!"); json.WriteObjectEnd(); json.Flush(); }
private static void WriterSystemTextJsonHelloWorldUtf8(bool formatted, ArrayFormatterWrapper output) { var json = new Utf8JsonWriter <ArrayFormatterWrapper>(output, formatted); json.WriteObjectStart(); json.WriteAttributeUtf8(Message, HelloWorld); json.WriteObjectEnd(); json.Flush(); }
private static void WriterSystemTextJsonArrayOnlyUtf8(bool formatted, ArrayFormatterWrapper output, ReadOnlySpan <int> data) { Utf8JsonWriter <ArrayFormatterWrapper> json = Utf8JsonWriter.Create(output, formatted); json.WriteArrayStart("ExtraArray"); for (var i = 0; i < data.Length; i++) { json.WriteValue(data[i]); } json.WriteArrayEnd(); json.Flush(); }
public void Setup() { string jsonString = JsonStrings.ResourceManager.GetString(TestCase.ToString()); _dataUtf8 = Encoding.UTF8.GetBytes(jsonString); _stream = new MemoryStream(_dataUtf8); _reader = new StreamReader(_stream, Encoding.UTF8, false, 1024, true); _output = new byte[_dataUtf8.Length]; _outputStream = new MemoryStream(_output); _writer = new StreamWriter(_outputStream, Encoding.UTF8, 1024, true); _arrayOutput = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); }
public void Setup() { var output = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(output); WriteDepth(ref jsonUtf8, Depth - 1); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); _dataUtf8 = Encoding.UTF8.GetBytes(actualStr); _stream = new MemoryStream(_dataUtf8); _reader = new StreamReader(_stream, Encoding.UTF8, false, 1024, true); }
public static void TestDepth(int depth) { for (int i = 0; i < depth; i++) { var output = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(output); WriteDepth(ref jsonUtf8, i); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Span <byte> data = formatted.Array.AsSpan(formatted.Offset, formatted.Count); var json = new Utf8JsonReader(data) { MaxDepth = depth }; int actualDepth = 0; while (json.Read()) { if (json.TokenType == JsonTokenType.Value) { actualDepth = json.Depth; } } Stream stream = new MemoryStream(data.ToArray()); TextReader reader = new StreamReader(stream, Encoding.UTF8, false, 1024, true); int expectedDepth = 0; var sb = new StringBuilder(); var newtonJson = new JsonTextReader(reader) { MaxDepth = depth }; while (newtonJson.Read()) { if (newtonJson.TokenType == JsonToken.String) { expectedDepth = newtonJson.Depth; } } Assert.Equal(expectedDepth, actualDepth); Assert.Equal(i + 1, actualDepth); } }
public void WriteHelloWorldJsonUtf8(bool prettyPrint) { string expectedStr = GetHelloWorldExpectedString(prettyPrint, isUtf8: true); var output = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(output, prettyPrint); jsonUtf8.WriteObjectStart(); jsonUtf8.WriteAttribute("message", "Hello, World!"); jsonUtf8.WriteObjectEnd(); jsonUtf8.Flush(); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expectedStr, actualStr); }
public void Setup() { _data = new int[ExtraArraySize]; Random rand = new Random(42); for (int i = 0; i < ExtraArraySize; i++) { _data[i] = rand.Next(-10000, 10000); } var buffer = new byte[BufferSize]; _memoryStream = new MemoryStream(buffer); _streamWriter = new StreamWriter(_memoryStream, new UTF8Encoding(false), BufferSize, true); _arrayFormatterWrapper = new ArrayFormatterWrapper(10000, SymbolTable.InvariantUtf8); _arrayFormatter = new ArrayFormatter(BufferSize, SymbolTable.InvariantUtf8); // To pass an initialBuffer to Utf8Json: // _output = new byte[BufferSize]; _output = null; var random = new Random(42); const int numberOfItems = 10; _longs = new long[numberOfItems]; _longs[0] = 0; _longs[1] = long.MaxValue; _longs[2] = long.MinValue; _longs[3] = 12345678901; _longs[4] = -12345678901; for (int i = 5; i < numberOfItems; i++) { long value = random.Next(int.MinValue, int.MaxValue); value += value < 0 ? int.MinValue : int.MaxValue; _longs[i] = value; } dataArray = new int[100]; for (int i = 0; i < 100; i++) { dataArray[i] = 12345; } MyDate = DateTime.Now; }
public void WriteJsonUtf8() { var formatter = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); var json = new Utf8JsonWriter <ArrayFormatterWrapper>(formatter, prettyPrint: false); Write(ref json); var formatted = formatter.Formatted; var str = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expected, str.Replace(" ", "")); formatter.Clear(); json = new Utf8JsonWriter <ArrayFormatterWrapper>(formatter, prettyPrint: true); Write(ref json); formatted = formatter.Formatted; str = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expected, str.Replace("\r\n", "").Replace("\n", "").Replace(" ", "")); }
public void Setup() { _data = new int[ExtraArraySize]; Random rand = new Random(42); for (int i = 0; i < ExtraArraySize; i++) { _data[i] = rand.Next(-10000, 10000); } var buffer = new byte[BufferSize]; _memoryStream = new MemoryStream(buffer); _streamWriter = new StreamWriter(_memoryStream, new UTF8Encoding(false), BufferSize, true); _arrayFormatterWrapper = new ArrayFormatterWrapper(BufferSize, SymbolTable.InvariantUtf8); // To pass an initialBuffer to Utf8Json: // _output = new byte[BufferSize]; _output = null; }
public void ChangeEntryPointLibraryName() { string depsJson = TestJson.DepsJsonSignalR; byte[] dataUtf8 = Encoding.UTF8.GetBytes(depsJson); JsonObject obj = JsonObject.Parse(dataUtf8); var targetsString = new Utf8Span("targets"); var librariesString = new Utf8Span("libraries"); JsonObject targets = obj[targetsString]; foreach (JsonObject target in targets) { Assert.True(target.TryGetChild(out JsonObject firstChild)); obj.Remove(firstChild); } JsonObject libraries = obj[librariesString]; Assert.True(libraries.TryGetChild(out JsonObject child)); obj.Remove(child); string expected = ChangeEntryPointLibraryNameExpected(); var output = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(output); jsonUtf8.Write(obj); jsonUtf8.Flush(); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(expected, actualStr); }
private static void WriterSystemJsonBasic(bool formatted, bool skipValidation, ArrayFormatterWrapper output, ReadOnlySpan <int> data) { var state = new JsonWriterState(options: new JsonWriterOptions { Indented = formatted, SkipValidation = skipValidation }); var json = new Utf8JsonWriter2(output, state); json.WriteStartObject(); json.WriteNumber("age", 42); json.WriteString("first", "John"); json.WriteString("last", "Smith"); json.WriteStartArray("phoneNumbers"); json.WriteStringValue("425-000-1212"); json.WriteStringValue("425-000-1213"); json.WriteEndArray(); json.WriteStartObject("address"); json.WriteString("street", "1 Microsoft Way"); json.WriteString("city", "Redmond"); json.WriteNumber("zip", 98052); json.WriteEndObject(); json.WriteStartArray("ExtraArray"); for (var i = 0; i < data.Length; i++) { json.WriteNumberValue(data[i]); } json.WriteEndArray(); json.WriteEndObject(); json.Flush(); }
public void ParseJson(bool compactData, TestCaseType type, string jsonString) { // Remove all formatting/indendation if (compactData) { using (JsonTextReader jsonReader = new JsonTextReader(new StringReader(jsonString))) { jsonReader.FloatParseHandling = FloatParseHandling.Decimal; JToken jtoken = JToken.ReadFrom(jsonReader); var stringWriter = new StringWriter(); using (JsonTextWriter jsonWriter = new JsonTextWriter(stringWriter)) { jtoken.WriteTo(jsonWriter); jsonString = stringWriter.ToString(); } } } byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString); JsonObject obj = JsonObject.Parse(dataUtf8); var stream = new MemoryStream(dataUtf8); var streamReader = new StreamReader(stream, Encoding.UTF8, false, 1024, true); using (JsonTextReader jsonReader = new JsonTextReader(streamReader)) { JToken jtoken = JToken.ReadFrom(jsonReader); string expectedString = ""; string actualString = ""; if (type == TestCaseType.Json400KB) { expectedString = ReadJson400KB(jtoken); actualString = ReadJson400KB(obj); } else if (type == TestCaseType.HelloWorld) { expectedString = ReadHelloWorld(jtoken); actualString = ReadHelloWorld(obj); } Assert.Equal(expectedString, actualString); } string actual = obj.PrintJson(); // Change casing to match what JSON.NET does. actual = actual.Replace("true", "True").Replace("false", "False"); TextReader reader = new StringReader(jsonString); string expected = JsonTestHelper.NewtonsoftReturnStringHelper(reader); Assert.Equal(expected, actual); if (compactData) { var output = new ArrayFormatterWrapper(1024, SymbolTable.InvariantUtf8); var jsonUtf8 = new Utf8JsonWriter <ArrayFormatterWrapper>(output); jsonUtf8.Write(obj); jsonUtf8.Flush(); ArraySegment <byte> formatted = output.Formatted; string actualStr = Encoding.UTF8.GetString(formatted.Array, formatted.Offset, formatted.Count); Assert.Equal(jsonString, actualStr); } obj.Dispose(); }