public void CustomParseJson(string jsonString) { byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString); JsonObject obj = JsonObject.Parse(dataUtf8); 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); }
public static void TestJsonReaderUtf8(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); byte[] result = JsonLabReturnBytesHelper(dataUtf8, out int length); string actualStr = Encoding.UTF8.GetString(result.AsSpan(0, length)); byte[] resultSequence = JsonLabSequenceReturnBytesHelper(dataUtf8, out length); string actualStrSequence = Encoding.UTF8.GetString(resultSequence.AsSpan(0, length)); Stream stream = new MemoryStream(dataUtf8); TextReader reader = new StreamReader(stream, Encoding.UTF8, false, 1024, true); string expectedStr = JsonTestHelper.NewtonsoftReturnStringHelper(reader); Assert.Equal(expectedStr, actualStr); Assert.Equal(expectedStr, actualStrSequence); long memoryBefore = GC.GetAllocatedBytesForCurrentThread(); JsonLabEmptyLoopHelper(dataUtf8); long memoryAfter = GC.GetAllocatedBytesForCurrentThread(); Assert.Equal(0, memoryAfter - memoryBefore); }
public static void TestJsonReaderUtf8(TestCaseType type, string jsonString) { byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString); byte[] result = JsonLabReturnBytesHelper(dataUtf8, out int length); string actualStr = Encoding.UTF8.GetString(result.AsSpan(0, length)); byte[] resultSequence = JsonLabSequenceReturnBytesHelper(dataUtf8, out length); string actualStrSequence = Encoding.UTF8.GetString(resultSequence.AsSpan(0, length)); Stream stream = new MemoryStream(dataUtf8); TextReader reader = new StreamReader(stream, Encoding.UTF8, false, 1024, true); string expectedStr = JsonTestHelper.NewtonsoftReturnStringHelper(reader); Assert.Equal(expectedStr, actualStr); Assert.Equal(expectedStr, actualStrSequence); long memoryBefore = GC.GetAllocatedBytesForCurrentThread(); JsonLabEmptyLoopHelper(dataUtf8); long memoryAfter = GC.GetAllocatedBytesForCurrentThread(); Assert.Equal(0, memoryAfter - memoryBefore); }
private static string ChangeEntryPointLibraryNameExpected() { JToken deps = JObject.Parse(TestJson.DepsJsonSignalR); foreach (JProperty target in deps["targets"]) { JProperty targetLibrary = target.Value.Children <JProperty>().FirstOrDefault(); if (targetLibrary == null) { continue; } targetLibrary.Remove(); } JProperty library = deps["libraries"].Children <JProperty>().First(); library.Remove(); string json = deps.ToString(Newtonsoft.Json.Formatting.None); TextReader reader = new StringReader(json); return(JsonTestHelper.NewtonsoftReturnStringHelper(reader)); }
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(); }
public void MultiSegmentSequenceReaderSequence() { string jsonString = TestJson.Json400KB; byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString); ReadOnlySequence <byte> sequenceMultiple = JsonTestHelper.GetSequence(dataUtf8, 4_000); var reader = new Utf8JsonReaderSequence(sequenceMultiple); byte[] outputArray = new byte[dataUtf8.Length]; Span <byte> destination = outputArray; while (reader.Read()) { JsonTokenType tokenType = reader.TokenType; ReadOnlySpan <byte> valueSpan = reader.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; } } reader.Dispose(); string actualStr = Encoding.UTF8.GetString(outputArray.AsSpan(0, outputArray.Length - destination.Length)); Stream stream = new MemoryStream(dataUtf8); TextReader textReader = new StreamReader(stream, Encoding.UTF8, false, 1024, true); string expectedStr = JsonTestHelper.NewtonsoftReturnStringHelper(textReader); Assert.Equal(expectedStr, actualStr); }