diff --git a/Src/Newtonsoft.Json.TestConsole/Program.cs b/Src/Newtonsoft.Json.TestConsole/Program.cs index 9df1c254..406f167d 100644 --- a/Src/Newtonsoft.Json.TestConsole/Program.cs +++ b/Src/Newtonsoft.Json.TestConsole/Program.cs @@ -53,7 +53,7 @@ namespace Newtonsoft.Json.TestConsole //t.DeserializeLargeJson(); //PerformanceTests t = new PerformanceTests(); - LargeArrayJTokenPathPerformance(); + SerializeJsonAsync(); //t.Iterations = 50000; //t.BenchmarkDeserializeMethod(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText); @@ -96,6 +96,20 @@ namespace Newtonsoft.Json.TestConsole } } + public static void SerializeJsonAsync() + { + PerformanceTests t = new PerformanceTests(); + t.Iterations = 50000; + t.SerializeAsync().Wait(); + } + + public static void DeserializeJsonAsync() + { + PerformanceTests t = new PerformanceTests(); + t.Iterations = 50000; + t.DeserializeAsync().Wait(); + } + public static void DeserializeJson() { PerformanceTests t = new PerformanceTests(); diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonReaderAsyncTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderAsyncTests.cs new file mode 100644 index 00000000..ba276c53 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Bson/BsonReaderAsyncTests.cs @@ -0,0 +1,1346 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Threading.Tasks; +using System.Text; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif +using Newtonsoft.Json.Bson; +using System.IO; +using Newtonsoft.Json.Linq; + +namespace Newtonsoft.Json.Tests.Bson +{ + [TestFixture] + public class BsonReaderAsyncTests : TestFixtureBase + { + private const char Euro = '\u20ac'; + + [Test] + public async Task ReadSingleObjectAsync() + { + byte[] data = HexToBytes("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00"); + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("Blah", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + Assert.AreEqual(1L, reader.Value); + Assert.AreEqual(typeof(long), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadGuid_TextAsync() + { + byte[] data = HexToBytes("31-00-00-00-02-30-00-25-00-00-00-64-38-32-31-65-65-64-37-2D-34-62-35-63-2D-34-33-63-39-2D-38-61-63-32-2D-36-39-32-38-65-35-37-39-62-37-30-35-00-00"); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + reader.ReadRootValueAsArray = true; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("d821eed7-4b5c-43c9-8ac2-6928e579b705", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + + ms = new MemoryStream(data); + reader = new BsonReader(ms); + reader.ReadRootValueAsArray = true; + + JsonSerializer serializer = new JsonSerializer(); + IList l = serializer.Deserialize>(reader); + + Assert.AreEqual(1, l.Count); + Assert.AreEqual(new Guid("D821EED7-4B5C-43C9-8AC2-6928E579B705"), l[0]); + } + + [Test] + public async Task ReadGuid_BytesAsync() + { + byte[] data = HexToBytes("1D-00-00-00-05-30-00-10-00-00-00-04-D7-EE-21-D8-5C-4B-C9-43-8A-C2-69-28-E5-79-B7-05-00"); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + reader.ReadRootValueAsArray = true; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Guid g = new Guid("D821EED7-4B5C-43C9-8AC2-6928E579B705"); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + Assert.AreEqual(g, reader.Value); + Assert.AreEqual(typeof(Guid), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + + ms = new MemoryStream(data); + reader = new BsonReader(ms); + reader.ReadRootValueAsArray = true; + + JsonSerializer serializer = new JsonSerializer(); + IList l = serializer.Deserialize>(reader); + + Assert.AreEqual(1, l.Count); + Assert.AreEqual(g, l[0]); + } + + [Test] + public async Task ReadDoubleAsync() + { + byte[] data = HexToBytes("10-00-00-00-01-30-00-8F-C2-F5-28-5C-FF-58-40-00"); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + reader.ReadRootValueAsArray = true; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(99.99d, reader.Value); + Assert.AreEqual(typeof(double), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadDouble_DecimalAsync() + { + byte[] data = HexToBytes("10-00-00-00-01-30-00-8F-C2-F5-28-5C-FF-58-40-00"); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + reader.FloatParseHandling = FloatParseHandling.Decimal; + reader.ReadRootValueAsArray = true; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(99.99m, reader.Value); + Assert.AreEqual(typeof(decimal), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadValuesAsync() + { + byte[] data = HexToBytes("8C-00-00-00-12-30-00-FF-FF-FF-FF-FF-FF-FF-7F-12-31-00-FF-FF-FF-FF-FF-FF-FF-7F-10-32-00-FF-FF-FF-7F-10-33-00-FF-FF-FF-7F-10-34-00-FF-00-00-00-10-35-00-7F-00-00-00-02-36-00-02-00-00-00-61-00-01-37-00-00-00-00-00-00-00-F0-45-01-38-00-FF-FF-FF-FF-FF-FF-EF-7F-01-39-00-00-00-00-E0-FF-FF-EF-47-08-31-30-00-01-05-31-31-00-05-00-00-00-02-00-01-02-03-04-09-31-32-00-40-C5-E2-BA-E3-00-00-00-09-31-33-00-40-C5-E2-BA-E3-00-00-00-00"); + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); +#pragma warning disable 612,618 + reader.JsonNet35BinaryCompatibility = true; +#pragma warning restore 612,618 + reader.ReadRootValueAsArray = true; + reader.DateTimeKindHandling = DateTimeKind.Utc; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + Assert.AreEqual(long.MaxValue, reader.Value); + Assert.AreEqual(typeof(long), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + Assert.AreEqual(long.MaxValue, reader.Value); + Assert.AreEqual(typeof(long), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + Assert.AreEqual((long)int.MaxValue, reader.Value); + Assert.AreEqual(typeof(long), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + Assert.AreEqual((long)int.MaxValue, reader.Value); + Assert.AreEqual(typeof(long), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + Assert.AreEqual((long)byte.MaxValue, reader.Value); + Assert.AreEqual(typeof(long), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + Assert.AreEqual((long)sbyte.MaxValue, reader.Value); + Assert.AreEqual(typeof(long), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("a", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual((double)decimal.MaxValue, reader.Value); + Assert.AreEqual(typeof(double), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(double.MaxValue, reader.Value); + Assert.AreEqual(typeof(double), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual((double)float.MaxValue, reader.Value); + Assert.AreEqual(typeof(double), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Boolean, reader.TokenType); + Assert.AreEqual(true, reader.Value); + Assert.AreEqual(typeof(bool), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + CollectionAssert.AreEquivalent(new byte[] { 0, 1, 2, 3, 4 }, (byte[])reader.Value); + Assert.AreEqual(typeof(byte[]), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(new DateTime(2000, 12, 29, 12, 30, 0, DateTimeKind.Utc), reader.Value); + Assert.AreEqual(typeof(DateTime), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(new DateTime(2000, 12, 29, 12, 30, 0, DateTimeKind.Utc), reader.Value); + Assert.AreEqual(typeof(DateTime), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadObjectBsonFromSiteAsync() + { + byte[] data = HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("0", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("a", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("1", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("b", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("2", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("c", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadArrayBsonFromSiteAsync() + { + byte[] data = HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.AreEqual(false, reader.ReadRootValueAsArray); + Assert.AreEqual(DateTimeKind.Local, reader.DateTimeKindHandling); + + reader.ReadRootValueAsArray = true; + reader.DateTimeKindHandling = DateTimeKind.Utc; + + Assert.AreEqual(true, reader.ReadRootValueAsArray); + Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("a", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("b", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("c", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadAsInt32BadStringAsync() + { + await ExceptionAssert.ThrowsAsync(async () => + { + byte[] data = HexToBytes("20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.AreEqual(false, reader.ReadRootValueAsArray); + Assert.AreEqual(DateTimeKind.Local, reader.DateTimeKindHandling); + + reader.ReadRootValueAsArray = true; + reader.DateTimeKindHandling = DateTimeKind.Utc; + + Assert.AreEqual(true, reader.ReadRootValueAsArray); + Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + await reader.ReadAsInt32Async(); + }, "Could not convert string to integer: a. Path '[0]'."); + } + + [Test] + public async Task ReadBytesAsync() + { + byte[] data = HexToBytes("2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-02-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00"); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms, true, DateTimeKind.Utc); +#pragma warning disable 612,618 + reader.JsonNet35BinaryCompatibility = true; +#pragma warning restore 612,618 + + Assert.AreEqual(true, reader.ReadRootValueAsArray); + Assert.AreEqual(DateTimeKind.Utc, reader.DateTimeKindHandling); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("a", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("b", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + byte[] encodedStringData = await reader.ReadAsBytesAsync(); + Assert.IsNotNull(encodedStringData); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + Assert.AreEqual(encodedStringData, reader.Value); + Assert.AreEqual(typeof(byte[]), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + + string decodedString = Encoding.UTF8.GetString(encodedStringData, 0, encodedStringData.Length); + Assert.AreEqual("Hello world!", decodedString); + } + + [Test] + public async Task ReadOidAsync() + { + byte[] data = HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000"); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("_id", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + CollectionAssert.AreEquivalent(HexToBytes("4ABBED9D1D8B0F0218000001"), (byte[])reader.Value); + Assert.AreEqual(typeof(byte[]), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("test", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("1234£56", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadNestedArrayAsync() + { + string hexdoc = "82-00-00-00-07-5F-69-64-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00"; + + byte[] data = HexToBytes(hexdoc); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("_id", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + CollectionAssert.AreEquivalent(HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF"), (byte[])reader.Value); + Assert.AreEqual(typeof(byte[]), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("a", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + for (int i = 1; i <= 8; i++) + { + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + + double value = (i != 5) + ? Convert.ToDouble(i) + : 5.78960446186581E+77d; + + Assert.AreEqual(value, reader.Value); + } + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("b", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("test", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadRegexAsync() + { + string hexdoc = "15-00-00-00-0B-72-65-67-65-78-00-74-65-73-74-00-67-69-6D-00-00"; + + byte[] data = HexToBytes(hexdoc); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("regex", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual(@"/test/gim", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadCodeAsync() + { + string hexdoc = "1A-00-00-00-0D-63-6F-64-65-00-0B-00-00-00-49-20-61-6D-20-63-6F-64-65-21-00-00"; + + byte[] data = HexToBytes(hexdoc); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("code", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual(@"I am code!", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadUndefinedAsync() + { + string hexdoc = "10-00-00-00-06-75-6E-64-65-66-69-6E-65-64-00-00"; + + byte[] data = HexToBytes(hexdoc); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("undefined", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Undefined, reader.TokenType); + Assert.AreEqual(null, reader.Value); + Assert.AreEqual(null, reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadLongAsync() + { + string hexdoc = "13-00-00-00-12-6C-6F-6E-67-00-FF-FF-FF-FF-FF-FF-FF-7F-00"; + + byte[] data = HexToBytes(hexdoc); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("long", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + Assert.AreEqual(long.MaxValue, reader.Value); + Assert.AreEqual(typeof(long), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadReferenceAsync() + { + string hexdoc = "1E-00-00-00-0C-6F-69-64-00-04-00-00-00-6F-69-64-00-01-02-03-04-05-06-07-08-09-0A-0B-0C-00"; + + byte[] data = HexToBytes(hexdoc); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("oid", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("$ref", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("oid", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("$id", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + CollectionAssert.AreEquivalent(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, (byte[])reader.Value); + Assert.AreEqual(typeof(byte[]), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadCodeWScopeAsync() + { + string hexdoc = "75-00-00-00-0F-63-6F-64-65-57-69-74-68-53-63-6F-70-65-00-61-00-00-00-35-00-00-00-66-6F-72-20-28-69-6E-74-20-69-20-3D-20-30-3B-20-69-20-3C-20-31-30-30-30-3B-20-69-2B-2B-29-0D-0A-7B-0D-0A-20-20-61-6C-65-72-74-28-61-72-67-31-29-3B-0D-0A-7D-00-24-00-00-00-02-61-72-67-31-00-15-00-00-00-4A-73-6F-6E-2E-4E-45-54-20-69-73-20-61-77-65-73-6F-6D-65-2E-00-00-00"; + + byte[] data = HexToBytes(hexdoc); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("codeWithScope", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("$code", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("for (int i = 0; i < 1000; i++)\r\n{\r\n alert(arg1);\r\n}", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("$scope", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("arg1", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("Json.NET is awesome.", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadEndOfStreamAsync() + { + BsonReader reader = new BsonReader(new MemoryStream()); + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ReadLargeStringsAsync() + { + string bson = + "4E-02-00-00-02-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-22-01-00-00-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-00"; + + BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson))); + + StringBuilder largeStringBuilder = new StringBuilder(); + for (int i = 0; i < 100; i++) + { + if (i > 0) + { + largeStringBuilder.Append("-"); + } + + largeStringBuilder.Append(i.ToString(CultureInfo.InvariantCulture)); + } + string largeString = largeStringBuilder.ToString(); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual(largeString, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual(largeString, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadEmptyStringsAsync() + { + string bson = "0C-00-00-00-02-00-01-00-00-00-00-00"; + + BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson))); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task WriteAndReadEmptyListsAndDictionariesAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("Arguments"); + await writer.WriteStartObjectAsync(); + await writer.WriteEndObjectAsync(); + await writer.WritePropertyNameAsync("List"); + await writer.WriteStartArrayAsync(); + await writer.WriteEndArrayAsync(); + await writer.WriteEndObjectAsync(); + + string bson = BitConverter.ToString(ms.ToArray()); + + Assert.AreEqual("20-00-00-00-03-41-72-67-75-6D-65-6E-74-73-00-05-00-00-00-00-04-4C-69-73-74-00-05-00-00-00-00-00", bson); + + BsonReader reader = new BsonReader(new MemoryStream(HexToBytes(bson))); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("Arguments", reader.Value.ToString()); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("List", reader.Value.ToString()); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task UnspecifiedDateTimeKindHandlingAsync() + { + DateTime value = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); + + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + writer.DateTimeKindHandling = DateTimeKind.Unspecified; + + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("DateTime"); + await writer.WriteValueAsync(value); + await writer.WriteEndObjectAsync(); + + byte[] bson = ms.ToArray(); + + BsonReader reader = new BsonReader(new MemoryStream(bson), false, DateTimeKind.Unspecified); + JObject o = (JObject)JToken.ReadFrom(reader); + Assert.AreEqual(value, (DateTime)o["DateTime"]); + } + + [Test] + public async Task LocalDateTimeKindHandlingAsync() + { + DateTime value = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local); + + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("DateTime"); + await writer.WriteValueAsync(value); + await writer.WriteEndObjectAsync(); + + byte[] bson = ms.ToArray(); + + BsonReader reader = new BsonReader(new MemoryStream(bson), false, DateTimeKind.Local); + JObject o = (JObject)JToken.ReadFrom(reader); + Assert.AreEqual(value, (DateTime)o["DateTime"]); + } + + private async Task WriteAndReadStringValueAsync(string val) + { + MemoryStream ms = new MemoryStream(); + BsonWriter bs = new BsonWriter(ms); + await bs.WriteStartObjectAsync(); + await bs.WritePropertyNameAsync("StringValue"); + await bs.WriteValueAsync(val); + await bs.WriteEndAsync(); + + ms.Seek(0, SeekOrigin.Begin); + + BsonReader reader = new BsonReader(ms); + // object + await reader.ReadAsync(); + // property name + await reader.ReadAsync(); + // string + await reader.ReadAsync(); + return (string)reader.Value; + } + + private async Task WriteAndReadStringPropertyNameAsync(string val) + { + MemoryStream ms = new MemoryStream(); + BsonWriter bs = new BsonWriter(ms); + await bs.WriteStartObjectAsync(); + await bs.WritePropertyNameAsync(val); + await bs.WriteValueAsync("Dummy"); + await bs.WriteEndAsync(); + + ms.Seek(0, SeekOrigin.Begin); + + BsonReader reader = new BsonReader(ms); + // object + await reader.ReadAsync(); + // property name + await reader.ReadAsync(); + return (string)reader.Value; + } + + [Test] + public async Task TestReadLenStringValueShortTripleByteAsync() + { + StringBuilder sb = new StringBuilder(); + //sb.Append('1',127); //first char of euro at the end of the boundry. + //sb.Append(euro, 5); + //sb.Append('1',128); + sb.Append(Euro); + + string expected = sb.ToString(); + Assert.AreEqual(expected, await WriteAndReadStringValueAsync(expected)); + } + + [Test] + public async Task TestReadLenStringValueTripleByteCharBufferBoundry0Async() + { + StringBuilder sb = new StringBuilder(); + sb.Append('1', 127); //first char of euro at the end of the boundry. + sb.Append(Euro, 5); + sb.Append('1', 128); + sb.Append(Euro); + + string expected = sb.ToString(); + Assert.AreEqual(expected, await WriteAndReadStringValueAsync(expected)); + } + + [Test] + public async Task TestReadLenStringValueTripleByteCharBufferBoundry1Async() + { + StringBuilder sb = new StringBuilder(); + sb.Append('1', 126); + sb.Append(Euro, 5); //middle char of euro at the end of the boundry. + sb.Append('1', 128); + sb.Append(Euro); + + string expected = sb.ToString(); + string result = await WriteAndReadStringValueAsync(expected); + Assert.AreEqual(expected, result); + } + + [Test] + public async Task TestReadLenStringValueTripleByteCharOneAsync() + { + StringBuilder sb = new StringBuilder(); + sb.Append(Euro, 1); //Just one triple byte char in the string. + + string expected = sb.ToString(); + Assert.AreEqual(expected, await WriteAndReadStringValueAsync(expected)); + } + + [Test] + public async Task TestReadLenStringValueTripleByteCharBufferBoundry2Async() + { + StringBuilder sb = new StringBuilder(); + sb.Append('1', 125); + sb.Append(Euro, 5); //last char of the eruo at the end of the boundry. + sb.Append('1', 128); + sb.Append(Euro); + + string expected = sb.ToString(); + Assert.AreEqual(expected, await WriteAndReadStringValueAsync(expected)); + } + + [Test] + public async Task TestReadStringValueAsync() + { + string expected = "test"; + Assert.AreEqual(expected, await WriteAndReadStringValueAsync(expected)); + } + + [Test] + public async Task TestReadStringValueLongAsync() + { + StringBuilder sb = new StringBuilder(); + sb.Append('t', 150); + string expected = sb.ToString(); + Assert.AreEqual(expected, await WriteAndReadStringValueAsync(expected)); + } + + [Test] + public async Task TestReadStringPropertyNameShortTripleByteAsync() + { + StringBuilder sb = new StringBuilder(); + //sb.Append('1',127); //first char of euro at the end of the boundry. + //sb.Append(euro, 5); + //sb.Append('1',128); + sb.Append(Euro); + + string expected = sb.ToString(); + Assert.AreEqual(expected, await WriteAndReadStringPropertyNameAsync(expected)); + } + + [Test] + public async Task TestReadStringPropertyNameTripleByteCharBufferBoundry0Async() + { + StringBuilder sb = new StringBuilder(); + sb.Append('1', 127); //first char of euro at the end of the boundry. + sb.Append(Euro, 5); + sb.Append('1', 128); + sb.Append(Euro); + + string expected = sb.ToString(); + string result = await WriteAndReadStringPropertyNameAsync(expected); + Assert.AreEqual(expected, result); + } + + [Test] + public async Task TestReadStringPropertyNameTripleByteCharBufferBoundry1Async() + { + StringBuilder sb = new StringBuilder(); + sb.Append('1', 126); + sb.Append(Euro, 5); //middle char of euro at the end of the boundry. + sb.Append('1', 128); + sb.Append(Euro); + + string expected = sb.ToString(); + Assert.AreEqual(expected, await WriteAndReadStringPropertyNameAsync(expected)); + } + + [Test] + public async Task TestReadStringPropertyNameTripleByteCharOneAsync() + { + StringBuilder sb = new StringBuilder(); + sb.Append(Euro, 1); //Just one triple byte char in the string. + + string expected = sb.ToString(); + Assert.AreEqual(expected, await WriteAndReadStringPropertyNameAsync(expected)); + } + + [Test] + public async Task TestReadStringPropertyNameTripleByteCharBufferBoundry2Async() + { + StringBuilder sb = new StringBuilder(); + sb.Append('1', 125); + sb.Append(Euro, 5); //last char of the eruo at the end of the boundry. + sb.Append('1', 128); + sb.Append(Euro); + + string expected = sb.ToString(); + Assert.AreEqual(expected, await WriteAndReadStringPropertyNameAsync(expected)); + } + + [Test] + public async Task TestReadStringPropertyNameAsync() + { + string expected = "test"; + Assert.AreEqual(expected, await WriteAndReadStringPropertyNameAsync(expected)); + } + + [Test] + public async Task TestReadStringPropertyNameLongAsync() + { + StringBuilder sb = new StringBuilder(); + sb.Append('t', 150); + string expected = sb.ToString(); + Assert.AreEqual(expected, await WriteAndReadStringPropertyNameAsync(expected)); + } + + [Test] + public async Task ReadRegexWithOptionsAsync() + { + string hexdoc = "1A-00-00-00-0B-72-65-67-65-78-00-61-62-63-00-69-00-0B-74-65-73-74-00-00-00-00"; + + byte[] data = HexToBytes(hexdoc); + + MemoryStream ms = new MemoryStream(data); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("/abc/i", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("//", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task CanRoundTripStackOverflowDataAsync() + { + var doc = + @"{ +""AboutMe"": ""

I'm the Director for Research and Development for ProPhoenix, a public safety software company. This position allows me to investigate new and existing technologies and incorporate them into our product line, with the end goal being to help public safety agencies to do their jobs more effeciently and safely.

\r\n\r\n

I'm an advocate for PowerShell, as I believe it encourages administrative best practices and allows developers to provide additional access to their applications, without needing to explicity write code for each administrative feature. Part of my advocacy for PowerShell includes my blog, appearances on various podcasts, and acting as a Community Director for PowerShellCommunity.Org

\r\n\r\n

I’m also a co-host of Mind of Root (a weekly audio podcast about systems administration, tech news, and topics).

\r\n"", +""WebsiteUrl"": ""http://blog.usepowershell.com"" +}"; + JObject parsed = JObject.Parse(doc); + var memoryStream = new MemoryStream(); + var bsonWriter = new BsonWriter(memoryStream); + parsed.WriteTo(bsonWriter); + await bsonWriter.FlushAsync(); + memoryStream.Position = 0; + + BsonReader reader = new BsonReader(memoryStream); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("AboutMe", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("

I'm the Director for Research and Development for ProPhoenix, a public safety software company. This position allows me to investigate new and existing technologies and incorporate them into our product line, with the end goal being to help public safety agencies to do their jobs more effeciently and safely.

\r\n\r\n

I'm an advocate for PowerShell, as I believe it encourages administrative best practices and allows developers to provide additional access to their applications, without needing to explicity write code for each administrative feature. Part of my advocacy for PowerShell includes my blog, appearances on various podcasts, and acting as a Community Director for PowerShellCommunity.Org

\r\n\r\n

I’m also a co-host of Mind of Root (a weekly audio podcast about systems administration, tech news, and topics).

\r\n", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("WebsiteUrl", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("http://blog.usepowershell.com", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task MultibyteCharacterPropertyNamesAndStringsAsync() + { + string json = @"{ + ""ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ"": ""ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ"" +}"; + JObject parsed = JObject.Parse(json); + var memoryStream = new MemoryStream(); + var bsonWriter = new BsonWriter(memoryStream); + parsed.WriteTo(bsonWriter); + await bsonWriter.FlushAsync(); + memoryStream.Position = 0; + + BsonReader reader = new BsonReader(memoryStream); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("ΕΝΤΟΛΗ ΧΧΧ ΧΧΧΧΧΧΧΧΧ ΤΑ ΠΡΩΤΑΣΦΑΛΙΣΤΗΡΙΑ ΠΟΥ ΔΕΝ ΕΧΟΥΝ ΥΠΟΛΟΙΠΟ ΝΑ ΤΑ ΣΤΕΛΝΟΥΜΕ ΑΠΕΥΘΕΙΑΣ ΣΤΟΥΣ ΠΕΛΑΤΕΣ", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task GuidsShouldBeProperlyDeserialisedAsync() + { + Guid g = new Guid("822C0CE6-CC42-4753-A3C3-26F0684A4B88"); + + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("TheGuid"); + await writer.WriteValueAsync(g); + await writer.WriteEndObjectAsync(); + await writer.FlushAsync(); + + byte[] bytes = ms.ToArray(); + + BsonReader reader = new BsonReader(new MemoryStream(bytes)); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + Assert.AreEqual(typeof(Guid), reader.ValueType); + Assert.AreEqual(g, (Guid)reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + + JsonSerializer serializer = new JsonSerializer(); + serializer.MetadataPropertyHandling = MetadataPropertyHandling.Default; + ObjectTestClass b = serializer.Deserialize(new BsonReader(new MemoryStream(bytes))); + Assert.AreEqual(typeof(Guid), b.TheGuid.GetType()); + Assert.AreEqual(g, (Guid)b.TheGuid); + } + + [Test] + public async Task GuidsShouldBeProperlyDeserialised_AsBytesAsync() + { + Guid g = new Guid("822C0CE6-CC42-4753-A3C3-26F0684A4B88"); + + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("TheGuid"); + await writer.WriteValueAsync(g); + await writer.WriteEndObjectAsync(); + await writer.FlushAsync(); + + byte[] bytes = ms.ToArray(); + + BsonReader reader = new BsonReader(new MemoryStream(bytes)); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + + CollectionAssert.AreEquivalent(g.ToByteArray(), reader.ReadAsBytes()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + Assert.AreEqual(typeof(byte[]), reader.ValueType); + CollectionAssert.AreEquivalent(g.ToByteArray(), (byte[])reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + + JsonSerializer serializer = new JsonSerializer(); + BytesTestClass b = serializer.Deserialize(new BsonReader(new MemoryStream(bytes))); + CollectionAssert.AreEquivalent(g.ToByteArray(), b.TheGuid); + } + + [Test] + public async Task GuidsShouldBeProperlyDeserialised_AsBytes_ReadAheadAsync() + { + Guid g = new Guid("822C0CE6-CC42-4753-A3C3-26F0684A4B88"); + + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("TheGuid"); + await writer.WriteValueAsync(g); + await writer.WriteEndObjectAsync(); + await writer.FlushAsync(); + + byte[] bytes = ms.ToArray(); + + BsonReader reader = new BsonReader(new MemoryStream(bytes)); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + + CollectionAssert.AreEquivalent(g.ToByteArray(), reader.ReadAsBytes()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + Assert.AreEqual(typeof(byte[]), reader.ValueType); + CollectionAssert.AreEquivalent(g.ToByteArray(), (byte[])reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + + JsonSerializer serializer = new JsonSerializer(); + serializer.MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead; + BytesTestClass b = serializer.Deserialize(new BsonReader(new MemoryStream(bytes))); + CollectionAssert.AreEquivalent(g.ToByteArray(), b.TheGuid); + } + + public class BytesTestClass + { + public byte[] TheGuid { get; set; } + } + + public class ObjectTestClass + { + public object TheGuid { get; set; } + } + } +} + +#endif diff --git a/Src/Newtonsoft.Json.Tests/Bson/BsonWriterAsyncTests.cs b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterAsyncTests.cs new file mode 100644 index 00000000..53ca1743 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Bson/BsonWriterAsyncTests.cs @@ -0,0 +1,541 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +#if !(NET20 || NET35 || PORTABLE) || NETSTANDARD1_1 +using System.Numerics; +#endif +using System.Text; +using System.Threading.Tasks; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif +using Newtonsoft.Json.Bson; +using System.IO; +using System.Globalization; + +namespace Newtonsoft.Json.Tests.Bson +{ + [TestFixture] + public class BsonWriterAsyncTests : TestFixtureBase + { + [Test] + public async Task CloseOutputAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + Assert.IsTrue(ms.CanRead); + await writer.CloseAsync(); + Assert.IsFalse(ms.CanRead); + + ms = new MemoryStream(); + writer = new BsonWriter(ms) { CloseOutput = false }; + + Assert.IsTrue(ms.CanRead); + await writer.CloseAsync(); + Assert.IsTrue(ms.CanRead); + } + + [Test] + public async Task WriteSingleObjectAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("Blah"); + await writer.WriteValueAsync(1); + await writer.WriteEndObjectAsync(); + + string bson = BytesToHex(ms.ToArray()); + Assert.AreEqual("0F-00-00-00-10-42-6C-61-68-00-01-00-00-00-00", bson); + } + + [Test] + public async Task WriteValuesAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartArrayAsync(); + await writer.WriteValueAsync(long.MaxValue); + await writer.WriteValueAsync((ulong)long.MaxValue); + await writer.WriteValueAsync(int.MaxValue); + await writer.WriteValueAsync((uint)int.MaxValue); + await writer.WriteValueAsync(byte.MaxValue); + await writer.WriteValueAsync(sbyte.MaxValue); + await writer.WriteValueAsync('a'); + await writer.WriteValueAsync(decimal.MaxValue); + await writer.WriteValueAsync(double.MaxValue); + await writer.WriteValueAsync(float.MaxValue); + await writer.WriteValueAsync(true); + await writer.WriteValueAsync(new byte[] { 0, 1, 2, 3, 4 }); + await writer.WriteValueAsync(new DateTimeOffset(2000, 12, 29, 12, 30, 0, TimeSpan.Zero)); + await writer.WriteValueAsync(new DateTime(2000, 12, 29, 12, 30, 0, DateTimeKind.Utc)); + await writer.WriteEndAsync(); + + string bson = BytesToHex(ms.ToArray()); + Assert.AreEqual("8C-00-00-00-12-30-00-FF-FF-FF-FF-FF-FF-FF-7F-12-31-00-FF-FF-FF-FF-FF-FF-FF-7F-10-32-00-FF-FF-FF-7F-10-33-00-FF-FF-FF-7F-10-34-00-FF-00-00-00-10-35-00-7F-00-00-00-02-36-00-02-00-00-00-61-00-01-37-00-00-00-00-00-00-00-F0-45-01-38-00-FF-FF-FF-FF-FF-FF-EF-7F-01-39-00-00-00-00-E0-FF-FF-EF-47-08-31-30-00-01-05-31-31-00-05-00-00-00-00-00-01-02-03-04-09-31-32-00-40-C5-E2-BA-E3-00-00-00-09-31-33-00-40-C5-E2-BA-E3-00-00-00-00", bson); + } + + [Test] + public async Task WriteDoubleAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartArrayAsync(); + await writer.WriteValueAsync(99.99d); + await writer.WriteEndAsync(); + + string bson = BytesToHex(ms.ToArray()); + Assert.AreEqual("10-00-00-00-01-30-00-8F-C2-F5-28-5C-FF-58-40-00", bson); + } + + [Test] + public async Task WriteGuidAsync() + { + Guid g = new Guid("D821EED7-4B5C-43C9-8AC2-6928E579B705"); + + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartArrayAsync(); + await writer.WriteValueAsync(g); + await writer.WriteEndAsync(); + + string bson = BytesToHex(ms.ToArray()); + Assert.AreEqual("1D-00-00-00-05-30-00-10-00-00-00-04-D7-EE-21-D8-5C-4B-C9-43-8A-C2-69-28-E5-79-B7-05-00", bson); + } + + [Test] + public async Task WriteArrayBsonFromSiteAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + await writer.WriteStartArrayAsync(); + await writer.WriteValueAsync("a"); + await writer.WriteValueAsync("b"); + await writer.WriteValueAsync("c"); + await writer.WriteEndArrayAsync(); + + await writer.FlushAsync(); + + ms.Seek(0, SeekOrigin.Begin); + + string expected = "20-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-02-32-00-02-00-00-00-63-00-00"; + string bson = BytesToHex(ms.ToArray()); + + Assert.AreEqual(expected, bson); + } + + [Test] + public async Task WriteBytesAsync() + { + byte[] data = Encoding.UTF8.GetBytes("Hello world!"); + + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + await writer.WriteStartArrayAsync(); + await writer.WriteValueAsync("a"); + await writer.WriteValueAsync("b"); + await writer.WriteValueAsync(data); + await writer.WriteEndArrayAsync(); + + await writer.FlushAsync(); + + ms.Seek(0, SeekOrigin.Begin); + + string expected = "2B-00-00-00-02-30-00-02-00-00-00-61-00-02-31-00-02-00-00-00-62-00-05-32-00-0C-00-00-00-00-48-65-6C-6C-6F-20-77-6F-72-6C-64-21-00"; + string bson = BytesToHex(ms.ToArray()); + + Assert.AreEqual(expected, bson); + + BsonReader reader = new BsonReader(new MemoryStream(ms.ToArray())); + reader.ReadRootValueAsArray = true; + await reader.ReadAsync(); + await reader.ReadAsync(); + await reader.ReadAsync(); + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + CollectionAssert.AreEquivalent(data, (byte[])reader.Value); + } + + [Test] + public async Task WriteNestedArrayAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + await writer.WriteStartObjectAsync(); + + await writer.WritePropertyNameAsync("_id"); + await writer.WriteValueAsync(HexToBytes("4A-78-93-79-17-22-00-00-00-00-61-CF")); + + await writer.WritePropertyNameAsync("a"); + await writer.WriteStartArrayAsync(); + for (int i = 1; i <= 8; i++) + { + double value = (i != 5) + ? Convert.ToDouble(i) + : 5.78960446186581E+77d; + + await writer.WriteValueAsync(value); + } + await writer.WriteEndArrayAsync(); + + await writer.WritePropertyNameAsync("b"); + await writer.WriteValueAsync("test"); + + await writer.WriteEndObjectAsync(); + + await writer.FlushAsync(); + + ms.Seek(0, SeekOrigin.Begin); + + string expected = "87-00-00-00-05-5F-69-64-00-0C-00-00-00-00-4A-78-93-79-17-22-00-00-00-00-61-CF-04-61-00-5D-00-00-00-01-30-00-00-00-00-00-00-00-F0-3F-01-31-00-00-00-00-00-00-00-00-40-01-32-00-00-00-00-00-00-00-08-40-01-33-00-00-00-00-00-00-00-10-40-01-34-00-00-00-00-00-00-00-14-50-01-35-00-00-00-00-00-00-00-18-40-01-36-00-00-00-00-00-00-00-1C-40-01-37-00-00-00-00-00-00-00-20-40-00-02-62-00-05-00-00-00-74-65-73-74-00-00"; + string bson = BytesToHex(ms.ToArray()); + + Assert.AreEqual(expected, bson); + } + + [Test] + public async Task WriteLargeStringsAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + StringBuilder largeStringBuilder = new StringBuilder(); + for (int i = 0; i < 100; i++) + { + if (i > 0) + { + largeStringBuilder.Append("-"); + } + + largeStringBuilder.Append(i.ToString(CultureInfo.InvariantCulture)); + } + string largeString = largeStringBuilder.ToString(); + + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync(largeString); + await writer.WriteValueAsync(largeString); + await writer.WriteEndObjectAsync(); + + string bson = BytesToHex(ms.ToArray()); + Assert.AreEqual("4E-02-00-00-02-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-22-01-00-00-30-2D-31-2D-32-2D-33-2D-34-2D-35-2D-36-2D-37-2D-38-2D-39-2D-31-30-2D-31-31-2D-31-32-2D-31-33-2D-31-34-2D-31-35-2D-31-36-2D-31-37-2D-31-38-2D-31-39-2D-32-30-2D-32-31-2D-32-32-2D-32-33-2D-32-34-2D-32-35-2D-32-36-2D-32-37-2D-32-38-2D-32-39-2D-33-30-2D-33-31-2D-33-32-2D-33-33-2D-33-34-2D-33-35-2D-33-36-2D-33-37-2D-33-38-2D-33-39-2D-34-30-2D-34-31-2D-34-32-2D-34-33-2D-34-34-2D-34-35-2D-34-36-2D-34-37-2D-34-38-2D-34-39-2D-35-30-2D-35-31-2D-35-32-2D-35-33-2D-35-34-2D-35-35-2D-35-36-2D-35-37-2D-35-38-2D-35-39-2D-36-30-2D-36-31-2D-36-32-2D-36-33-2D-36-34-2D-36-35-2D-36-36-2D-36-37-2D-36-38-2D-36-39-2D-37-30-2D-37-31-2D-37-32-2D-37-33-2D-37-34-2D-37-35-2D-37-36-2D-37-37-2D-37-38-2D-37-39-2D-38-30-2D-38-31-2D-38-32-2D-38-33-2D-38-34-2D-38-35-2D-38-36-2D-38-37-2D-38-38-2D-38-39-2D-39-30-2D-39-31-2D-39-32-2D-39-33-2D-39-34-2D-39-35-2D-39-36-2D-39-37-2D-39-38-2D-39-39-00-00", bson); + } + + [Test] + public async Task WriteEmptyStringsAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync(""); + await writer.WriteValueAsync(""); + await writer.WriteEndObjectAsync(); + + string bson = BytesToHex(ms.ToArray()); + Assert.AreEqual("0C-00-00-00-02-00-01-00-00-00-00-00", bson); + } + + [Test] + public async Task WriteCommentAsync() + { + await ExceptionAssert.ThrowsAsync(async () => + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartArrayAsync(); + await writer.WriteCommentAsync("fail"); + }, "Cannot write JSON comment as BSON. Path ''."); + } + + [Test] + public async Task WriteConstructorAsync() + { + await ExceptionAssert.ThrowsAsync(async () => + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartArrayAsync(); + await writer.WriteStartConstructorAsync("fail"); + }, "Cannot write JSON constructor as BSON. Path ''."); + } + + [Test] + public async Task WriteRawAsync() + { + await ExceptionAssert.ThrowsAsync(async () => + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartArrayAsync(); + await writer.WriteRawAsync("fail"); + }, "Cannot write raw JSON as BSON. Path ''."); + } + + [Test] + public async Task WriteRawValueAsync() + { + await ExceptionAssert.ThrowsAsync(async () => + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartArrayAsync(); + await writer.WriteRawValueAsync("fail"); + }, "Cannot write raw JSON as BSON. Path ''."); + } + + [Test] + public async Task WriteOidAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + byte[] oid = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; + + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("_oid"); + writer.WriteObjectId(oid); + await writer.WriteEndObjectAsync(); + + string bson = BytesToHex(ms.ToArray()); + Assert.AreEqual("17-00-00-00-07-5F-6F-69-64-00-01-02-03-04-05-06-07-08-09-0A-0B-0C-00", bson); + + ms.Seek(0, SeekOrigin.Begin); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + CollectionAssert.AreEquivalent(oid, (byte[])reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task WriteOidPlusContentAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("_id"); + writer.WriteObjectId(HexToBytes("4ABBED9D1D8B0F0218000001")); + await writer.WritePropertyNameAsync("test"); + await writer.WriteValueAsync("1234£56"); + await writer.WriteEndObjectAsync(); + + byte[] expected = HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000"); + + CollectionAssert.AreEquivalent(expected, ms.ToArray()); + } + + [Test] + public async Task WriteRegexPlusContentAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("regex"); + writer.WriteRegex("abc", "i"); + await writer.WritePropertyNameAsync("test"); + writer.WriteRegex(string.Empty, null); + await writer.WriteEndObjectAsync(); + + byte[] expected = HexToBytes("1A-00-00-00-0B-72-65-67-65-78-00-61-62-63-00-69-00-0B-74-65-73-74-00-00-00-00"); + + CollectionAssert.AreEquivalent(expected, ms.ToArray()); + } + + [Test] + public async Task WriteReadEmptyAndNullStringsAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartArrayAsync(); + await writer.WriteValueAsync("Content!"); + await writer.WriteValueAsync(""); + await writer.WriteValueAsync((string)null); + await writer.WriteEndArrayAsync(); + + ms.Seek(0, SeekOrigin.Begin); + + BsonReader reader = new BsonReader(ms); + reader.ReadRootValueAsArray = true; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("Content!", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Null, reader.TokenType); + Assert.AreEqual(null, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task WriteDateTimesAsync() + { + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + writer.DateTimeKindHandling = DateTimeKind.Unspecified; + + await writer.WriteStartArrayAsync(); + await writer.WriteValueAsync(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc)); + await writer.WriteValueAsync(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Local)); + await writer.WriteValueAsync(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Unspecified)); + await writer.WriteEndArrayAsync(); + + ms.Seek(0, SeekOrigin.Begin); + + BsonReader reader = new BsonReader(ms); + reader.ReadRootValueAsArray = true; + reader.DateTimeKindHandling = DateTimeKind.Utc; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(new DateTime(2000, 10, 12, 20, 55, 0, DateTimeKind.Utc), reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task WriteValueOutsideOfObjectOrArrayAsync() + { + await ExceptionAssert.ThrowsAsync(async () => + { + MemoryStream stream = new MemoryStream(); + + using (BsonWriter writer = new BsonWriter(stream)) + { + await writer.WriteValueAsync("test"); + await writer.FlushAsync(); + } + }, "Error writing String value. BSON must start with an Object or Array. Path ''."); + } + + [Test] + public async Task DateTimeZoneHandlingAsync() + { + MemoryStream ms = new MemoryStream(); + JsonWriter writer = new BsonWriter(ms) + { + DateTimeZoneHandling = DateTimeZoneHandling.Utc + }; + + await writer.WriteStartArrayAsync(); + await writer.WriteValueAsync(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified)); + await writer.WriteEndArrayAsync(); + + Assert.AreEqual("10-00-00-00-09-30-00-C8-88-07-6B-DC-00-00-00-00", (BitConverter.ToString(ms.ToArray()))); + } + +#if !PORTABLE || NETSTANDARD1_1 + [Test] + public async Task WriteBigIntegerAsync() + { + BigInteger i = BigInteger.Parse("1999999999999999999999999999999999999999999999999999999999990"); + + MemoryStream ms = new MemoryStream(); + BsonWriter writer = new BsonWriter(ms); + + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("Blah"); + await writer.WriteValueAsync(i); + await writer.WriteEndObjectAsync(); + + string bson = BytesToHex(ms.ToArray()); + Assert.AreEqual("2A-00-00-00-05-42-6C-61-68-00-1A-00-00-00-00-F6-FF-FF-FF-FF-FF-FF-1F-B2-21-CB-28-59-84-C4-AE-03-8A-44-34-2F-4C-4E-9E-3E-01-00", bson); + + ms.Seek(0, SeekOrigin.Begin); + BsonReader reader = new BsonReader(ms); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + CollectionAssert.AreEqual(new byte[] { 246, 255, 255, 255, 255, 255, 255, 31, 178, 33, 203, 40, 89, 132, 196, 174, 3, 138, 68, 52, 47, 76, 78, 158, 62, 1 }, (byte[])reader.Value); + Assert.AreEqual(i, new BigInteger((byte[])reader.Value)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + } +#endif + } +} + +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ExceptionHandlingAsyncTests.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ExceptionHandlingAsyncTests.cs new file mode 100644 index 00000000..db959b22 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ExceptionHandlingAsyncTests.cs @@ -0,0 +1,1006 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +using System.Globalization; +#if !PORTABLE || NETSTANDARD1_1 +using System.Numerics; +#endif +using System.Text; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif +using System.IO; +using System.Threading.Tasks; +using Newtonsoft.Json.Tests.TestObjects.JsonTextReaderTests; + +namespace Newtonsoft.Json.Tests.JsonTextReaderTests +{ + [TestFixture] +#if !DNXCORE50 + [Category("JsonTextReaderTests")] +#endif + public class ExceptionHandlingAsyncTests : TestFixtureBase + { + [Test] + public async Task UnexpectedEndAfterReadingNAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("n")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsync(), "Unexpected end when reading JSON. Path '', line 1, position 1."); + } + + [Test] + public async Task UnexpectedEndAfterReadingNuAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("nu")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsync(), "Unexpected end when reading JSON. Path '', line 1, position 2."); + } + + [Test] + public async Task UnexpectedEndAfterReadingNeAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("ne")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsync(), "Unexpected end when reading JSON. Path '', line 1, position 2."); + } + + [Test] + public async Task UnexpectedEndOfHexAsync() + { + JsonReader reader = new JsonTextReader(new StringReader(@"'h\u123")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unexpected end while parsing Unicode character. Path '', line 1, position 4."); + } + + [Test] + public async Task UnexpectedEndOfControlCharacterAsync() + { + JsonReader reader = new JsonTextReader(new StringReader(@"'h\")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unterminated string. Expected delimiter: '. Path '', line 1, position 3."); + } + + [Test] + public async Task ReadInvalidNonBase10NumberAsync() + { + string json = "0aq2dun13.hod"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unexpected character encountered while parsing number: q. Path '', line 1, position 2."); + + reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsDecimalAsync(); }, "Unexpected character encountered while parsing number: q. Path '', line 1, position 2."); + + reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsInt32Async(); }, "Unexpected character encountered while parsing number: q. Path '', line 1, position 2."); + } + + [Test] + public async Task ThrowErrorWhenParsingUnquoteStringThatStartsWithNEAsync() + { + const string json = @"{ ""ItemName"": ""value"", ""u"":netanelsalinger,""r"":9 }"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unexpected content while parsing JSON. Path 'u', line 1, position 29."); + } + + [Test] + public async Task UnexpectedEndOfStringAsync() + { + JsonReader reader = new JsonTextReader(new StringReader("'hi")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unterminated string. Expected delimiter: '. Path '', line 1, position 3."); + } + + [Test] + public async Task UnexpectedEndTokenWhenParsingOddEndTokenAsync() + { + JsonReader reader = new JsonTextReader(new StringReader(@"{}}")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Additional text encountered after finished reading JSON content: }. Path '', line 1, position 2."); + } + + [Test] + public async Task ResetJsonTextReaderErrorCountAsync() + { + ToggleReaderError toggleReaderError = new ToggleReaderError(new StringReader("{'first':1,'second':2,'third':3}")); + JsonTextReader jsonTextReader = new JsonTextReader(toggleReaderError); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + + toggleReaderError.Error = true; + + await ExceptionAssert.ThrowsAsync(async () => await jsonTextReader.ReadAsync(), "Read error"); + await ExceptionAssert.ThrowsAsync(async () => await jsonTextReader.ReadAsync(), "Read error"); + + toggleReaderError.Error = false; + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual("first", jsonTextReader.Value); + + toggleReaderError.Error = true; + + await ExceptionAssert.ThrowsAsync(async () => await jsonTextReader.ReadAsync(), "Read error"); + + toggleReaderError.Error = false; + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(1L, jsonTextReader.Value); + + toggleReaderError.Error = true; + + await ExceptionAssert.ThrowsAsync(async () => await jsonTextReader.ReadAsync(), "Read error"); + await ExceptionAssert.ThrowsAsync(async () => await jsonTextReader.ReadAsync(), "Read error"); + await ExceptionAssert.ThrowsAsync(async () => await jsonTextReader.ReadAsync(), "Read error"); + + toggleReaderError.Error = false; + } + + [Test] + public async Task MatchWithInsufficentCharactersAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"nul")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unexpected end when reading JSON. Path '', line 1, position 3."); + } + + [Test] + public async Task MatchWithWrongCharactersAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"nulz")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Error parsing null value. Path '', line 1, position 3."); + } + + [Test] + public async Task MatchWithNoTrailingSeparatorAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"nullz")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Error parsing null value. Path '', line 1, position 4."); + } + + [Test] + public async Task UnclosedCommentAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"/* sdf")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unexpected end while parsing comment. Path '', line 1, position 6."); + } + + [Test] + public async Task BadCommentStartAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"/sdf")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Error parsing comment. Expected: *, got s. Path '', line 1, position 1."); + } + + [Test] + public async Task MissingColonAsync() + { + string json = @"{ + ""A"" : true, + ""B"" """; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Boolean, reader.TokenType); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, @"Invalid character after parsing property name. Expected ':' but got: "". Path 'A', line 3, position 8."); + } + + [Test] + public async Task ParseConstructorWithBadCharacterAsync() + { + string json = "new Date,()"; + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { Assert.IsTrue(await reader.ReadAsync()); }, "Unexpected character while parsing constructor: ,. Path '', line 1, position 8."); + } + + [Test] + public async Task ParseConstructorWithUnexpectedEndAsync() + { + string json = "new Dat"; + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unexpected end while parsing constructor. Path '', line 1, position 7."); + } + + [Test] + public async Task ParseConstructorWithUnexpectedCharacterAsync() + { + string json = "new Date !"; + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unexpected character while parsing constructor: !. Path '', line 1, position 9."); + } + + [Test] + public async Task ParseAdditionalContent_CommaAsync() + { + string json = @"[ +""Small"", +""Medium"", +""Large"" +],"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => + { + while (await reader.ReadAsync()) + { + } + }, "Additional text encountered after finished reading JSON content: ,. Path '', line 5, position 1."); + } + + [Test] + public async Task ParseAdditionalContent_TextAsync() + { + string json = @"[ +""Small"", +""Medium"", +""Large"" +]content"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); +#if DEBUG + reader.SetCharBuffer(new char[2]); +#endif + + await reader.ReadAsync(); + Assert.AreEqual(1, reader.LineNumber); + + await reader.ReadAsync(); + Assert.AreEqual(2, reader.LineNumber); + + await reader.ReadAsync(); + Assert.AreEqual(3, reader.LineNumber); + + await reader.ReadAsync(); + Assert.AreEqual(4, reader.LineNumber); + + await reader.ReadAsync(); + Assert.AreEqual(5, reader.LineNumber); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Additional text encountered after finished reading JSON content: c. Path '', line 5, position 1."); + } + + [Test] + public async Task ParseAdditionalContent_WhitespaceThenTextAsync() + { + string json = @"'hi' a"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => + { + while (await reader.ReadAsync()) + { + } + }, "Additional text encountered after finished reading JSON content: a. Path '', line 1, position 5."); + } + + [Test] + public async Task ParseIncompleteCommentSeparatorAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("true/")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Error parsing boolean value. Path '', line 1, position 4."); + } + + [Test] + public async Task ReadBadCharInArrayAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"[}")); + + await reader.ReadAsync(); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unexpected character encountered while parsing value: }. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadAsBytesNoContentWrappedObjectAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"{")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsBytesAsync(); }, "Unexpected end when reading JSON. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadBytesEmptyWrappedObjectAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"{}")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsBytesAsync(); }, "Error reading bytes. Unexpected token: StartObject. Path '', line 1, position 2." ); + } + + [Test] + public async Task ReadIntegerWithErrorAsync() + { + string json = @"{ + ChildId: 333333333333333333333333333333333333333 +}"; + + JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType); + + await ExceptionAssert.ThrowsAsync(async () => await jsonTextReader.ReadAsInt32Async(), "JSON integer 333333333333333333333333333333333333333 is too large or small for an Int32. Path 'ChildId', line 2, position 52."); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType); + + Assert.IsFalse(await jsonTextReader.ReadAsync()); + } + + [Test] + public async Task ReadIntegerWithErrorInArrayAsync() + { + string json = @"[ + 333333333333333333333333333333333333333, + 3.3, + , + 0f +]"; + + JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, jsonTextReader.TokenType); + + await ExceptionAssert.ThrowsAsync(async () => await jsonTextReader.ReadAsInt32Async(), "JSON integer 333333333333333333333333333333333333333 is too large or small for an Int32. Path '[0]', line 2, position 41."); + + await ExceptionAssert.ThrowsAsync(async () => await jsonTextReader.ReadAsInt32Async(), "Input string '3.3' is not a valid integer. Path '[1]', line 3, position 5."); + + await ExceptionAssert.ThrowsAsync(async () => await jsonTextReader.ReadAsInt32Async(), "Unexpected character encountered while parsing value: ,. Path '[2]', line 4, position 3."); + + await ExceptionAssert.ThrowsAsync(async () => await jsonTextReader.ReadAsInt32Async(), "Input string '0f' is not a valid integer. Path '[3]', line 5, position 4."); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, jsonTextReader.TokenType); + + Assert.IsFalse(await jsonTextReader.ReadAsync()); + } + + [Test] + public async Task ReadBytesWithErrorAsync() + { + string json = @"{ + ChildId: '123' +}"; + + JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType); + + try + { + await jsonTextReader.ReadAsBytesAsync(); + } + catch (FormatException) + { + } + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType); + + Assert.IsFalse(await jsonTextReader.ReadAsync()); + } + + [Test] + public async Task ReadInt32OverflowAsync() + { + long i = int.MaxValue; + + JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture))); + await reader.ReadAsync(); + Assert.AreEqual(typeof(long), reader.ValueType); + + for (int j = 1; j < 1000; j++) + { + long total = j + i; + await ExceptionAssert.ThrowsAsync(async () => + { + reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture))); + await reader.ReadAsInt32Async(); + }, "JSON integer " + total + " is too large or small for an Int32. Path '', line 1, position 10."); + } + } + + [Test] + public async Task ReadInt32Overflow_NegativeAsync() + { + long i = int.MinValue; + + JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture))); + await reader.ReadAsync(); + Assert.AreEqual(typeof(long), reader.ValueType); + Assert.AreEqual(i, reader.Value); + + for (int j = 1; j < 1000; j++) + { + long total = -j + i; + await ExceptionAssert.ThrowsAsync(async () => + { + reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture))); + await reader.ReadAsInt32Async(); + }, "JSON integer " + total + " is too large or small for an Int32. Path '', line 1, position 11."); + } + } + +#if !PORTABLE || NETSTANDARD1_1 + [Test] + public async Task ReadInt64OverflowAsync() + { + BigInteger i = new BigInteger(long.MaxValue); + + JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture))); + await reader.ReadAsync(); + Assert.AreEqual(typeof(long), reader.ValueType); + + for (int j = 1; j < 1000; j++) + { + BigInteger total = i + j; + + reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture))); + await reader.ReadAsync(); + + Assert.AreEqual(typeof(BigInteger), reader.ValueType); + } + } + + [Test] + public async Task ReadInt64Overflow_NegativeAsync() + { + BigInteger i = new BigInteger(long.MinValue); + + JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture))); + await reader.ReadAsync(); + Assert.AreEqual(typeof(long), reader.ValueType); + + for (int j = 1; j < 1000; j++) + { + BigInteger total = i + -j; + + reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture))); + await reader.ReadAsync(); + + Assert.AreEqual(typeof(BigInteger), reader.ValueType); + } + } +#endif + + [Test] + public async Task ReadAsString_Null_AdditionalBadDataAsync() + { + string json = @"nullllll"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsStringAsync(); }, "Error parsing null value. Path '', line 1, position 4."); + } + + [Test] + public async Task ReadAsBoolean_AdditionalBadDataAsync() + { + string json = @"falseeeee"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsBooleanAsync(); }, "Unexpected character encountered while parsing value: e. Path '', line 1, position 5."); + } + + [Test] + public async Task ReadAsString_AdditionalBadDataAsync() + { + string json = @"falseeeee"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsStringAsync(); }, "Unexpected character encountered while parsing value: e. Path '', line 1, position 5."); + } + + [Test] + public async Task ReadAsBoolean_UnexpectedEndAsync() + { + string json = @"tru"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsBooleanAsync(); }, "Unexpected end when reading JSON. Path '', line 1, position 3."); + } + + [Test] + public async Task ReadAsBoolean_BadDataAsync() + { + string json = @"pie"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsBooleanAsync(); }, "Unexpected character encountered while parsing value: p. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadAsString_BadDataAsync() + { + string json = @"pie"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsStringAsync(); }, "Unexpected character encountered while parsing value: p. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadAsDouble_BadDataAsync() + { + string json = @"pie"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsDoubleAsync(); }, "Unexpected character encountered while parsing value: p. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadAsDouble_BooleanAsync() + { + string json = @"true"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsDoubleAsync(); }, "Unexpected character encountered while parsing value: t. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadAsBytes_BadDataAsync() + { + string json = @"pie"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsBytesAsync(); }, "Unexpected character encountered while parsing value: p. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadAsBytesIntegerArrayWithNoEndAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"[1")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsBytesAsync(); }, "Unexpected end when reading bytes. Path '[0]', line 1, position 2."); + } + + [Test] + public async Task ReadAsBytesArrayWithBadContentAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"[1.0]")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsBytesAsync(); }, "Unexpected token when reading bytes: Float. Path '[0]', line 1, position 4."); + } + + [Test] + public async Task ReadAsBytesBadContentAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsBytesAsync(); }, "Unexpected character encountered while parsing value: e. Path '', line 1, position 2."); + } + + [Test] + public async Task ReadAsBytes_CommaErrorsAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("[,'']")); + await reader.ReadAsync(); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsBytesAsync(); + }, "Unexpected character encountered while parsing value: ,. Path '[0]', line 1, position 2."); + + CollectionAssert.AreEquivalent(new byte[0], await reader.ReadAsBytesAsync()); + Assert.IsTrue(await reader.ReadAsync()); + } + + [Test] + public async Task ReadAsBytes_InvalidEndArrayAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("]")); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsBytesAsync(); + }, "Unexpected character encountered while parsing value: ]. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadAsBytes_CommaErrors_MultipleAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("['',,'']")); + await reader.ReadAsync(); + CollectionAssert.AreEquivalent(new byte[0], await reader.ReadAsBytesAsync()); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsBytesAsync(); + }, "Unexpected character encountered while parsing value: ,. Path '[1]', line 1, position 5."); + + CollectionAssert.AreEquivalent(new byte[0], await reader.ReadAsBytesAsync()); + Assert.IsTrue(await reader.ReadAsync()); + } + + [Test] + public async Task ReadBytesWithBadCharacterAsync() + { + JsonReader reader = new JsonTextReader(new StringReader(@"true")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsBytesAsync(); }, "Unexpected character encountered while parsing value: t. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadBytesWithUnexpectedEndAsync() + { + string helloWorld = "Hello world!"; + byte[] helloWorldData = Encoding.UTF8.GetBytes(helloWorld); + + JsonReader reader = new JsonTextReader(new StringReader(@"'" + Convert.ToBase64String(helloWorldData))); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsBytesAsync(); }, "Unterminated string. Expected delimiter: '. Path '', line 1, position 17."); + } + + [Test] + public async Task ReadAsDateTime_BadDataAsync() + { + string json = @"pie"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsDateTimeAsync(); }, "Unexpected character encountered while parsing value: p. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadAsDateTime_BooleanAsync() + { + string json = @"true"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsDateTimeAsync(); }, "Unexpected character encountered while parsing value: t. Path '', line 1, position 1."); + } + +#if !NET20 + [Test] + public async Task ReadAsDateTimeOffsetBadContentAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsDateTimeOffsetAsync(); }, "Unexpected character encountered while parsing value: e. Path '', line 1, position 2."); + } +#endif + + [Test] + public async Task ReadAsDecimalBadContentAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"new Date()")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsDecimalAsync(); }, "Unexpected character encountered while parsing value: e. Path '', line 1, position 2."); + } + + [Test] + public async Task ReadAsDecimalBadContent_SecondLineAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@" +new Date()")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsDecimalAsync(); }, "Unexpected character encountered while parsing value: e. Path '', line 2, position 2."); + } + + [Test] + public async Task ReadInt32WithBadCharacterAsync() + { + JsonReader reader = new JsonTextReader(new StringReader(@"true")); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsInt32Async(); }, "Unexpected character encountered while parsing value: t. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadNumberValue_CommaErrorsAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("[,1]")); + await reader.ReadAsync(); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsInt32Async(); + }, "Unexpected character encountered while parsing value: ,. Path '[0]', line 1, position 2."); + + Assert.AreEqual(1, await reader.ReadAsInt32Async()); + Assert.IsTrue(await reader.ReadAsync()); + } + + [Test] + public async Task ReadNumberValue_InvalidEndArrayAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("]")); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsInt32Async(); + }, "Unexpected character encountered while parsing value: ]. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadNumberValue_CommaErrors_MultipleAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("[1,,1]")); + await reader.ReadAsync(); + await reader.ReadAsInt32Async(); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsInt32Async(); + }, "Unexpected character encountered while parsing value: ,. Path '[1]', line 1, position 4."); + + Assert.AreEqual(1, await reader.ReadAsInt32Async()); + Assert.IsTrue(await reader.ReadAsync()); + } + + [Test] + public async Task ReadAsString_UnexpectedEndAsync() + { + string json = @"tru"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsStringAsync(); }, "Unexpected end when reading JSON. Path '', line 1, position 3."); + } + + [Test] + public async Task ReadAsString_Null_UnexpectedEndAsync() + { + string json = @"nul"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsStringAsync(); }, "Unexpected end when reading JSON. Path '', line 1, position 3."); + } + + [Test] + public async Task ReadStringValue_InvalidEndArrayAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("]")); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsDateTimeAsync(); + }, "Unexpected character encountered while parsing value: ]. Path '', line 1, position 1."); + } + + [Test] + public async Task ReadStringValue_CommaErrorsAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("[,'']")); + await reader.ReadAsync(); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsStringAsync(); + }, "Unexpected character encountered while parsing value: ,. Path '[0]', line 1, position 2."); + + Assert.AreEqual(string.Empty, await reader.ReadAsStringAsync()); + Assert.IsTrue(await reader.ReadAsync()); + } + + [Test] + public async Task ReadStringValue_CommaErrors_MultipleAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("['',,'']")); + await reader.ReadAsync(); + await reader.ReadAsInt32Async(); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsStringAsync(); + }, "Unexpected character encountered while parsing value: ,. Path '[1]', line 1, position 5."); + + Assert.AreEqual(string.Empty, await reader.ReadAsStringAsync()); + Assert.IsTrue(await reader.ReadAsync()); + } + + [Test] + public async Task ReadStringValue_Numbers_NotStringAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("[56,56]")); + await reader.ReadAsync(); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsDateTimeAsync(); + }, "Unexpected character encountered while parsing value: 5. Path '', line 1, position 2."); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsDateTimeAsync(); + }, "Unexpected character encountered while parsing value: 6. Path '', line 1, position 3."); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsDateTimeAsync(); + }, "Unexpected character encountered while parsing value: ,. Path '[0]', line 1, position 4."); + + Assert.AreEqual(56, await reader.ReadAsInt32Async()); + Assert.IsTrue(await reader.ReadAsync()); + } + + [Test] + public async Task ErrorReadingCommentAsync() + { + string json = @"/"; + + JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unexpected end while parsing comment. Path '', line 1, position 1."); + } + + [Test] + public async Task EscapedPathInExceptionMessageAsync() + { + string json = @"{ + ""frameworks"": { + ""dnxcore50"": { + ""dependencies"": { + ""System.Xml.ReaderWriter"": { + ""source"": !!! !!! + } + } + } + } +}"; + + await ExceptionAssert.ThrowsAsync( + async () => + { + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + while (await reader.ReadAsync()) + { + } + }, + "Unexpected character encountered while parsing value: !. Path 'frameworks.dnxcore50.dependencies['System.Xml.ReaderWriter'].source', line 6, position 20."); + } + + [Test] + public async Task MaxDepthAsync() + { + string json = "[[]]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)) + { + MaxDepth = 1 + }; + + Assert.IsTrue(await reader.ReadAsync()); + + await ExceptionAssert.ThrowsAsync(async () => { Assert.IsTrue(await reader.ReadAsync()); }, "The reader's MaxDepth of 1 has been exceeded. Path '[0]', line 1, position 2."); + } + + [Test] + public async Task MaxDepthDoesNotRecursivelyErrorAsync() + { + string json = "[[[[]]],[[]]]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)) + { + MaxDepth = 1 + }; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(0, reader.Depth); + + await ExceptionAssert.ThrowsAsync(async () => { Assert.IsTrue(await reader.ReadAsync()); }, "The reader's MaxDepth of 1 has been exceeded. Path '[0]', line 1, position 2."); + Assert.AreEqual(1, reader.Depth); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(2, reader.Depth); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(3, reader.Depth); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(3, reader.Depth); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(2, reader.Depth); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(1, reader.Depth); + + await ExceptionAssert.ThrowsAsync(async () => { Assert.IsTrue(await reader.ReadAsync()); }, "The reader's MaxDepth of 1 has been exceeded. Path '[1]', line 1, position 9."); + Assert.AreEqual(1, reader.Depth); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(2, reader.Depth); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(2, reader.Depth); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(1, reader.Depth); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(0, reader.Depth); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task UnexpectedEndWhenParsingUnquotedPropertyAsync() + { + JsonReader reader = new JsonTextReader(new StringReader(@"{aww")); + Assert.IsTrue(await reader.ReadAsync()); + + await ExceptionAssert.ThrowsAsync(async () => { await reader.ReadAsync(); }, "Unexpected end while parsing unquoted property name. Path '', line 1, position 4."); + } + } +} + +#endif diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/FloatAsyncTests.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/FloatAsyncTests.cs new file mode 100644 index 00000000..ee48f234 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/FloatAsyncTests.cs @@ -0,0 +1,352 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif +using System.IO; +using System.Threading.Tasks; + +namespace Newtonsoft.Json.Tests.JsonTextReaderTests +{ + [TestFixture] +#if !DNXCORE50 + [Category("JsonTextReaderTests")] +#endif + public class FloatAsyncTests : TestFixtureBase + { + [Test] + public async Task Float_ReadAsString_ExactAsync() + { + const string testJson = "{float: 0.0620}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(testJson)); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + + string s = await reader.ReadAsStringAsync(); + Assert.AreEqual("0.0620", s); + } + + [Test] + public async Task Float_NaN_ReadAsync() + { + const string testJson = "{float: NaN}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(testJson)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(double.NaN, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task Float_NaN_ReadAsInt32Async() + { + const string testJson = "{float: NaN}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(testJson)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsInt32Async(), "Cannot read NaN value. Path 'float', line 1, position 11."); + } + + [Test] + public async Task Float_NaNAndInifinity_ReadAsDoubleAsync() + { + const string testJson = @"[ + NaN, + Infinity, + -Infinity +]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(testJson)); + + Assert.IsTrue(await reader.ReadAsync()); + + Assert.AreEqual(double.NaN, reader.ReadAsDouble()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(double.NaN, reader.Value); + + Assert.AreEqual(double.PositiveInfinity, reader.ReadAsDouble()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(double.PositiveInfinity, reader.Value); + + Assert.AreEqual(double.NegativeInfinity, reader.ReadAsDouble()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(double.NegativeInfinity, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task Float_NaNAndInifinity_ReadAsStringAsync() + { + const string testJson = @"[ + NaN, + Infinity, + -Infinity +]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(testJson)); + + Assert.IsTrue(await reader.ReadAsync()); + + Assert.AreEqual(JsonConvert.NaN, reader.ReadAsString()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual(JsonConvert.NaN, reader.Value); + + Assert.AreEqual(JsonConvert.PositiveInfinity, reader.ReadAsString()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual(JsonConvert.PositiveInfinity, reader.Value); + + Assert.AreEqual(JsonConvert.NegativeInfinity, reader.ReadAsString()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual(JsonConvert.NegativeInfinity, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task FloatParseHandling_ReadAsStringAsync() + { + string json = "[9223372036854775807, 1.7976931348623157E+308, 792281625142643375935439503.35, 792281625142643375935555555555555555555555555555555555555555555555555439503.35]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + reader.FloatParseHandling = FloatParseHandling.Decimal; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.AreEqual("9223372036854775807", reader.ReadAsString()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("9223372036854775807", reader.Value); + + Assert.AreEqual("1.7976931348623157E+308", reader.ReadAsString()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("1.7976931348623157E+308", reader.Value); + + Assert.AreEqual("792281625142643375935439503.35", reader.ReadAsString()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("792281625142643375935439503.35", reader.Value); + + Assert.AreEqual("792281625142643375935555555555555555555555555555555555555555555555555439503.35", reader.ReadAsString()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("792281625142643375935555555555555555555555555555555555555555555555555439503.35", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + } + + [Test] + public async Task FloatParseHandlingAsync() + { + string json = "[1.0,1,9.9,1E-06]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + reader.FloatParseHandling = FloatParseHandling.Decimal; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(1.0m, reader.Value); + Assert.AreEqual(typeof(decimal), reader.ValueType); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(1L, reader.Value); + Assert.AreEqual(typeof(long), reader.ValueType); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(9.9m, reader.Value); + Assert.AreEqual(typeof(decimal), reader.ValueType); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(Convert.ToDecimal(1E-06), reader.Value); + Assert.AreEqual(typeof(decimal), reader.ValueType); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + } + + [Test] + public async Task FloatParseHandling_NaNAsync() + { + string json = "[NaN]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + reader.FloatParseHandling = FloatParseHandling.Decimal; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsync(), "Cannot read NaN value. Path '', line 1, position 4."); + } + + [Test] + public async Task FloatingPointNonFiniteNumbersAsync() + { + string input = @"[ + NaN, + Infinity, + -Infinity +]"; + + StringReader sr = new StringReader(input); + + using (JsonReader jsonReader = new JsonTextReader(sr)) + { + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.StartArray); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.Float); + Assert.AreEqual(jsonReader.Value, double.NaN); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.Float); + Assert.AreEqual(jsonReader.Value, double.PositiveInfinity); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.Float); + Assert.AreEqual(jsonReader.Value, double.NegativeInfinity); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.EndArray); + } + } + + [Test] + public async Task ReadFloatingPointNumberAsync() + { + string json = + @"[0.0,0.0,0.1,1.0,1.000001,1E-06,4.94065645841247E-324,Infinity,-Infinity,NaN,1.7976931348623157E+308,-1.7976931348623157E+308,Infinity,-Infinity,NaN,0e-10,0.25e-5,0.3e10]"; + + using (JsonReader jsonReader = new JsonTextReader(new StringReader(json))) + { + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.StartArray, jsonReader.TokenType); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(0.0, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(0.0, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(0.1, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(1.0, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(1.000001, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(1E-06, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(4.94065645841247E-324, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(double.PositiveInfinity, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(double.NegativeInfinity, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(double.NaN, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(double.MaxValue, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(double.MinValue, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(double.PositiveInfinity, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(double.NegativeInfinity, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(double.NaN, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(0d, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(0.0000025d, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, jsonReader.TokenType); + Assert.AreEqual(3000000000d, jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.EndArray, jsonReader.TokenType); + } + } + } +} + +#endif diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/MiscAsyncTests.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/MiscAsyncTests.cs new file mode 100644 index 00000000..a05893b9 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/MiscAsyncTests.cs @@ -0,0 +1,990 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +using System.Globalization; +using System.Text; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif +using System.IO; +using System.Threading.Tasks; +using Newtonsoft.Json.Tests.TestObjects.JsonTextReaderTests; +using Newtonsoft.Json.Utilities; + +namespace Newtonsoft.Json.Tests.JsonTextReaderTests +{ + [TestFixture] +#if !DNXCORE50 + [Category("JsonTextReaderTests")] +#endif + public class MiscAsyncTests : TestFixtureBase + { + [Test] + public async Task LineInfoAndNewLinesAsync() + { + string json = "{}"; + + JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, jsonTextReader.TokenType); + Assert.AreEqual(1, jsonTextReader.LineNumber); + Assert.AreEqual(1, jsonTextReader.LinePosition); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + + Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType); + Assert.AreEqual(1, jsonTextReader.LineNumber); + Assert.AreEqual(2, jsonTextReader.LinePosition); + + json = "\n{\"a\":\"bc\"}"; + + jsonTextReader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, jsonTextReader.TokenType); + Assert.AreEqual(2, jsonTextReader.LineNumber); + Assert.AreEqual(1, jsonTextReader.LinePosition); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType); + Assert.AreEqual(2, jsonTextReader.LineNumber); + Assert.AreEqual(5, jsonTextReader.LinePosition); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.String, jsonTextReader.TokenType); + Assert.AreEqual(2, jsonTextReader.LineNumber); + Assert.AreEqual(9, jsonTextReader.LinePosition); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType); + Assert.AreEqual(2, jsonTextReader.LineNumber); + Assert.AreEqual(10, jsonTextReader.LinePosition); + + json = "\n{\"a\":\n\"bc\",\"d\":true\n}"; + + jsonTextReader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, jsonTextReader.TokenType); + Assert.AreEqual(2, jsonTextReader.LineNumber); + Assert.AreEqual(1, jsonTextReader.LinePosition); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType); + Assert.AreEqual(2, jsonTextReader.LineNumber); + Assert.AreEqual(5, jsonTextReader.LinePosition); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.String, jsonTextReader.TokenType); + Assert.AreEqual(3, jsonTextReader.LineNumber); + Assert.AreEqual(4, jsonTextReader.LinePosition); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType); + Assert.AreEqual(3, jsonTextReader.LineNumber); + Assert.AreEqual(9, jsonTextReader.LinePosition); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.Boolean, jsonTextReader.TokenType); + Assert.AreEqual(3, jsonTextReader.LineNumber); + Assert.AreEqual(13, jsonTextReader.LinePosition); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType); + Assert.AreEqual(4, jsonTextReader.LineNumber); + Assert.AreEqual(1, jsonTextReader.LinePosition); + } + + [Test] + public async Task UnescapeDoubleQuotesAsync() + { + string json = @"{""recipe_id"":""12"",""recipe_name"":""Apocalypse Leather Armors"",""recipe_text"":""#C16------------------------------\r\n#C12Ingredients #C20\r\n#C16------------------------------\r\n\r\na piece of Leather Armor\r\n( ie #L \""Enhanced Leather Armor Boots\"" \""85644\"" )\r\n\r\n\r\n#L \""Hacker Tool\"" \""87814\""\r\n\r\n\r\n#L \""Clanalizer\"" \""208313\""\r\n\r\n\r\n#C16------------------------------\r\n#C12Recipe #C16\r\n#C16------------------------------#C20\r\n\r\nHacker Tool\r\n#C15+#C20\r\na piece of Leather Armor\r\n#C15=#C20\r\n\r\na piece of Hacked Leather Armor\r\n( ie : #L \""Hacked Leather Armor Boots\"" \""245979\"" )\r\n#C16Skills: | BE |#C20\r\n\r\n#C14------------------------------#C20\r\n\r\nClanalizer\r\n#C15+#C20\r\na piece of Hacked Leather Armor\r\n#C15=#C20\r\n\r\na piece of Apocalypse Leather Armor\r\n( ie : #L \""Apocalypse Leather Armor Boots\"" \""245966\"" )\r\n#C16Skills: | ?? |#C20\r\n\r\n#C16------------------------------\r\n#C12Details#C16\r\n#C16------------------------------#C20\r\n\r\n#L \""Apocalypse Leather Armor Boots\"" \""245967\""\r\n#L \""Apocalypse Leather Armor Gloves\"" \""245969\""\r\n#L \""Apocalypse Leather Armor Helmet\"" \""245975\""\r\n#L \""Apocalypse Leather Armor Pants\"" \""245971\""\r\n#L \""Apocalypse Leather Armor Sleeves\"" \""245973\""\r\n#L \""Apocalypse Leather Body Armor\"" \""245965\""\r\n\r\n#C16------------------------------\r\n#C12Comments#C16\r\n#C16------------------------------#C20\r\n\r\nNice froob armor.. but ugleh!\r\n\r\n"",""recipe_author"":null}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("recipe_text", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + + Assert.AreEqual("#C16------------------------------\r\n#C12Ingredients #C20\r\n#C16------------------------------\r\n\r\na piece of Leather Armor\r\n( ie #L \"Enhanced Leather Armor Boots\" \"85644\" )\r\n\r\n\r\n#L \"Hacker Tool\" \"87814\"\r\n\r\n\r\n#L \"Clanalizer\" \"208313\"\r\n\r\n\r\n#C16------------------------------\r\n#C12Recipe #C16\r\n#C16------------------------------#C20\r\n\r\nHacker Tool\r\n#C15+#C20\r\na piece of Leather Armor\r\n#C15=#C20\r\n\r\na piece of Hacked Leather Armor\r\n( ie : #L \"Hacked Leather Armor Boots\" \"245979\" )\r\n#C16Skills: | BE |#C20\r\n\r\n#C14------------------------------#C20\r\n\r\nClanalizer\r\n#C15+#C20\r\na piece of Hacked Leather Armor\r\n#C15=#C20\r\n\r\na piece of Apocalypse Leather Armor\r\n( ie : #L \"Apocalypse Leather Armor Boots\" \"245966\" )\r\n#C16Skills: | ?? |#C20\r\n\r\n#C16------------------------------\r\n#C12Details#C16\r\n#C16------------------------------#C20\r\n\r\n#L \"Apocalypse Leather Armor Boots\" \"245967\"\r\n#L \"Apocalypse Leather Armor Gloves\" \"245969\"\r\n#L \"Apocalypse Leather Armor Helmet\" \"245975\"\r\n#L \"Apocalypse Leather Armor Pants\" \"245971\"\r\n#L \"Apocalypse Leather Armor Sleeves\" \"245973\"\r\n#L \"Apocalypse Leather Body Armor\" \"245965\"\r\n\r\n#C16------------------------------\r\n#C12Comments#C16\r\n#C16------------------------------#C20\r\n\r\nNice froob armor.. but ugleh!\r\n\r\n", reader.Value); + } + + [Test] + public async Task SurrogatePairValidAsync() + { + string json = @"{ ""MATHEMATICAL ITALIC CAPITAL ALPHA"": ""\uD835\uDEE2"" }"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + + string s = reader.Value.ToString(); + Assert.AreEqual(2, s.Length); + + StringInfo stringInfo = new StringInfo(s); + Assert.AreEqual(1, stringInfo.LengthInTextElements); + } + + [Test] + public async Task SurrogatePairReplacementAsync() + { + // existing good surrogate pair + Assert.AreEqual("ABC \ud800\udc00 DEF", await ReadStringAsync("ABC \\ud800\\udc00 DEF")); + + // invalid surrogates (two high back-to-back) + Assert.AreEqual("ABC \ufffd\ufffd DEF", await ReadStringAsync("ABC \\ud800\\ud800 DEF")); + + // invalid surrogates (two high back-to-back) + Assert.AreEqual("ABC \ufffd\ufffd\u1234 DEF", await ReadStringAsync("ABC \\ud800\\ud800\\u1234 DEF")); + + // invalid surrogates (three high back-to-back) + Assert.AreEqual("ABC \ufffd\ufffd\ufffd DEF", await ReadStringAsync("ABC \\ud800\\ud800\\ud800 DEF")); + + // invalid surrogates (high followed by a good surrogate pair) + Assert.AreEqual("ABC \ufffd\ud800\udc00 DEF", await ReadStringAsync("ABC \\ud800\\ud800\\udc00 DEF")); + + // invalid high surrogate at end of string + Assert.AreEqual("ABC \ufffd", await ReadStringAsync("ABC \\ud800")); + + // high surrogate not followed by low surrogate + Assert.AreEqual("ABC \ufffd DEF", await ReadStringAsync("ABC \\ud800 DEF")); + + // low surrogate not preceded by high surrogate + Assert.AreEqual("ABC \ufffd\ufffd DEF", await ReadStringAsync("ABC \\udc00\\ud800 DEF")); + + // make sure unencoded invalid surrogate characters don't make it through + Assert.AreEqual("\ufffd\ufffd\ufffd", await ReadStringAsync("\udc00\ud800\ud800")); + + Assert.AreEqual("ABC \ufffd\b", await ReadStringAsync("ABC \\ud800\\b")); + Assert.AreEqual("ABC \ufffd ", await ReadStringAsync("ABC \\ud800 ")); + Assert.AreEqual("ABC \b\ufffd", await ReadStringAsync("ABC \\b\\ud800")); + } + + private async Task ReadStringAsync(string input) + { + MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(@"""" + input + @"""")); + + JsonTextReader reader = new JsonTextReader(new StreamReader(ms)); + await reader.ReadAsync(); + + return (string)reader.Value; + } + + [Test] + public async Task YahooFinanceAsync() + { + string input = @"{ +""matches"" : [ +{""t"":""C"", ""n"":""Citigroup Inc."", ""e"":""NYSE"", ""id"":""662713""} +,{""t"":""CHL"", ""n"":""China Mobile Ltd. (ADR)"", ""e"":""NYSE"", ""id"":""660998""} +,{""t"":""PTR"", ""n"":""PetroChina Company Limited (ADR)"", ""e"":""NYSE"", ""id"":""664536""} +,{""t"":""RIO"", ""n"":""Companhia Vale do Rio Doce (ADR)"", ""e"":""NYSE"", ""id"":""671472""} +,{""t"":""RIOPR"", ""n"":""Companhia Vale do Rio Doce (ADR)"", ""e"":""NYSE"", ""id"":""3512643""} +,{""t"":""CSCO"", ""n"":""Cisco Systems, Inc."", ""e"":""NASDAQ"", ""id"":""99624""} +,{""t"":""CVX"", ""n"":""Chevron Corporation"", ""e"":""NYSE"", ""id"":""667226""} +,{""t"":""TM"", ""n"":""Toyota Motor Corporation (ADR)"", ""e"":""NYSE"", ""id"":""655880""} +,{""t"":""JPM"", ""n"":""JPMorgan Chase \\x26 Co."", ""e"":""NYSE"", ""id"":""665639""} +,{""t"":""COP"", ""n"":""ConocoPhillips"", ""e"":""NYSE"", ""id"":""1691168""} +,{""t"":""LFC"", ""n"":""China Life Insurance Company Ltd. (ADR)"", ""e"":""NYSE"", ""id"":""688679""} +,{""t"":""NOK"", ""n"":""Nokia Corporation (ADR)"", ""e"":""NYSE"", ""id"":""657729""} +,{""t"":""KO"", ""n"":""The Coca-Cola Company"", ""e"":""NYSE"", ""id"":""6550""} +,{""t"":""VZ"", ""n"":""Verizon Communications Inc."", ""e"":""NYSE"", ""id"":""664887""} +,{""t"":""AMX"", ""n"":""America Movil S.A.B de C.V. (ADR)"", ""e"":""NYSE"", ""id"":""665834""}], +""all"" : false +} +"; + + using (JsonReader jsonReader = new JsonTextReader(new StringReader(input))) + { + while (await jsonReader.ReadAsync()) + { + } + } + } + + [Test] + public async Task DepthAsync() + { + string input = @"{ + value:'Purple', + array:[1,2,new Date(1)], + subobject:{prop:1,proparray:[1]} +}"; + + StringReader sr = new StringReader(input); + + using (JsonReader reader = new JsonTextReader(sr)) + { + Assert.AreEqual(0, reader.Depth); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.StartObject); + Assert.AreEqual(0, reader.Depth); + Assert.AreEqual("", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.PropertyName); + Assert.AreEqual(1, reader.Depth); + Assert.AreEqual("value", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.String); + Assert.AreEqual(reader.Value, @"Purple"); + Assert.AreEqual(reader.QuoteChar, '\''); + Assert.AreEqual(1, reader.Depth); + Assert.AreEqual("value", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.PropertyName); + Assert.AreEqual(1, reader.Depth); + Assert.AreEqual("array", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.StartArray); + Assert.AreEqual(1, reader.Depth); + Assert.AreEqual("array", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.Integer); + Assert.AreEqual(1L, reader.Value); + Assert.AreEqual(2, reader.Depth); + Assert.AreEqual("array[0]", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.Integer); + Assert.AreEqual(2L, reader.Value); + Assert.AreEqual(2, reader.Depth); + Assert.AreEqual("array[1]", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.StartConstructor); + Assert.AreEqual("Date", reader.Value); + Assert.AreEqual(2, reader.Depth); + Assert.AreEqual("array[2]", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.Integer); + Assert.AreEqual(1L, reader.Value); + Assert.AreEqual(3, reader.Depth); + Assert.AreEqual("array[2][0]", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.EndConstructor); + Assert.AreEqual(null, reader.Value); + Assert.AreEqual(2, reader.Depth); + Assert.AreEqual("array[2]", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.EndArray); + Assert.AreEqual(1, reader.Depth); + Assert.AreEqual("array", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.PropertyName); + Assert.AreEqual(1, reader.Depth); + Assert.AreEqual("subobject", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.StartObject); + Assert.AreEqual(1, reader.Depth); + Assert.AreEqual("subobject", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.PropertyName); + Assert.AreEqual(2, reader.Depth); + Assert.AreEqual("subobject.prop", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.Integer); + Assert.AreEqual(2, reader.Depth); + Assert.AreEqual("subobject.prop", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.PropertyName); + Assert.AreEqual(2, reader.Depth); + Assert.AreEqual("subobject.proparray", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.StartArray); + Assert.AreEqual(2, reader.Depth); + Assert.AreEqual("subobject.proparray", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.Integer); + Assert.AreEqual(3, reader.Depth); + Assert.AreEqual("subobject.proparray[0]", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.EndArray); + Assert.AreEqual(2, reader.Depth); + Assert.AreEqual("subobject.proparray", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.EndObject); + Assert.AreEqual(1, reader.Depth); + Assert.AreEqual("subobject", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(reader.TokenType, JsonToken.EndObject); + Assert.AreEqual(0, reader.Depth); + Assert.AreEqual("", reader.Path); + } + } + + [Test] + public async Task AppendCharsWhileReadingNullAsync() + { + string json = @"[ + { + ""$id"": ""1"", + ""Name"": ""e1"", + ""Manager"": null + }, + { + ""$id"": ""2"", + ""Name"": ""e2"", + ""Manager"": null + }, + { + ""$ref"": ""1"" + }, + { + ""$ref"": ""2"" + } +]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); +#if DEBUG + reader.SetCharBuffer(new char[129]); +#endif + + for (int i = 0; i < 15; i++) + { + await reader.ReadAsync(); + } + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Null, reader.TokenType); + } + + [Test] + public async Task AppendCharsWhileReadingNewLineAsync() + { + string json = @" +{ + ""description"": ""A person"", + ""type"": ""object"", + ""properties"": + { + ""name"": {""type"":""string""}, + ""hobbies"": { + ""type"": ""array"", + ""items"": {""type"":""string""} + } + } +} +"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); +#if DEBUG + reader.SetCharBuffer(new char[129]); +#endif + + for (int i = 0; i < 14; i++) + { + Assert.IsTrue(await reader.ReadAsync()); + } + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("type", reader.Value); + } + + [Test] + public async Task BufferTestAsync() + { + string json = @"{ + ""CPU"": ""Intel"", + ""Description"": ""Amazing!\nBuy now!"", + ""Drives"": [ + ""DVD read/writer"", + ""500 gigabyte hard drive"", + ""Amazing Drive" + new string('!', 9000) + @""" + ] + }"; + + FakeArrayPool arrayPool = new FakeArrayPool(); + + for (int i = 0; i < 1000; i++) + { + using (JsonTextReader reader = new JsonTextReader(new StringReader(json))) + { + reader.ArrayPool = arrayPool; + + while (await reader.ReadAsync()) + { + } + } + + if ((i + 1) % 100 == 0) + { + Console.WriteLine("Allocated buffers: " + arrayPool.FreeArrays.Count); + } + } + + Assert.AreEqual(0, arrayPool.UsedArrays.Count); + Assert.AreEqual(6, arrayPool.FreeArrays.Count); + } + + [Test] + public async Task BufferTest_WithErrorAsync() + { + string json = @"{ + ""CPU"": ""Intel?\nYes"", + ""Description"": ""Amazin"; + + FakeArrayPool arrayPool = new FakeArrayPool(); + + try + { + // dispose will free used buffers + using (JsonTextReader reader = new JsonTextReader(new StringReader(json))) + { + reader.ArrayPool = arrayPool; + + while (await reader.ReadAsync()) + { + } + } + + Assert.Fail(); + } + catch + { + } + + Assert.AreEqual(0, arrayPool.UsedArrays.Count); + Assert.AreEqual(2, arrayPool.FreeArrays.Count); + } + + [Test] + public async Task WriteReadWriteAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw) + { + Formatting = Formatting.Indented + }) + { + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync(true); + + await jsonWriter.WriteStartObjectAsync(); + await jsonWriter.WritePropertyNameAsync("integer"); + await jsonWriter.WriteValueAsync(99); + await jsonWriter.WritePropertyNameAsync("string"); + await jsonWriter.WriteValueAsync("how now brown cow?"); + await jsonWriter.WritePropertyNameAsync("array"); + + await jsonWriter.WriteStartArrayAsync(); + for (int i = 0; i < 5; i++) + { + await jsonWriter.WriteValueAsync(i); + } + + await jsonWriter.WriteStartObjectAsync(); + await jsonWriter.WritePropertyNameAsync("decimal"); + await jsonWriter.WriteValueAsync(990.00990099m); + await jsonWriter.WriteEndObjectAsync(); + + await jsonWriter.WriteValueAsync(5); + await jsonWriter.WriteEndArrayAsync(); + + await jsonWriter.WriteEndObjectAsync(); + + await jsonWriter.WriteValueAsync("This is a string."); + await jsonWriter.WriteNullAsync(); + await jsonWriter.WriteNullAsync(); + await jsonWriter.WriteEndArrayAsync(); + } + + string json = sb.ToString(); + + JsonSerializer serializer = new JsonSerializer(); + + object jsonObject = serializer.Deserialize(new JsonTextReader(new StringReader(json))); + + sb = new StringBuilder(); + sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw) + { + Formatting = Formatting.Indented + }) + { + serializer.Serialize(jsonWriter, jsonObject); + } + + Assert.AreEqual(json, sb.ToString()); + } + + [Test] + public async Task LongStringTestAsync() + { + int length = 20000; + string json = @"[""" + new string(' ', length) + @"""]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual(typeof(string), reader.ValueType); + Assert.AreEqual(20000, reader.Value.ToString().Length); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task EscapedUnicodeTextAsync() + { + string json = @"[""\u003c"",""\u5f20""]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); +#if DEBUG + reader.SetCharBuffer(new char[2]); +#endif + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + await reader.ReadAsync(); + Assert.AreEqual("<", reader.Value); + + await reader.ReadAsync(); + Assert.AreEqual(24352, Convert.ToInt32(Convert.ToChar((string)reader.Value))); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + } + + [Test] + public async Task SupportMultipleContentAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"{'prop1':[1]} 1 2 ""name"" [][]null {}{} 1.1")); + reader.SupportMultipleContent = true; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Null, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task SingleLineCommentsAsync() + { + string json = @"//comment*//*hi*/ +{//comment +Name://comment +true//comment after true" + StringUtils.CarriageReturn + + @",//comment after comma" + StringUtils.CarriageReturnLineFeed + + @"""ExpiryDate""://comment" + StringUtils.LineFeed + + @"new " + StringUtils.LineFeed + + @"Date +(//comment +null//comment +), + ""Price"": 3.99, + ""Sizes"": //comment +[//comment + + ""Small""//comment +]//comment +}//comment +//comment 1 "; + + JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual("comment*//*hi*/", reader.Value); + Assert.AreEqual(1, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(2, reader.LineNumber); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual(2, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("Name", reader.Value); + Assert.AreEqual(3, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual(3, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Boolean, reader.TokenType); + Assert.AreEqual(true, reader.Value); + Assert.AreEqual(4, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual("comment after true", reader.Value); + Assert.AreEqual(4, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual("comment after comma", reader.Value); + Assert.AreEqual(5, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("ExpiryDate", reader.Value); + Assert.AreEqual(6, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual(6, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartConstructor, reader.TokenType); + Assert.AreEqual(9, reader.LineNumber); + Assert.AreEqual("Date", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Null, reader.TokenType); + Assert.AreEqual(10, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual(10, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndConstructor, reader.TokenType); + Assert.AreEqual(11, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("Price", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("Sizes", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual("comment ", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual("comment 1 ", reader.Value); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task JustSinglelineCommentAsync() + { + string json = @"//comment"; + + JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual("comment", reader.Value); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ScientificNotationAsync() + { + double d = Convert.ToDouble("6.0221418e23", CultureInfo.InvariantCulture); + + Assert.AreEqual("6,0221418E+23", d.ToString(new CultureInfo("fr-FR"))); + Assert.AreEqual("602214180000000000000000", d.ToString("0.#############################################################################")); + + string json = @"[0e-10,0E-10,0.25e-5,0.3e10,6.0221418e23]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await reader.ReadAsync(); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(0d, reader.Value); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(0d, reader.Value); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(0.0000025d, reader.Value); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(3000000000d, reader.Value); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(602214180000000000000000d, reader.Value); + + await reader.ReadAsync(); + + reader = new JsonTextReader(new StringReader(json)); + + await reader.ReadAsync(); + + await reader.ReadAsDecimalAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(0m, reader.Value); + + await reader.ReadAsDecimalAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(0m, reader.Value); + + await reader.ReadAsDecimalAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(0.0000025m, reader.Value); + + await reader.ReadAsDecimalAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(3000000000m, reader.Value); + + await reader.ReadAsDecimalAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(602214180000000000000000m, reader.Value); + + await reader.ReadAsync(); + } + + [Test] + public async Task WriteReadBoundaryDecimalsAsync() + { + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw); + + await writer.WriteStartArrayAsync(); + await writer.WriteValueAsync(decimal.MaxValue); + await writer.WriteValueAsync(decimal.MinValue); + await writer.WriteEndArrayAsync(); + + string json = sw.ToString(); + + StringReader sr = new StringReader(json); + JsonTextReader reader = new JsonTextReader(sr); + + Assert.IsTrue(await reader.ReadAsync()); + + decimal? max = await reader.ReadAsDecimalAsync(); + Assert.AreEqual(decimal.MaxValue, max); + + decimal? min = await reader.ReadAsDecimalAsync(); + Assert.AreEqual(decimal.MinValue, min); + + Assert.IsTrue(await reader.ReadAsync()); + } + +#if !DNXCORE50 + [Test] + public async Task LinePositionOnNewLineAsync() + { + string json1 = "{'a':'bc'}"; + + JsonTextReader r = new JsonTextReader(new StringReader(json1)); + + Assert.IsTrue(await r.ReadAsync()); + Assert.AreEqual(1, r.LineNumber); + Assert.AreEqual(1, r.LinePosition); + + Assert.IsTrue(await r.ReadAsync()); + Assert.AreEqual(1, r.LineNumber); + Assert.AreEqual(5, r.LinePosition); + + Assert.IsTrue(await r.ReadAsync()); + Assert.AreEqual(1, r.LineNumber); + Assert.AreEqual(9, r.LinePosition); + + Assert.IsTrue(await r.ReadAsync()); + Assert.AreEqual(1, r.LineNumber); + Assert.AreEqual(10, r.LinePosition); + + Assert.IsFalse(await r.ReadAsync()); + + string json2 = "\n{'a':'bc'}"; + + r = new JsonTextReader(new StringReader(json2)); + + Assert.IsTrue(await r.ReadAsync()); + Assert.AreEqual(2, r.LineNumber); + Assert.AreEqual(1, r.LinePosition); + + Assert.IsTrue(await r.ReadAsync()); + Assert.AreEqual(2, r.LineNumber); + Assert.AreEqual(5, r.LinePosition); + + Assert.IsTrue(await r.ReadAsync()); + Assert.AreEqual(2, r.LineNumber); + Assert.AreEqual(9, r.LinePosition); + + Assert.IsTrue(await r.ReadAsync()); + Assert.AreEqual(2, r.LineNumber); + Assert.AreEqual(10, r.LinePosition); + + Assert.IsFalse(await r.ReadAsync()); + } +#endif + } +} + +#endif diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ParseAsyncTests.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ParseAsyncTests.cs new file mode 100644 index 00000000..5225eaaa --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ParseAsyncTests.cs @@ -0,0 +1,492 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +using System.Text; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif +using System.IO; +using System.Threading.Tasks; +using Newtonsoft.Json.Tests.TestObjects.JsonTextReaderTests; +using Newtonsoft.Json.Utilities; + +namespace Newtonsoft.Json.Tests.JsonTextReaderTests +{ + [TestFixture] +#if !DNXCORE50 + [Category("JsonTextReaderTests")] +#endif + public class ParseAsyncTests : TestFixtureBase + { + [Test] + public async Task ParseAdditionalContent_WhitespaceAsync() + { + string json = @"[ +""Small"", +""Medium"", +""Large"" +] + +"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + while (await reader.ReadAsync()) + { + } + } + + [Test] + public async Task ParsingQuotedPropertyWithControlCharactersAsync() + { + JsonReader reader = new JsonTextReader(new StringReader(@"{'hi\r\nbye':1}")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("hi\r\nbye", reader.Value); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + Assert.AreEqual(1L, reader.Value); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ParseIntegersAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("1")); + Assert.AreEqual(1, await reader.ReadAsInt32Async()); + + reader = new JsonTextReader(new StringReader("-1")); + Assert.AreEqual(-1, await reader.ReadAsInt32Async()); + + reader = new JsonTextReader(new StringReader("0")); + Assert.AreEqual(0, await reader.ReadAsInt32Async()); + + reader = new JsonTextReader(new StringReader("-0")); + Assert.AreEqual(0, await reader.ReadAsInt32Async()); + + reader = new JsonTextReader(new StringReader(int.MaxValue.ToString())); + Assert.AreEqual(int.MaxValue, await reader.ReadAsInt32Async()); + + reader = new JsonTextReader(new StringReader(int.MinValue.ToString())); + Assert.AreEqual(int.MinValue, await reader.ReadAsInt32Async()); + + reader = new JsonTextReader(new StringReader(long.MaxValue.ToString())); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsInt32Async(), "JSON integer 9223372036854775807 is too large or small for an Int32. Path '', line 1, position 19."); + + reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsInt32Async(), "Unexpected character encountered while parsing number: s. Path '', line 1, position 77."); + + reader = new JsonTextReader(new StringReader("1E-06")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsInt32Async(), "Input string '1E-06' is not a valid integer. Path '', line 1, position 5."); + + reader = new JsonTextReader(new StringReader("1.1")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsInt32Async(), "Input string '1.1' is not a valid integer. Path '', line 1, position 3."); + + reader = new JsonTextReader(new StringReader("")); + Assert.AreEqual(null, await reader.ReadAsInt32Async()); + + reader = new JsonTextReader(new StringReader("-")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsInt32Async(), "Input string '-' is not a valid integer. Path '', line 1, position 1."); + } + + [Test] + public async Task ParseDecimalsAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("1.1")); + Assert.AreEqual(1.1m, await reader.ReadAsDecimalAsync()); + + reader = new JsonTextReader(new StringReader("-1.1")); + Assert.AreEqual(-1.1m, await reader.ReadAsDecimalAsync()); + + reader = new JsonTextReader(new StringReader("0.0")); + Assert.AreEqual(0.0m, await reader.ReadAsDecimalAsync()); + + reader = new JsonTextReader(new StringReader("-0.0")); + Assert.AreEqual(0, await reader.ReadAsDecimalAsync()); + + reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsDecimalAsync(), "Unexpected character encountered while parsing number: s. Path '', line 1, position 77."); + + reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd")); + reader.FloatParseHandling = FloatParseHandling.Decimal; + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsync(), "Unexpected character encountered while parsing number: s. Path '', line 1, position 77."); + + reader = new JsonTextReader(new StringReader("1E-06")); + Assert.AreEqual(0.000001m, await reader.ReadAsDecimalAsync()); + + reader = new JsonTextReader(new StringReader("")); + Assert.AreEqual(null, await reader.ReadAsDecimalAsync()); + + reader = new JsonTextReader(new StringReader("-")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsDecimalAsync(), "Input string '-' is not a valid decimal. Path '', line 1, position 1."); + } + + [Test] + public async Task ParseDoublesAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("1.1")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(typeof(double), reader.ValueType); + Assert.AreEqual(1.1d, reader.Value); + + reader = new JsonTextReader(new StringReader("-1.1")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(typeof(double), reader.ValueType); + Assert.AreEqual(-1.1d, reader.Value); + + reader = new JsonTextReader(new StringReader("0.0")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(typeof(double), reader.ValueType); + Assert.AreEqual(0.0d, reader.Value); + + reader = new JsonTextReader(new StringReader("-0.0")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(typeof(double), reader.ValueType); + Assert.AreEqual(-0.0d, reader.Value); + + reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsync(), "Unexpected character encountered while parsing number: s. Path '', line 1, position 77."); + + reader = new JsonTextReader(new StringReader("1E-06")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(typeof(double), reader.ValueType); + Assert.AreEqual(0.000001d, reader.Value); + + reader = new JsonTextReader(new StringReader("")); + Assert.IsFalse(await reader.ReadAsync()); + + reader = new JsonTextReader(new StringReader("-")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsync(), "Input string '-' is not a valid number. Path '', line 1, position 1."); + + reader = new JsonTextReader(new StringReader("1.7976931348623157E+308")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(typeof(double), reader.ValueType); + Assert.AreEqual(Double.MaxValue, reader.Value); + + reader = new JsonTextReader(new StringReader("-1.7976931348623157E+308")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(typeof(double), reader.ValueType); + Assert.AreEqual(Double.MinValue, reader.Value); + + reader = new JsonTextReader(new StringReader("1E+309")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsync(), "Input string '1E+309' is not a valid number. Path '', line 1, position 6."); + + reader = new JsonTextReader(new StringReader("-1E+5000")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsync(), "Input string '-1E+5000' is not a valid number. Path '', line 1, position 8."); + + reader = new JsonTextReader(new StringReader("5.1231231E")); + await ExceptionAssert.ThrowsAsync(async () => await reader.ReadAsync(), "Input string '5.1231231E' is not a valid number. Path '', line 1, position 10."); + + reader = new JsonTextReader(new StringReader("1E-23")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(typeof(double), reader.ValueType); + Assert.AreEqual(1e-23, reader.Value); + } + + [Test] + public async Task ParseArrayWithMissingValuesAsync() + { + string json = "[,,, \n\r\n \0 \r , , ]"; + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Undefined, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Undefined, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Undefined, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Undefined, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Undefined, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + } + + [Test] + public async Task ParseBooleanWithNoExtraContentAsync() + { + string json = "[true "; + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ParseContentDelimitedByNonStandardWhitespaceAsync() + { + string json = "\x00a0{\x00a0'h\x00a0i\x00a0'\x00a0:\x00a0[\x00a0true\x00a0,\x00a0new\x00a0Date\x00a0(\x00a0)\x00a0]\x00a0/*\x00a0comment\x00a0*/\x00a0}\x00a0"; + JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Boolean, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartConstructor, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndConstructor, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ParseObjectWithNoEndAsync() + { + string json = "{hi:1, "; + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ParseEmptyArrayAsync() + { + string json = "[]"; + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + } + + [Test] + public async Task ParseEmptyObjectAsync() + { + string json = "{}"; + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task ParseEmptyConstructorAsync() + { + string json = "new Date()"; + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartConstructor, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndConstructor, reader.TokenType); + } + + [Test] + public async Task ParseHexNumberAsync() + { + string json = @"0x20"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await reader.ReadAsDecimalAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(32m, reader.Value); + } + + [Test] + public async Task ParseNumbersAsync() + { + string json = @"[0,1,2 , 3]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + + await reader.ReadAsync(); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + } + + [Test] + public async Task ParseLineFeedDelimitedConstructorAsync() + { + string json = "new Date\n()"; + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual("Date", reader.Value); + Assert.AreEqual(JsonToken.StartConstructor, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndConstructor, reader.TokenType); + } + + [Test] + public async Task ParseNullStringConstructorAsync() + { + string json = "new Date\0()"; + JsonTextReader reader = new JsonTextReader(new StringReader(json)); +#if DEBUG + reader.SetCharBuffer(new char[7]); +#endif + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual("Date", reader.Value); + Assert.AreEqual(JsonToken.StartConstructor, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndConstructor, reader.TokenType); + } + + [Test] + public async Task ParseOctalNumberAsync() + { + string json = @"010"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await reader.ReadAsDecimalAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(8m, reader.Value); + } + + [Test] + public async Task DateParseHandlingAsync() + { + string json = @"[""1970-01-01T00:00:00Z"",""\/Date(0)\/""]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + reader.DateParseHandling = DateParseHandling.DateTime; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value); + Assert.AreEqual(typeof(DateTime), reader.ValueType); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value); + Assert.AreEqual(typeof(DateTime), reader.ValueType); + Assert.IsTrue(await reader.ReadAsync()); + + reader = new JsonTextReader(new StringReader(json)); + reader.DateParseHandling = DateParseHandling.DateTimeOffset; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value); + Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value); + Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); + Assert.IsTrue(await reader.ReadAsync()); + + reader = new JsonTextReader(new StringReader(json)); + reader.DateParseHandling = DateParseHandling.None; + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(@"1970-01-01T00:00:00Z", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(@"/Date(0)/", reader.Value); + Assert.AreEqual(typeof(string), reader.ValueType); + Assert.IsTrue(await reader.ReadAsync()); + + reader = new JsonTextReader(new StringReader(json)); + reader.DateParseHandling = DateParseHandling.DateTime; + + Assert.IsTrue(await reader.ReadAsync()); + await reader.ReadAsDateTimeOffsetAsync(); + Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value); + Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); + await reader.ReadAsDateTimeOffsetAsync(); + Assert.AreEqual(new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero), reader.Value); + Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); + Assert.IsTrue(await reader.ReadAsync()); + + reader = new JsonTextReader(new StringReader(json)); + reader.DateParseHandling = DateParseHandling.DateTimeOffset; + + Assert.IsTrue(await reader.ReadAsync()); + await reader.ReadAsDateTimeAsync(); + Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value); + Assert.AreEqual(typeof(DateTime), reader.ValueType); + await reader.ReadAsDateTimeAsync(); + Assert.AreEqual(new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc), reader.Value); + Assert.AreEqual(typeof(DateTime), reader.ValueType); + Assert.IsTrue(await reader.ReadAsync()); + } + } +} + +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadAsyncTests.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadAsyncTests.cs new file mode 100644 index 00000000..6bdd831a --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadAsyncTests.cs @@ -0,0 +1,1687 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +using System.Globalization; +using Newtonsoft.Json.Linq; +#if !PORTABLE || NETSTANDARD1_1 +using System.Numerics; +#endif +using System.Text; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif +using System.IO; +using System.Net; +using System.Net.Sockets; +using System.Threading; +using System.Threading.Tasks; +using System.Linq; +using Newtonsoft.Json.Tests.TestObjects.JsonTextReaderTests; +using Newtonsoft.Json.Utilities; + +namespace Newtonsoft.Json.Tests.JsonTextReaderTests +{ + [TestFixture] +#if !DNXCORE50 + [Category("JsonTextReaderTests")] +#endif + public class ReadAsyncTests : TestFixtureBase + { +#if !(PORTABLE || PORTABLE40 || NET35 || NET20) + [Test] + public async Task ReadAsBooleanAsync() + { + string json = @"[ + 1, + 0, + 1.1, + 0.0, + 0.000000000001, + 9999999999, + -9999999999, + 9999999999999999999999999999999999999999999999999999999999999999999999, + -9999999999999999999999999999999999999999999999999999999999999999999999, + 'true', + 'TRUE', + 'false', + 'FALSE', + // comment! + /* comment! */ + '', + null +]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); +#if DEBUG + reader.SetCharBuffer(new char[10]); +#endif + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual("", reader.Path); + + Assert.AreEqual(true, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[0]", reader.Path); + + Assert.AreEqual(false, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[1]", reader.Path); + + Assert.AreEqual(true, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[2]", reader.Path); + + Assert.AreEqual(false, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[3]", reader.Path); + + Assert.AreEqual(true, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[4]", reader.Path); + + Assert.AreEqual(true, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[5]", reader.Path); + + Assert.AreEqual(true, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[6]", reader.Path); + + Assert.AreEqual(true, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[7]", reader.Path); + + Assert.AreEqual(true, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[8]", reader.Path); + + Assert.AreEqual(true, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[9]", reader.Path); + + Assert.AreEqual(true, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[10]", reader.Path); + + Assert.AreEqual(false, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[11]", reader.Path); + + Assert.AreEqual(false, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[12]", reader.Path); + + Assert.AreEqual(null, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[13]", reader.Path); + + Assert.AreEqual(null, await reader.ReadAsBooleanAsync()); + Assert.AreEqual("[14]", reader.Path); + + Assert.AreEqual(null, await reader.ReadAsBooleanAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + Assert.AreEqual("", reader.Path); + + Assert.AreEqual(null, await reader.ReadAsBooleanAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + Assert.AreEqual("", reader.Path); + } +#endif + + [Test] + public async Task ReadAsBoolean_NullCharAsync() + { + string json = '\0' + @"true" + '\0' + '\0'; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.AreEqual(true, await reader.ReadAsBooleanAsync()); + Assert.AreEqual(null, await reader.ReadAsBooleanAsync()); + } + + [Test] + public async Task ReadAsBytesAsync() + { + byte[] data = Encoding.UTF8.GetBytes("Hello world"); + + string json = @"""" + Convert.ToBase64String(data) + @""""; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + byte[] result = await reader.ReadAsBytesAsync(); + + CollectionAssert.AreEquivalent(data, result); + } + + [Test] + public async Task ReadAsBooleanNoContentAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"")); + + Assert.IsNull(await reader.ReadAsBooleanAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadAsBytesIntegerArrayWithCommentsAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"[/*hi*/1/*hi*/,2/*hi*/]")); + + byte[] data = await reader.ReadAsBytesAsync(); + Assert.AreEqual(2, data.Length); + Assert.AreEqual(1, data[0]); + Assert.AreEqual(2, data[1]); + } + + [Test] + public async Task ReadUnicodeAsync() + { + string json = @"{""Message"":""Hi,I\u0092ve send you smth""}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); +#if DEBUG + reader.SetCharBuffer(new char[5]); +#endif + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("Message", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual(@"Hi,I" + '\u0092' + "ve send you smth", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ReadHexidecimalWithAllLettersAsync() + { + string json = @"{""text"":0xabcdef12345}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + Assert.AreEqual(11806310474565, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task ReadSingleBytesAsync() + { + StringReader s = new StringReader(@"""SGVsbG8gd29ybGQu"""); + JsonTextReader reader = new JsonTextReader(s); + + byte[] data = await reader.ReadAsBytesAsync(); + Assert.IsNotNull(data); + + string text = Encoding.UTF8.GetString(data, 0, data.Length); + Assert.AreEqual("Hello world.", text); + } + + [Test] + public async Task ReadOctalNumberAsync() + { + StringReader s = new StringReader(@"[0372, 0xFA, 0XFA]"); + JsonTextReader jsonReader = new JsonTextReader(s); + + Assert.IsTrue(await jsonReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, jsonReader.TokenType); + + Assert.IsTrue(await jsonReader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType); + Assert.AreEqual(250L, jsonReader.Value); + + Assert.IsTrue(await jsonReader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType); + Assert.AreEqual(250L, jsonReader.Value); + + Assert.IsTrue(await jsonReader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType); + Assert.AreEqual(250L, jsonReader.Value); + + Assert.IsTrue(await jsonReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, jsonReader.TokenType); + + Assert.IsFalse(await jsonReader.ReadAsync()); + } + + [Test] + public async Task ReadOctalNumberAsInt64Async() + { + StringReader s = new StringReader(@"[0372, 0xFA, 0XFA]"); + JsonTextReader jsonReader = new JsonTextReader(s); + + Assert.IsTrue(await jsonReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, jsonReader.TokenType); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType); + Assert.AreEqual(typeof(long), jsonReader.ValueType); + Assert.AreEqual(250L, (long)jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType); + Assert.AreEqual(typeof(long), jsonReader.ValueType); + Assert.AreEqual(250L, (long)jsonReader.Value); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType); + Assert.AreEqual(typeof(long), jsonReader.ValueType); + Assert.AreEqual(250L, (long)jsonReader.Value); + + Assert.IsTrue(await jsonReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, jsonReader.TokenType); + + Assert.IsFalse(await jsonReader.ReadAsync()); + } + + [Test] + public async Task ReadOctalNumberAsInt32Async() + { + StringReader s = new StringReader(@"[0372, 0xFA, 0XFA]"); + JsonTextReader jsonReader = new JsonTextReader(s); + + Assert.IsTrue(await jsonReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, jsonReader.TokenType); + + await jsonReader.ReadAsInt32Async(); + Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType); + Assert.AreEqual(typeof(int), jsonReader.ValueType); + Assert.AreEqual(250, jsonReader.Value); + + await jsonReader.ReadAsInt32Async(); + Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType); + Assert.AreEqual(typeof(int), jsonReader.ValueType); + Assert.AreEqual(250, jsonReader.Value); + + await jsonReader.ReadAsInt32Async(); + Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType); + Assert.AreEqual(typeof(int), jsonReader.ValueType); + Assert.AreEqual(250, jsonReader.Value); + + Assert.IsTrue(await jsonReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, jsonReader.TokenType); + + Assert.IsFalse(await jsonReader.ReadAsync()); + } + + [Test] + public async Task ReadAsDecimalNoContentAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"")); + + Assert.IsNull(await reader.ReadAsDecimalAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadAsBytesNoContentAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"")); + + Assert.IsNull(await reader.ReadAsBytesAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadAsDateTimeOffsetNoContentAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader(@"")); + + Assert.IsNull(await reader.ReadAsDateTimeOffsetAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadAsDateTimeOffsetAsync() + { + string json = "{\"Offset\":\"\\/Date(946663200000+0600)\\/\"}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await reader.ReadAsDateTimeOffsetAsync(); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); + Assert.AreEqual(new DateTimeOffset(new DateTime(2000, 1, 1), TimeSpan.FromHours(6)), reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task ReadAsDateTimeOffsetNegativeAsync() + { + string json = @"{""Offset"":""\/Date(946706400000-0600)\/""}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await reader.ReadAsDateTimeOffsetAsync(); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); + Assert.AreEqual(new DateTimeOffset(new DateTime(2000, 1, 1), TimeSpan.FromHours(-6)), reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task ReadAsDateTimeOffsetBadStringAsync() + { + string json = @"{""Offset"":""blablahbla""}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsDateTimeOffsetAsync(); + }, "Could not convert string to DateTimeOffset: blablahbla. Path 'Offset', line 1, position 22."); + } + + [Test] + public async Task ReadAsDateTimeOffsetHoursOnlyAsync() + { + string json = "{\"Offset\":\"\\/Date(946663200000+06)\\/\"}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await reader.ReadAsDateTimeOffsetAsync(); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); + Assert.AreEqual(new DateTimeOffset(new DateTime(2000, 1, 1), TimeSpan.FromHours(6)), reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task ReadAsDateTimeOffsetWithMinutesAsync() + { + string json = @"{""Offset"":""\/Date(946708260000-0631)\/""}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await reader.ReadAsDateTimeOffsetAsync(); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); + Assert.AreEqual(new DateTimeOffset(new DateTime(2000, 1, 1), TimeSpan.FromHours(-6).Add(TimeSpan.FromMinutes(-31))), reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task ReadAsDateTimeOffsetIsoDateAsync() + { + string json = @"{""Offset"":""2011-08-01T21:25Z""}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await reader.ReadAsDateTimeOffsetAsync(); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); + Assert.AreEqual(new DateTimeOffset(new DateTime(2011, 8, 1, 21, 25, 0, DateTimeKind.Utc), TimeSpan.Zero), reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task ReadAsDateTimeOffsetUnitedStatesDateAsync() + { + string json = @"{""Offset"":""1/30/2011""}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + reader.Culture = new CultureInfo("en-US"); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await reader.ReadAsDateTimeOffsetAsync(); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); + + DateTimeOffset dt = (DateTimeOffset)reader.Value; + Assert.AreEqual(new DateTime(2011, 1, 30, 0, 0, 0, DateTimeKind.Unspecified), dt.DateTime); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task ReadAsDateTimeOffsetNewZealandDateAsync() + { + string json = @"{""Offset"":""30/1/2011""}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + reader.Culture = new CultureInfo("en-NZ"); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await reader.ReadAsDateTimeOffsetAsync(); + Assert.AreEqual(JsonToken.Date, reader.TokenType); + Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType); + + DateTimeOffset dt = (DateTimeOffset)reader.Value; + Assert.AreEqual(new DateTime(2011, 1, 30, 0, 0, 0, DateTimeKind.Unspecified), dt.DateTime); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task ReadAsDecimalIntAsync() + { + string json = @"{""Name"":1}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await reader.ReadAsDecimalAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(typeof(decimal), reader.ValueType); + Assert.AreEqual(1m, reader.Value); + } + + [Test] + public async Task ReadAsIntDecimalAsync() + { + string json = @"{""Name"": 1.1}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsInt32Async(); + }, "Input string '1.1' is not a valid integer. Path 'Name', line 1, position 12."); + } + + [Test] + public async Task ReadAsDecimalAsync() + { + string json = @"{""decimal"":-7.92281625142643E+28}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + decimal? d = await reader.ReadAsDecimalAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(typeof(decimal), reader.ValueType); + Assert.AreEqual(-79228162514264300000000000000m, d); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task ReadAsDecimalFrenchAsync() + { + string json = @"{""decimal"":""9,99""}"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + reader.Culture = new CultureInfo("fr-FR"); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + decimal? d = await reader.ReadAsDecimalAsync(); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(typeof(decimal), reader.ValueType); + Assert.AreEqual(9.99m, d); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + } + + [Test] + public async Task ReadBufferOnControlCharAsync() + { + string json = @"[ + { + ""Name"": ""Jim"", + ""BirthDate"": ""\/Date(978048000000)\/"", + ""LastModified"": ""\/Date(978048000000)\/"" + }, + { + ""Name"": ""Jim"", + ""BirthDate"": ""\/Date(978048000000)\/"", + ""LastModified"": ""\/Date(978048000000)\/"" + } +]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); +#if DEBUG + reader.SetCharBuffer(new char[5]); +#endif + + for (int i = 0; i < 13; i++) + { + await reader.ReadAsync(); + } + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(new DateTime(631136448000000000), reader.Value); + } + + [Test] + public async Task ReadBufferOnEndCommentAsync() + { + string json = @"/*comment*/ { /*comment*/ + ""Name"": /*comment*/ ""Apple"" /*comment*/, /*comment*/ + ""ExpiryDate"": ""\/Date(1230422400000)\/"", + ""Price"": 3.99, + ""Sizes"": /*comment*/ [ /*comment*/ + ""Small"", /*comment*/ + ""Medium"" /*comment*/, + /*comment*/ ""Large"" + /*comment*/ ] /*comment*/ + } /*comment*/"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); +#if DEBUG + reader.SetCharBuffer(new char[5]); +#endif + + for (int i = 0; i < 26; i++) + { + Assert.IsTrue(await reader.ReadAsync()); + } + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ReadAsDouble_NullAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("null")); + Assert.AreEqual(null, await reader.ReadAsDoubleAsync()); + } + + [Test] + public async Task ReadAsDouble_SuccessAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("'12.34'")); + Assert.AreEqual(12.34d, await reader.ReadAsDoubleAsync()); + } + + [Test] + public async Task ReadAsDouble_HexAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("0XCAFEBABE")); + Assert.AreEqual(3405691582d, await reader.ReadAsDoubleAsync()); + } + + [Test] + public async Task ReadAsDouble_AllowThousandsAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("'1,112.34'")); + Assert.AreEqual(1112.34d, await reader.ReadAsDoubleAsync()); + } + + [Test] + public async Task ReadAsDouble_FailureAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("['Trump',1]")); + + Assert.IsTrue(await reader.ReadAsync()); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsDoubleAsync(); + }, "Could not convert string to double: Trump. Path '[0]', line 1, position 8."); + + Assert.AreEqual(1d, await reader.ReadAsDoubleAsync()); + Assert.IsTrue(await reader.ReadAsync()); + } + + [Test] + public async Task ReadAsString_BooleanAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("{\"Test1\":false}")); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + + string s = await reader.ReadAsStringAsync(); + Assert.AreEqual("false", s); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task Read_Boolean_FailureAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("{\"Test1\":false1}")); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsync(); + }, "Error parsing boolean value. Path 'Test1', line 1, position 14."); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ReadAsString_Boolean_FailureAsync() + { + JsonTextReader reader = new JsonTextReader(new StringReader("{\"Test1\":false1}")); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + + await ExceptionAssert.ThrowsAsync(async () => + { + await reader.ReadAsStringAsync(); + }, "Unexpected character encountered while parsing value: 1. Path 'Test1', line 1, position 14."); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ReadValue_EmptyString_PositionAsync() + { + string json = @"['','','','','','','']"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + await reader.ReadAsync(); + await reader.ReadAsInt32Async(); + Assert.AreEqual("[0]", reader.Path); + await reader.ReadAsDecimalAsync(); + Assert.AreEqual("[1]", reader.Path); + await reader.ReadAsDateTimeAsync(); + Assert.AreEqual("[2]", reader.Path); + await reader.ReadAsDateTimeOffsetAsync(); + Assert.AreEqual("[3]", reader.Path); + await reader.ReadAsStringAsync(); + Assert.AreEqual("[4]", reader.Path); + await reader.ReadAsBytesAsync(); + Assert.AreEqual("[5]", reader.Path); + await reader.ReadAsDoubleAsync(); + Assert.AreEqual("[6]", reader.Path); + + Assert.IsNull(await reader.ReadAsStringAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsNull(await reader.ReadAsStringAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + + Assert.IsNull(await reader.ReadAsBytesAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadValueCommentsAsync() + { + string json = @"/*comment*/[/*comment*/1/*comment*/,2,/*comment*//*comment*/""three""/*comment*/,""four""/*comment*/,null,/*comment*/null,3.99,1.1/*comment*/,''/*comment*/]/*comment*/"; + + JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.AreEqual(1, await reader.ReadAsInt32Async()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + + Assert.AreEqual(2, await reader.ReadAsInt32Async()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + + Assert.AreEqual("three", await reader.ReadAsStringAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + + Assert.AreEqual("four", await reader.ReadAsStringAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + + Assert.AreEqual(null, await reader.ReadAsStringAsync()); + Assert.AreEqual(JsonToken.Null, reader.TokenType); + + Assert.AreEqual(null, await reader.ReadAsInt32Async()); + Assert.AreEqual(JsonToken.Null, reader.TokenType); + + Assert.AreEqual(3.99m, await reader.ReadAsDecimalAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + + Assert.AreEqual(1.1m, await reader.ReadAsDecimalAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + + CollectionAssert.AreEquivalent(new byte[0], await reader.ReadAsBytesAsync()); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.AreEqual(null, await reader.ReadAsInt32Async()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadContentDelimitedByCommentsAsync() + { + string json = @"/*comment*/{/*comment*/Name:/*comment*/true/*comment*/,/*comment*/ + ""ExpiryDate"":/*comment*/new +" + StringUtils.LineFeed + @"Date +(/*comment*/null/*comment*/), + ""Price"": 3.99, + ""Sizes"":/*comment*/[/*comment*/ + ""Small""/*comment*/]/*comment*/}/*comment*/"; + + JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("Name", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Boolean, reader.TokenType); + Assert.AreEqual(true, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + Assert.AreEqual("ExpiryDate", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartConstructor, reader.TokenType); + Assert.AreEqual(5, reader.LineNumber); + Assert.AreEqual("Date", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Null, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndConstructor, reader.TokenType); + } + + [Test] + public async Task ReadNullIntLineNumberAndPositionAsync() + { + string json = @"[ + 1, + 2, + 3, + null +]"; + + JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); + + await reader.ReadAsync(); + Assert.AreEqual(1, reader.LineNumber); + + await reader.ReadAsInt32Async(); + Assert.AreEqual(2, reader.LineNumber); + Assert.AreEqual("[0]", reader.Path); + + await reader.ReadAsInt32Async(); + Assert.AreEqual(3, reader.LineNumber); + Assert.AreEqual("[1]", reader.Path); + + await reader.ReadAsInt32Async(); + Assert.AreEqual(4, reader.LineNumber); + Assert.AreEqual("[2]", reader.Path); + + await reader.ReadAsInt32Async(); + Assert.AreEqual(5, reader.LineNumber); + Assert.AreEqual("[3]", reader.Path); + + await reader.ReadAsync(); + Assert.AreEqual(6, reader.LineNumber); + Assert.AreEqual(string.Empty, reader.Path); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ReadingFromSlowStreamAsync() + { + string json = "[false, true, true, false, 'test!', 1.11, 0e-10, 0E-10, 0.25e-5, 0.3e10, 6.0221418e23, 'Purple\\r \\n monkey\\'s:\\tdishwasher']"; + + JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); + + Assert.IsTrue(await reader.ReadAsync()); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(false, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Boolean, reader.TokenType); + Assert.AreEqual(true, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Boolean, reader.TokenType); + Assert.AreEqual(true, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Boolean, reader.TokenType); + Assert.AreEqual(false, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual("test!", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(1.11d, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(0d, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(0d, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(0.0000025d, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(3000000000d, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Float, reader.TokenType); + Assert.AreEqual(602214180000000000000000d, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + Assert.AreEqual(reader.Value, "Purple\r \n monkey's:\tdishwasher"); + + Assert.IsTrue(await reader.ReadAsync()); + } + +#if !DNXCORE50 + [Test] + [Ignore] + public async Task ReadFromNetworkStreamAsync() + { + const int port = 11999; + const int jsonArrayElementsCount = 193; + + var serverStartedEvent = new ManualResetEvent(false); + var clientReceivedEvent = new ManualResetEvent(false); + + ThreadPool.QueueUserWorkItem(work => + { + var server = new TcpListener(IPAddress.Parse("0.0.0.0"), port); + server.Start(); + + serverStartedEvent.Set(); + + var serverSocket = server.AcceptSocket(); + + var jsonString = "[\r\n" + String.Join(",", Enumerable.Repeat(" \"testdata\"\r\n", jsonArrayElementsCount).ToArray()) + "]"; + var bytes = new UTF8Encoding().GetBytes(jsonString); + serverSocket.Send(bytes); + Console.WriteLine("server send: " + bytes.Length); + + clientReceivedEvent.WaitOne(); + + }); + + serverStartedEvent.WaitOne(); + + var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + socket.Blocking = false; + socket.Connect("127.0.0.1", port); + + var stream = new NetworkStream(socket); + + int i = 0; + using (var sr = new StreamReader(stream, new UTF8Encoding(), false)) + using (var jsonTextReader = new JsonTextReader(sr)) + { + while (await jsonTextReader.ReadAsync()) + { + i++; + + if (i == 193) + { + string s = string.Empty; + } + + Console.WriteLine($"{i} - {jsonTextReader.TokenType} - {jsonTextReader.Value}"); + } + + //var result = serializer.Deserialize(jsonTextReader).ToString(); + //Console.WriteLine("client receive: " + new UTF8Encoding().GetBytes(result).Length); + } + + clientReceivedEvent.Set(); + + Console.WriteLine("Done"); + } +#endif + + [Test] + public async Task ReadCommentInsideArrayAsync() + { + string json = @"{ + ""projects"": [ + ""src"", + //"" + ""test"" + ] +}"; + + JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json)); + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.String, jsonTextReader.TokenType); + Assert.AreEqual("src", jsonTextReader.Value); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, jsonTextReader.TokenType); + Assert.AreEqual(@"""", jsonTextReader.Value); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.String, jsonTextReader.TokenType); + Assert.AreEqual("test", jsonTextReader.Value); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType); + } + + [Test] + public async Task ReadAsBytes_Base64AndGuidAsync() + { + JsonTextReader jsonTextReader = new JsonTextReader(new StringReader("'AAAAAAAAAAAAAAAAAAAAAAAAAAABAAAA'")); + byte[] data = await jsonTextReader.ReadAsBytesAsync(); + byte[] expected = Convert.FromBase64String("AAAAAAAAAAAAAAAAAAAAAAAAAAABAAAA"); + + CollectionAssert.AreEqual(expected, data); + + jsonTextReader = new JsonTextReader(new StringReader("'AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAABAAAA'")); + data = await jsonTextReader.ReadAsBytesAsync(); + expected = new Guid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAABAAAA").ToByteArray(); + + CollectionAssert.AreEqual(expected, data); + } + + [Test] + public async Task ReadSingleQuoteInsideDoubleQuoteStringAsync() + { + string json = @"{""NameOfStore"":""Forest's Bakery And Cafe""}"; + + JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json)); + await jsonTextReader.ReadAsync(); + await jsonTextReader.ReadAsync(); + await jsonTextReader.ReadAsync(); + + Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value); + } + + [Test] + public async Task ReadMultilineStringAsync() + { + string json = @"""first line +second line +third line"""; + + JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.String, jsonTextReader.TokenType); + + Assert.AreEqual(@"first line +second line +third line", jsonTextReader.Value); + } + +#if !PORTABLE || NETSTANDARD1_1 + [Test] + public async Task ReadBigIntegerAsync() + { + string json = @"{ + ParentId: 1, + ChildId: 333333333333333333333333333333333333333, +}"; + + JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, jsonTextReader.TokenType); + Assert.AreEqual(typeof(BigInteger), jsonTextReader.ValueType); + Assert.AreEqual(BigInteger.Parse("333333333333333333333333333333333333333"), jsonTextReader.Value); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType); + + Assert.IsFalse(await jsonTextReader.ReadAsync()); + + JObject o = JObject.Parse(json); + var i = (BigInteger)((JValue)o["ChildId"]).Value; + Assert.AreEqual(BigInteger.Parse("333333333333333333333333333333333333333"), i); + } +#endif + + [Test] + public async Task ReadBadMSDateAsStringAsync() + { + string json = @"{ + ChildId: '\/Date(9467082_PIE_340000-0631)\/' +}"; + + JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json)); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.String, jsonTextReader.TokenType); + Assert.AreEqual(@"/Date(9467082_PIE_340000-0631)/", jsonTextReader.Value); + + Assert.IsTrue(await jsonTextReader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType); + + Assert.IsFalse(await jsonTextReader.ReadAsync()); + } + + [Test] + public async Task ReadConstructorAsync() + { + string json = @"{""DefaultConverter"":new Date(0, ""hi""),""MemberConverter"":""1970-01-01T00:00:00Z""}"; + + JsonReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartConstructor, reader.TokenType); + Assert.AreEqual("Date", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(0L, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual("hi", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndConstructor, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual("MemberConverter", reader.Value); + } + + [Test] + public async Task ReadingIndentedAsync() + { + string input = @"{ + CPU: 'Intel', + Drives: [ + 'DVD read/writer', + ""500 gigabyte hard drive"" + ] +}"; + + StringReader sr = new StringReader(input); + + using (JsonTextReader jsonReader = new JsonTextReader(sr)) + { +#if DEBUG + jsonReader.SetCharBuffer(new char[5]); +#endif + + Assert.AreEqual(jsonReader.TokenType, JsonToken.None); + Assert.AreEqual(0, jsonReader.LineNumber); + Assert.AreEqual(0, jsonReader.LinePosition); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.StartObject); + Assert.AreEqual(1, jsonReader.LineNumber); + Assert.AreEqual(1, jsonReader.LinePosition); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName); + Assert.AreEqual(jsonReader.Value, "CPU"); + Assert.AreEqual(2, jsonReader.LineNumber); + Assert.AreEqual(6, jsonReader.LinePosition); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.String, jsonReader.TokenType); + Assert.AreEqual("Intel", jsonReader.Value); + Assert.AreEqual(2, jsonReader.LineNumber); + Assert.AreEqual(14, jsonReader.LinePosition); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.PropertyName); + Assert.AreEqual(jsonReader.Value, "Drives"); + Assert.AreEqual(3, jsonReader.LineNumber); + Assert.AreEqual(9, jsonReader.LinePosition); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.StartArray); + Assert.AreEqual(3, jsonReader.LineNumber); + Assert.AreEqual(11, jsonReader.LinePosition); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.String); + Assert.AreEqual(jsonReader.Value, "DVD read/writer"); + Assert.AreEqual(jsonReader.QuoteChar, '\''); + Assert.AreEqual(4, jsonReader.LineNumber); + Assert.AreEqual(21, jsonReader.LinePosition); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.String); + Assert.AreEqual(jsonReader.Value, "500 gigabyte hard drive"); + Assert.AreEqual(jsonReader.QuoteChar, '"'); + Assert.AreEqual(5, jsonReader.LineNumber); + Assert.AreEqual(29, jsonReader.LinePosition); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.EndArray); + Assert.AreEqual(6, jsonReader.LineNumber); + Assert.AreEqual(3, jsonReader.LinePosition); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.EndObject); + Assert.AreEqual(7, jsonReader.LineNumber); + Assert.AreEqual(1, jsonReader.LinePosition); + + Assert.IsFalse(await jsonReader.ReadAsync()); + } + } + + [Test] + public async Task ReadLongStringAsync() + { + string s = new string('a', 10000); + JsonReader reader = new JsonTextReader(new StringReader("'" + s + "'")); + await reader.ReadAsync(); + + Assert.AreEqual(s, reader.Value); + } + + [Test] + public async Task ReadLongJsonArrayAsync() + { + int valueCount = 10000; + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw); + writer.WriteStartArray(); + for (int i = 0; i < valueCount; i++) + { + writer.WriteValue(i); + } + + writer.WriteEndArray(); + + string json = sw.ToString(); + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + Assert.IsTrue(await reader.ReadAsync()); + for (int i = 0; i < valueCount; i++) + { + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual((long)i, reader.Value); + } + + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task NullCharReadingAsync() + { + string json = "\0{\0'\0h\0i\0'\0:\0[\01\0,\0'\0'\0\0,\0null\0]\0,\0do\0:true\0}\0\0/*\0sd\0f\0*/\0/*\0sd\0f\0*/ \0"; + JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.String, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Null, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.PropertyName, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Boolean, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual("\0sd\0f\0", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Comment, reader.TokenType); + Assert.AreEqual("\0sd\0f\0", reader.Value); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ReadNullTerminatorStringsAsync() + { + JsonReader reader = new JsonTextReader(new StringReader("'h\0i'")); + Assert.IsTrue(await reader.ReadAsync()); + + Assert.AreEqual("h\0i", reader.Value); + } + + [Test] + public async Task ReadBytesNoStartWithUnexpectedEndAsync() + { + JsonReader reader = new JsonTextReader(new StringReader(@"[ ")); + Assert.IsTrue(await reader.ReadAsync()); + + Assert.IsNull(await reader.ReadAsBytesAsync()); + Assert.AreEqual(JsonToken.None, reader.TokenType); + } + + [Test] + public async Task ReadNewLinesAsync() + { + string newLinesText = StringUtils.CarriageReturn + StringUtils.CarriageReturnLineFeed + StringUtils.LineFeed + StringUtils.CarriageReturnLineFeed + " " + StringUtils.CarriageReturn + StringUtils.CarriageReturnLineFeed; + + string json = newLinesText + "{" + newLinesText + "'" + newLinesText + "name1" + newLinesText + "'" + newLinesText + ":" + newLinesText + "[" + newLinesText + "new" + newLinesText + "Date" + newLinesText + "(" + newLinesText + "1" + newLinesText + "," + newLinesText + "null" + newLinesText + "/*" + newLinesText + "blah comment" + newLinesText + "*/" + newLinesText + ")" + newLinesText + "," + newLinesText + "1.1111" + newLinesText + "]" + newLinesText + "," + newLinesText + "name2" + newLinesText + ":" + newLinesText + "{" + newLinesText + "}" + newLinesText + "}" + newLinesText; + + int count = 0; + StringReader sr = new StringReader(newLinesText); + while (sr.ReadLine() != null) + { + count++; + } + + JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1))); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(7, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(31, reader.LineNumber); + Assert.AreEqual(newLinesText + "name1" + newLinesText, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(37, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(55, reader.LineNumber); + Assert.AreEqual(JsonToken.StartConstructor, reader.TokenType); + Assert.AreEqual("Date", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(61, reader.LineNumber); + Assert.AreEqual(1L, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(73, reader.LineNumber); + Assert.AreEqual(null, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(91, reader.LineNumber); + Assert.AreEqual(newLinesText + "blah comment" + newLinesText, reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(97, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(109, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(115, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(133, reader.LineNumber); + Assert.AreEqual("name2", reader.Value); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(139, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(145, reader.LineNumber); + + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(151, reader.LineNumber); + } + + [Test] + public async Task ReadBytesFollowingNumberInArrayAsync() + { + string helloWorld = "Hello world!"; + byte[] helloWorldData = Encoding.UTF8.GetBytes(helloWorld); + + JsonReader reader = new JsonTextReader(new StringReader(@"[1,'" + Convert.ToBase64String(helloWorldData) + @"']")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartArray, reader.TokenType); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + byte[] data = await reader.ReadAsBytesAsync(); + CollectionAssert.AreEquivalent(helloWorldData, data); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndArray, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ReadBytesFollowingNumberInObjectAsync() + { + string helloWorld = "Hello world!"; + byte[] helloWorldData = Encoding.UTF8.GetBytes(helloWorld); + + JsonReader reader = new JsonTextReader(new StringReader(@"{num:1,data:'" + Convert.ToBase64String(helloWorldData) + @"'}")); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.StartObject, reader.TokenType); + Assert.IsTrue(await reader.ReadAsync()); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.Integer, reader.TokenType); + Assert.IsTrue(await reader.ReadAsync()); + byte[] data = await reader.ReadAsBytesAsync(); + CollectionAssert.AreEquivalent(helloWorldData, data); + Assert.AreEqual(JsonToken.Bytes, reader.TokenType); + Assert.IsTrue(await reader.ReadAsync()); + Assert.AreEqual(JsonToken.EndObject, reader.TokenType); + + Assert.IsFalse(await reader.ReadAsync()); + } + + [Test] + public async Task ReadingEscapedStringsAsync() + { + string input = "{value:'Purple\\r \\n monkey\\'s:\\tdishwasher'}"; + + StringReader sr = new StringReader(input); + + using (JsonReader jsonReader = new JsonTextReader(sr)) + { + Assert.AreEqual(0, jsonReader.Depth); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType); + Assert.AreEqual(0, jsonReader.Depth); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType); + Assert.AreEqual(1, jsonReader.Depth); + + await jsonReader.ReadAsync(); + Assert.AreEqual(jsonReader.TokenType, JsonToken.String); + Assert.AreEqual("Purple\r \n monkey's:\tdishwasher", jsonReader.Value); + Assert.AreEqual('\'', jsonReader.QuoteChar); + Assert.AreEqual(1, jsonReader.Depth); + + await jsonReader.ReadAsync(); + Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType); + Assert.AreEqual(0, jsonReader.Depth); + } + } + + [Test] + public async Task ReadRandomJsonAsync() + { + string json = @"[ + true, + { + ""integer"": 99, + ""string"": ""how now brown cow?"", + ""array"": [ + 0, + 1, + 2, + 3, + 4, + { + ""decimal"": 990.00990099 + }, + 5 + ] + }, + ""This is a string."", + null, + null +]"; + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + while (await reader.ReadAsync()) + { + } + } + + [Test] + public void AsyncMethodsAlreadyCancelled() + { + CancellationTokenSource source = new CancellationTokenSource(); + CancellationToken token = source.Token; + source.Cancel(); + + JsonTextReader reader = new JsonTextReader(new StreamReader(Stream.Null)); + Assert.IsTrue(reader.ReadAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsBooleanAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsBytesAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsDateTimeAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsDateTimeOffsetAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsDecimalAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsInt32Async(token).IsCanceled); + Assert.IsTrue(reader.ReadAsStringAsync(token).IsCanceled); + } + + private class NoOverridesDerivedJsonTextAsync : JsonTextReader + { + public NoOverridesDerivedJsonTextAsync() + : base(new StreamReader(Stream.Null)) + { + } + } + + private class MinimalOverridesDerivedJsonReader : JsonReader + { + public override bool Read() => true; + } + + [Test] + public void AsyncMethodsAlreadyCancelledOnTextReaderSubclass() + { + CancellationTokenSource source = new CancellationTokenSource(); + CancellationToken token = source.Token; + source.Cancel(); + + JsonTextReader reader = new NoOverridesDerivedJsonTextAsync(); + Assert.IsTrue(reader.ReadAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsBooleanAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsBytesAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsDateTimeAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsDateTimeOffsetAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsDecimalAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsInt32Async(token).IsCanceled); + Assert.IsTrue(reader.ReadAsStringAsync(token).IsCanceled); + } + + [Test] + public void AsyncMethodsAlreadyCancelledOnReaderSubclass() + { + CancellationTokenSource source = new CancellationTokenSource(); + CancellationToken token = source.Token; + source.Cancel(); + + JsonReader reader = new MinimalOverridesDerivedJsonReader(); + Assert.IsTrue(reader.ReadAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsBooleanAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsBytesAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsDateTimeAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsDateTimeOffsetAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsDecimalAsync(token).IsCanceled); + Assert.IsTrue(reader.ReadAsInt32Async(token).IsCanceled); + Assert.IsTrue(reader.ReadAsStringAsync(token).IsCanceled); + } + } +} + +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterAsyncTests.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterAsyncTests.cs new file mode 100644 index 00000000..21e92f28 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterAsyncTests.cs @@ -0,0 +1,1837 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Newtonsoft.Json.Tests.TestObjects.JsonTextReaderTests; +using Newtonsoft.Json.Utilities; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif + +namespace Newtonsoft.Json.Tests +{ + [TestFixture] + public class JsonTextWriterAsyncTests : TestFixtureBase + { + [Test] + public async Task BufferTestAsync() + { + FakeArrayPool arrayPool = new FakeArrayPool(); + + string longString = new string('A', 2000); + string longEscapedString = "Hello!" + new string('!', 50) + new string('\n', 1000) + "Good bye!"; + string longerEscapedString = "Hello!" + new string('!', 2000) + new string('\n', 1000) + "Good bye!"; + + for (int i = 0; i < 1000; i++) + { + StringWriter sw = new StringWriter(CultureInfo.InvariantCulture); + + using (JsonTextWriter writer = new JsonTextWriter(sw)) + { + writer.ArrayPool = arrayPool; + + await writer.WriteStartObjectAsync(); + + await writer.WritePropertyNameAsync("Prop1"); + await writer.WriteValueAsync(new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc)); + + await writer.WritePropertyNameAsync("Prop2"); + await writer.WriteValueAsync(longString); + + await writer.WritePropertyNameAsync("Prop3"); + await writer.WriteValueAsync(longEscapedString); + + await writer.WritePropertyNameAsync("Prop4"); + await writer.WriteValueAsync(longerEscapedString); + + await writer.WriteEndObjectAsync(); + } + + if ((i + 1) % 100 == 0) + { + Console.WriteLine("Allocated buffers: " + arrayPool.FreeArrays.Count); + } + } + + Assert.AreEqual(0, arrayPool.UsedArrays.Count); + Assert.AreEqual(3, arrayPool.FreeArrays.Count); + } + + [Test] + public async Task BufferTest_WithErrorAsync() + { + FakeArrayPool arrayPool = new FakeArrayPool(); + + StringWriter sw = new StringWriter(CultureInfo.InvariantCulture); + + try + { + // dispose will free used buffers + using (JsonTextWriter writer = new JsonTextWriter(sw)) + { + writer.ArrayPool = arrayPool; + + await writer.WriteStartObjectAsync(); + + await writer.WritePropertyNameAsync("Prop1"); + await writer.WriteValueAsync(new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc)); + + await writer.WritePropertyNameAsync("Prop2"); + await writer.WriteValueAsync("This is an escaped \n string!"); + + await writer.WriteValueAsync("Error!"); + } + + Assert.Fail(); + } + catch + { + } + + Assert.AreEqual(0, arrayPool.UsedArrays.Count); + Assert.AreEqual(1, arrayPool.FreeArrays.Count); + } + + [Test] + public async Task NewLineAsync() + { + MemoryStream ms = new MemoryStream(); + + using (var streamWriter = new StreamWriter(ms, new UTF8Encoding(false)) { NewLine = "\n" }) + using (var jsonWriter = new JsonTextWriter(streamWriter) + { + CloseOutput = true, + Indentation = 2, + Formatting = Formatting.Indented + }) + { + await jsonWriter.WriteStartObjectAsync(); + await jsonWriter.WritePropertyNameAsync("prop"); + await jsonWriter.WriteValueAsync(true); + await jsonWriter.WriteEndObjectAsync(); + } + + byte[] data = ms.ToArray(); + + string json = Encoding.UTF8.GetString(data, 0, data.Length); + + Assert.AreEqual(@"{" + '\n' + @" ""prop"": true" + '\n' + "}", json); + } + + [Test] + public async Task QuoteNameAndStringsAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + JsonTextWriter writer = new JsonTextWriter(sw) { QuoteName = false }; + + await writer.WriteStartObjectAsync(); + + await writer.WritePropertyNameAsync("name"); + await writer.WriteValueAsync("value"); + + await writer.WriteEndObjectAsync(); + await writer.FlushAsync(); + + Assert.AreEqual(@"{name:""value""}", sb.ToString()); + } + + [Test] + public async Task CloseOutputAsync() + { + MemoryStream ms = new MemoryStream(); + JsonTextWriter writer = new JsonTextWriter(new StreamWriter(ms)); + + Assert.IsTrue(ms.CanRead); + await writer.CloseAsync(); + Assert.IsFalse(ms.CanRead); + + ms = new MemoryStream(); + writer = new JsonTextWriter(new StreamWriter(ms)) { CloseOutput = false }; + + Assert.IsTrue(ms.CanRead); + await writer.CloseAsync(); + Assert.IsTrue(ms.CanRead); + } + +#if !(PORTABLE) + [Test] + public async Task WriteIConvertableAsync() + { + var sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw); + await writer.WriteValueAsync(new ConvertibleInt(1)); + + Assert.AreEqual("1", sw.ToString()); + } +#endif + + [Test] + public async Task ValueFormattingAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync('@'); + await jsonWriter.WriteValueAsync("\r\n\t\f\b?{\\r\\n\"\'"); + await jsonWriter.WriteValueAsync(true); + await jsonWriter.WriteValueAsync(10); + await jsonWriter.WriteValueAsync(10.99); + await jsonWriter.WriteValueAsync(0.99); + await jsonWriter.WriteValueAsync(0.000000000000000001d); + await jsonWriter.WriteValueAsync(0.000000000000000001m); + await jsonWriter.WriteValueAsync((string)null); + await jsonWriter.WriteValueAsync((object)null); + await jsonWriter.WriteValueAsync("This is a string."); + await jsonWriter.WriteNullAsync(); + await jsonWriter.WriteUndefinedAsync(); + await jsonWriter.WriteEndArrayAsync(); + } + + string expected = @"[""@"",""\r\n\t\f\b?{\\r\\n\""'"",true,10,10.99,0.99,1E-18,0.000000000000000001,null,null,""This is a string."",null,undefined]"; + string result = sb.ToString(); + + Assert.AreEqual(expected, result); + } + + [Test] + public async Task NullableValueFormattingAsync() + { + StringWriter sw = new StringWriter(); + using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) + { + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync((char?)null); + await jsonWriter.WriteValueAsync((char?)'c'); + await jsonWriter.WriteValueAsync((bool?)null); + await jsonWriter.WriteValueAsync((bool?)true); + await jsonWriter.WriteValueAsync((byte?)null); + await jsonWriter.WriteValueAsync((byte?)1); + await jsonWriter.WriteValueAsync((sbyte?)null); + await jsonWriter.WriteValueAsync((sbyte?)1); + await jsonWriter.WriteValueAsync((short?)null); + await jsonWriter.WriteValueAsync((short?)1); + await jsonWriter.WriteValueAsync((ushort?)null); + await jsonWriter.WriteValueAsync((ushort?)1); + await jsonWriter.WriteValueAsync((int?)null); + await jsonWriter.WriteValueAsync((int?)1); + await jsonWriter.WriteValueAsync((uint?)null); + await jsonWriter.WriteValueAsync((uint?)1); + await jsonWriter.WriteValueAsync((long?)null); + await jsonWriter.WriteValueAsync((long?)1); + await jsonWriter.WriteValueAsync((ulong?)null); + await jsonWriter.WriteValueAsync((ulong?)1); + await jsonWriter.WriteValueAsync((double?)null); + await jsonWriter.WriteValueAsync((double?)1.1); + await jsonWriter.WriteValueAsync((float?)null); + await jsonWriter.WriteValueAsync((float?)1.1); + await jsonWriter.WriteValueAsync((decimal?)null); + await jsonWriter.WriteValueAsync((decimal?)1.1m); + await jsonWriter.WriteValueAsync((DateTime?)null); + await jsonWriter.WriteValueAsync((DateTime?)new DateTime(DateTimeUtils.InitialJavaScriptDateTicks, DateTimeKind.Utc)); + await jsonWriter.WriteValueAsync((DateTimeOffset?)null); + await jsonWriter.WriteValueAsync((DateTimeOffset?)new DateTimeOffset(DateTimeUtils.InitialJavaScriptDateTicks, TimeSpan.Zero)); + await jsonWriter.WriteEndArrayAsync(); + } + + string json = sw.ToString(); + string expected = @"[null,""c"",null,true,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1.1,null,1.1,null,1.1,null,""1970-01-01T00:00:00Z"",null,""1970-01-01T00:00:00+00:00""]"; + + Assert.AreEqual(expected, json); + } + + [Test] + public async Task WriteValueObjectWithNullableAsync() + { + StringWriter sw = new StringWriter(); + using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) + { + char? value = 'c'; + + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync((object)value); + await jsonWriter.WriteEndArrayAsync(); + } + + string json = sw.ToString(); + string expected = @"[""c""]"; + + Assert.AreEqual(expected, json); + } + + [Test] + public async Task WriteValueObjectWithUnsupportedValueAsync() + { + await ExceptionAssert.ThrowsAsync(async () => + { + StringWriter sw = new StringWriter(); + using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) + { + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync(new Version(1, 1, 1, 1)); + await jsonWriter.WriteEndArrayAsync(); + } + }, @"Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation. Path ''."); + } + + [Test] + public async Task StringEscapingAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync(@"""These pretzels are making me thirsty!"""); + await jsonWriter.WriteValueAsync("Jeff's house was burninated."); + await jsonWriter.WriteValueAsync("1. You don't talk about fight club.\r\n2. You don't talk about fight club."); + await jsonWriter.WriteValueAsync("35% of\t statistics\n are made\r up."); + await jsonWriter.WriteEndArrayAsync(); + } + + string expected = @"[""\""These pretzels are making me thirsty!\"""",""Jeff's house was burninated."",""1. You don't talk about fight club.\r\n2. You don't talk about fight club."",""35% of\t statistics\n are made\r up.""]"; + string result = sb.ToString(); + + Assert.AreEqual(expected, result); + } + + [Test] + public async Task WriteEndAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + + await jsonWriter.WriteStartObjectAsync(); + await jsonWriter.WritePropertyNameAsync("CPU"); + await jsonWriter.WriteValueAsync("Intel"); + await jsonWriter.WritePropertyNameAsync("PSU"); + await jsonWriter.WriteValueAsync("500W"); + await jsonWriter.WritePropertyNameAsync("Drives"); + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync("DVD read/writer"); + await jsonWriter.WriteCommentAsync("(broken)"); + await jsonWriter.WriteValueAsync("500 gigabyte hard drive"); + await jsonWriter.WriteValueAsync("200 gigabype hard drive"); + await jsonWriter.WriteEndObjectAsync(); + Assert.AreEqual(WriteState.Start, jsonWriter.WriteState); + } + + string expected = @"{ + ""CPU"": ""Intel"", + ""PSU"": ""500W"", + ""Drives"": [ + ""DVD read/writer"" + /*(broken)*/, + ""500 gigabyte hard drive"", + ""200 gigabype hard drive"" + ] +}"; + string result = sb.ToString(); + + StringAssert.AreEqual(expected, result); + } + + [Test] + public async Task CloseWithRemainingContentAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + + await jsonWriter.WriteStartObjectAsync(); + await jsonWriter.WritePropertyNameAsync("CPU"); + await jsonWriter.WriteValueAsync("Intel"); + await jsonWriter.WritePropertyNameAsync("PSU"); + await jsonWriter.WriteValueAsync("500W"); + await jsonWriter.WritePropertyNameAsync("Drives"); + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync("DVD read/writer"); + await jsonWriter.WriteCommentAsync("(broken)"); + await jsonWriter.WriteValueAsync("500 gigabyte hard drive"); + await jsonWriter.WriteValueAsync("200 gigabype hard drive"); + await jsonWriter.CloseAsync(); + } + + string expected = @"{ + ""CPU"": ""Intel"", + ""PSU"": ""500W"", + ""Drives"": [ + ""DVD read/writer"" + /*(broken)*/, + ""500 gigabyte hard drive"", + ""200 gigabype hard drive"" + ] +}"; + string result = sb.ToString(); + + StringAssert.AreEqual(expected, result); + } + + [Test] + public async Task IndentingAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + + await jsonWriter.WriteStartObjectAsync(); + await jsonWriter.WritePropertyNameAsync("CPU"); + await jsonWriter.WriteValueAsync("Intel"); + await jsonWriter.WritePropertyNameAsync("PSU"); + await jsonWriter.WriteValueAsync("500W"); + await jsonWriter.WritePropertyNameAsync("Drives"); + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync("DVD read/writer"); + await jsonWriter.WriteCommentAsync("(broken)"); + await jsonWriter.WriteValueAsync("500 gigabyte hard drive"); + await jsonWriter.WriteValueAsync("200 gigabype hard drive"); + await jsonWriter.WriteEndAsync(); + await jsonWriter.WriteEndObjectAsync(); + Assert.AreEqual(WriteState.Start, jsonWriter.WriteState); + } + + // { + // "CPU": "Intel", + // "PSU": "500W", + // "Drives": [ + // "DVD read/writer" + // /*(broken)*/, + // "500 gigabyte hard drive", + // "200 gigabype hard drive" + // ] + // } + + string expected = @"{ + ""CPU"": ""Intel"", + ""PSU"": ""500W"", + ""Drives"": [ + ""DVD read/writer"" + /*(broken)*/, + ""500 gigabyte hard drive"", + ""200 gigabype hard drive"" + ] +}"; + string result = sb.ToString(); + + StringAssert.AreEqual(expected, result); + } + + [Test] + public async Task StateAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + Assert.AreEqual(WriteState.Start, jsonWriter.WriteState); + + await jsonWriter.WriteStartObjectAsync(); + Assert.AreEqual(WriteState.Object, jsonWriter.WriteState); + Assert.AreEqual("", jsonWriter.Path); + + await jsonWriter.WritePropertyNameAsync("CPU"); + Assert.AreEqual(WriteState.Property, jsonWriter.WriteState); + Assert.AreEqual("CPU", jsonWriter.Path); + + await jsonWriter.WriteValueAsync("Intel"); + Assert.AreEqual(WriteState.Object, jsonWriter.WriteState); + Assert.AreEqual("CPU", jsonWriter.Path); + + await jsonWriter.WritePropertyNameAsync("Drives"); + Assert.AreEqual(WriteState.Property, jsonWriter.WriteState); + Assert.AreEqual("Drives", jsonWriter.Path); + + await jsonWriter.WriteStartArrayAsync(); + Assert.AreEqual(WriteState.Array, jsonWriter.WriteState); + + await jsonWriter.WriteValueAsync("DVD read/writer"); + Assert.AreEqual(WriteState.Array, jsonWriter.WriteState); + Assert.AreEqual("Drives[0]", jsonWriter.Path); + + await jsonWriter.WriteEndAsync(); + Assert.AreEqual(WriteState.Object, jsonWriter.WriteState); + Assert.AreEqual("Drives", jsonWriter.Path); + + await jsonWriter.WriteEndObjectAsync(); + Assert.AreEqual(WriteState.Start, jsonWriter.WriteState); + Assert.AreEqual("", jsonWriter.Path); + } + } + + [Test] + public async Task FloatingPointNonFiniteNumbers_SymbolAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol; + + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync(double.NaN); + await jsonWriter.WriteValueAsync(double.PositiveInfinity); + await jsonWriter.WriteValueAsync(double.NegativeInfinity); + await jsonWriter.WriteValueAsync(float.NaN); + await jsonWriter.WriteValueAsync(float.PositiveInfinity); + await jsonWriter.WriteValueAsync(float.NegativeInfinity); + await jsonWriter.WriteEndArrayAsync(); + + await jsonWriter.FlushAsync(); + } + + string expected = @"[ + NaN, + Infinity, + -Infinity, + NaN, + Infinity, + -Infinity +]"; + string result = sb.ToString(); + + StringAssert.AreEqual(expected, result); + } + + [Test] + public async Task FloatingPointNonFiniteNumbers_ZeroAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + jsonWriter.FloatFormatHandling = FloatFormatHandling.DefaultValue; + + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync(double.NaN); + await jsonWriter.WriteValueAsync(double.PositiveInfinity); + await jsonWriter.WriteValueAsync(double.NegativeInfinity); + await jsonWriter.WriteValueAsync(float.NaN); + await jsonWriter.WriteValueAsync(float.PositiveInfinity); + await jsonWriter.WriteValueAsync(float.NegativeInfinity); + await jsonWriter.WriteValueAsync((double?)double.NaN); + await jsonWriter.WriteValueAsync((double?)double.PositiveInfinity); + await jsonWriter.WriteValueAsync((double?)double.NegativeInfinity); + await jsonWriter.WriteValueAsync((float?)float.NaN); + await jsonWriter.WriteValueAsync((float?)float.PositiveInfinity); + await jsonWriter.WriteValueAsync((float?)float.NegativeInfinity); + await jsonWriter.WriteEndArrayAsync(); + + await jsonWriter.FlushAsync(); + } + + string expected = @"[ + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + null, + null, + null, + null, + null, + null +]"; + string result = sb.ToString(); + + StringAssert.AreEqual(expected, result); + } + + [Test] + public async Task FloatingPointNonFiniteNumbers_StringAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + jsonWriter.FloatFormatHandling = FloatFormatHandling.String; + + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync(double.NaN); + await jsonWriter.WriteValueAsync(double.PositiveInfinity); + await jsonWriter.WriteValueAsync(double.NegativeInfinity); + await jsonWriter.WriteValueAsync(float.NaN); + await jsonWriter.WriteValueAsync(float.PositiveInfinity); + await jsonWriter.WriteValueAsync(float.NegativeInfinity); + await jsonWriter.WriteEndArrayAsync(); + + await jsonWriter.FlushAsync(); + } + + string expected = @"[ + ""NaN"", + ""Infinity"", + ""-Infinity"", + ""NaN"", + ""Infinity"", + ""-Infinity"" +]"; + string result = sb.ToString(); + + StringAssert.AreEqual(expected, result); + } + + [Test] + public async Task FloatingPointNonFiniteNumbers_QuoteCharAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + jsonWriter.FloatFormatHandling = FloatFormatHandling.String; + jsonWriter.QuoteChar = '\''; + + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync(double.NaN); + await jsonWriter.WriteValueAsync(double.PositiveInfinity); + await jsonWriter.WriteValueAsync(double.NegativeInfinity); + await jsonWriter.WriteValueAsync(float.NaN); + await jsonWriter.WriteValueAsync(float.PositiveInfinity); + await jsonWriter.WriteValueAsync(float.NegativeInfinity); + await jsonWriter.WriteEndArrayAsync(); + + await jsonWriter.FlushAsync(); + } + + string expected = @"[ + 'NaN', + 'Infinity', + '-Infinity', + 'NaN', + 'Infinity', + '-Infinity' +]"; + string result = sb.ToString(); + + StringAssert.AreEqual(expected, result); + } + + [Test] + public async Task WriteRawInStartAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol; + + await jsonWriter.WriteRawAsync("[1,2,3,4,5]"); + await jsonWriter.WriteWhitespaceAsync(" "); + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync(double.NaN); + await jsonWriter.WriteEndArrayAsync(); + } + + string expected = @"[1,2,3,4,5] [ + NaN +]"; + string result = sb.ToString(); + + StringAssert.AreEqual(expected, result); + } + + [Test] + public async Task WriteRawInArrayAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol; + + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync(double.NaN); + await jsonWriter.WriteRawAsync(",[1,2,3,4,5]"); + await jsonWriter.WriteRawAsync(",[1,2,3,4,5]"); + await jsonWriter.WriteValueAsync(float.NaN); + await jsonWriter.WriteEndArrayAsync(); + } + + string expected = @"[ + NaN,[1,2,3,4,5],[1,2,3,4,5], + NaN +]"; + string result = sb.ToString(); + + StringAssert.AreEqual(expected, result); + } + + [Test] + public async Task WriteRawInObjectAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + + await jsonWriter.WriteStartObjectAsync(); + await jsonWriter.WriteRawAsync(@"""PropertyName"":[1,2,3,4,5]"); + await jsonWriter.WriteEndAsync(); + } + + string expected = @"{""PropertyName"":[1,2,3,4,5]}"; + string result = sb.ToString(); + + Assert.AreEqual(expected, result); + } + + [Test] + public async Task WriteTokenAsync() + { + CancellationToken cancel = CancellationToken.None; + JsonTextReader reader = new JsonTextReader(new StringReader("[1,2,3,4,5]")); + reader.Read(); + reader.Read(); + + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw); + await writer.WriteTokenAsync(reader, cancel); + + Assert.AreEqual("1", sw.ToString()); + } + + [Test] + public async Task WriteRawValueAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + int i = 0; + string rawJson = "[1,2]"; + + await jsonWriter.WriteStartObjectAsync(); + + while (i < 3) + { + await jsonWriter.WritePropertyNameAsync("d" + i); + await jsonWriter.WriteRawValueAsync(rawJson); + + i++; + } + + await jsonWriter.WriteEndObjectAsync(); + } + + Assert.AreEqual(@"{""d0"":[1,2],""d1"":[1,2],""d2"":[1,2]}", sb.ToString()); + } + + [Test] + public async Task WriteObjectNestedInConstructorAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + await jsonWriter.WriteStartObjectAsync(); + await jsonWriter.WritePropertyNameAsync("con"); + + await jsonWriter.WriteStartConstructorAsync("Ext.data.JsonStore"); + await jsonWriter.WriteStartObjectAsync(); + await jsonWriter.WritePropertyNameAsync("aa"); + await jsonWriter.WriteValueAsync("aa"); + await jsonWriter.WriteEndObjectAsync(); + await jsonWriter.WriteEndConstructorAsync(); + + await jsonWriter.WriteEndObjectAsync(); + } + + Assert.AreEqual(@"{""con"":new Ext.data.JsonStore({""aa"":""aa""})}", sb.ToString()); + } + + [Test] + public async Task WriteFloatingPointNumberAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol; + + await jsonWriter.WriteStartArrayAsync(); + + await jsonWriter.WriteValueAsync(0.0); + await jsonWriter.WriteValueAsync(0f); + await jsonWriter.WriteValueAsync(0.1); + await jsonWriter.WriteValueAsync(1.0); + await jsonWriter.WriteValueAsync(1.000001); + await jsonWriter.WriteValueAsync(0.000001); + await jsonWriter.WriteValueAsync(double.Epsilon); + await jsonWriter.WriteValueAsync(double.PositiveInfinity); + await jsonWriter.WriteValueAsync(double.NegativeInfinity); + await jsonWriter.WriteValueAsync(double.NaN); + await jsonWriter.WriteValueAsync(double.MaxValue); + await jsonWriter.WriteValueAsync(double.MinValue); + await jsonWriter.WriteValueAsync(float.PositiveInfinity); + await jsonWriter.WriteValueAsync(float.NegativeInfinity); + await jsonWriter.WriteValueAsync(float.NaN); + + await jsonWriter.WriteEndArrayAsync(); + } + + Assert.AreEqual(@"[0.0,0.0,0.1,1.0,1.000001,1E-06,4.94065645841247E-324,Infinity,-Infinity,NaN,1.7976931348623157E+308,-1.7976931348623157E+308,Infinity,-Infinity,NaN]", sb.ToString()); + } + + [Test] + public async Task WriteIntegerNumberAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw) { Formatting = Formatting.Indented }) + { + await jsonWriter.WriteStartArrayAsync(); + + await jsonWriter.WriteValueAsync(int.MaxValue); + await jsonWriter.WriteValueAsync(int.MinValue); + await jsonWriter.WriteValueAsync(0); + await jsonWriter.WriteValueAsync(-0); + await jsonWriter.WriteValueAsync(9L); + await jsonWriter.WriteValueAsync(9UL); + await jsonWriter.WriteValueAsync(long.MaxValue); + await jsonWriter.WriteValueAsync(long.MinValue); + await jsonWriter.WriteValueAsync(ulong.MaxValue); + await jsonWriter.WriteValueAsync(ulong.MinValue); + + await jsonWriter.WriteEndArrayAsync(); + } + + Console.WriteLine(sb.ToString()); + + StringAssert.AreEqual(@"[ + 2147483647, + -2147483648, + 0, + 0, + 9, + 9, + 9223372036854775807, + -9223372036854775808, + 18446744073709551615, + 0 +]", sb.ToString()); + } + + [Test] + public async Task WriteTokenDirectAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + await jsonWriter.WriteTokenAsync(JsonToken.StartArray); + await jsonWriter.WriteTokenAsync(JsonToken.Integer, 1); + await jsonWriter.WriteTokenAsync(JsonToken.StartObject); + await jsonWriter.WriteTokenAsync(JsonToken.PropertyName, "string"); + await jsonWriter.WriteTokenAsync(JsonToken.Integer, int.MaxValue); + await jsonWriter.WriteTokenAsync(JsonToken.EndObject); + await jsonWriter.WriteTokenAsync(JsonToken.EndArray); + } + + Assert.AreEqual(@"[1,{""string"":2147483647}]", sb.ToString()); + } + + [Test] + public async Task WriteTokenDirect_BadValueAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + await jsonWriter.WriteTokenAsync(JsonToken.StartArray); + + await ExceptionAssert.ThrowsAsync(async () => { await jsonWriter.WriteTokenAsync(JsonToken.Integer, "three"); }, "Input string was not in a correct format."); + + await ExceptionAssert.ThrowsAsync(async () => { await jsonWriter.WriteTokenAsync(JsonToken.Integer); }, @"Value cannot be null. +Parameter name: value"); + } + } + + [Test] + public async Task WriteTokenNullCheckAsync() + { + using (JsonWriter jsonWriter = new JsonTextWriter(new StringWriter())) + { + await ExceptionAssert.ThrowsAsync(async () => { await jsonWriter.WriteTokenAsync(null); }); + await ExceptionAssert.ThrowsAsync(async () => { await jsonWriter.WriteTokenAsync(null, true); }); + } + } + + [Test] + public async Task TokenTypeOutOfRangeAsync() + { + using (JsonWriter jsonWriter = new JsonTextWriter(new StringWriter())) + { + ArgumentOutOfRangeException ex = await ExceptionAssert.ThrowsAsync(async () => await jsonWriter.WriteTokenAsync((JsonToken)int.MinValue)); + Assert.AreEqual("token", ex.ParamName); + + ex = await ExceptionAssert.ThrowsAsync(async () => await jsonWriter.WriteTokenAsync((JsonToken)int.MinValue, "test")); + Assert.AreEqual("token", ex.ParamName); + } + } + + [Test] + public async Task BadWriteEndArrayAsync() + { + await ExceptionAssert.ThrowsAsync(async () => + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonWriter jsonWriter = new JsonTextWriter(sw)) + { + await jsonWriter.WriteStartArrayAsync(); + + await jsonWriter.WriteValueAsync(0.0); + + await jsonWriter.WriteEndArrayAsync(); + await jsonWriter.WriteEndArrayAsync(); + } + }, "No token to close. Path ''."); + } + + [Test] + public async Task IndentationAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + jsonWriter.FloatFormatHandling = FloatFormatHandling.Symbol; + + Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting); + + jsonWriter.Indentation = 5; + Assert.AreEqual(5, jsonWriter.Indentation); + jsonWriter.IndentChar = '_'; + Assert.AreEqual('_', jsonWriter.IndentChar); + jsonWriter.QuoteName = true; + Assert.AreEqual(true, jsonWriter.QuoteName); + jsonWriter.QuoteChar = '\''; + Assert.AreEqual('\'', jsonWriter.QuoteChar); + + await jsonWriter.WriteStartObjectAsync(); + + await jsonWriter.WritePropertyNameAsync("propertyName"); + await jsonWriter.WriteValueAsync(double.NaN); + + jsonWriter.IndentChar = '?'; + Assert.AreEqual('?', jsonWriter.IndentChar); + jsonWriter.Indentation = 6; + Assert.AreEqual(6, jsonWriter.Indentation); + + await jsonWriter.WritePropertyNameAsync("prop2"); + await jsonWriter.WriteValueAsync(123); + + await jsonWriter.WriteEndObjectAsync(); + } + + string expected = @"{ +_____'propertyName': NaN, +??????'prop2': 123 +}"; + string result = sb.ToString(); + + StringAssert.AreEqual(expected, result); + } + + [Test] + public async Task WriteSingleBytesAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + string text = "Hello world."; + byte[] data = Encoding.UTF8.GetBytes(text); + + using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting); + + await jsonWriter.WriteValueAsync(data); + } + + string expected = @"""SGVsbG8gd29ybGQu"""; + string result = sb.ToString(); + + Assert.AreEqual(expected, result); + + byte[] d2 = Convert.FromBase64String(result.Trim('"')); + + Assert.AreEqual(text, Encoding.UTF8.GetString(d2, 0, d2.Length)); + } + + [Test] + public async Task WriteBytesInArrayAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + string text = "Hello world."; + byte[] data = Encoding.UTF8.GetBytes(text); + + using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) + { + jsonWriter.Formatting = Formatting.Indented; + Assert.AreEqual(Formatting.Indented, jsonWriter.Formatting); + + await jsonWriter.WriteStartArrayAsync(); + await jsonWriter.WriteValueAsync(data); + await jsonWriter.WriteValueAsync(data); + await jsonWriter.WriteValueAsync((object)data); + await jsonWriter.WriteValueAsync((byte[])null); + await jsonWriter.WriteValueAsync((Uri)null); + await jsonWriter.WriteEndArrayAsync(); + } + + string expected = @"[ + ""SGVsbG8gd29ybGQu"", + ""SGVsbG8gd29ybGQu"", + ""SGVsbG8gd29ybGQu"", + null, + null +]"; + string result = sb.ToString(); + + StringAssert.AreEqual(expected, result); + } + + [Test] + public async Task PathAsync() + { + StringBuilder sb = new StringBuilder(); + StringWriter sw = new StringWriter(sb); + + using (JsonTextWriter writer = new JsonTextWriter(sw)) + { + writer.Formatting = Formatting.Indented; + + await writer.WriteStartArrayAsync(); + Assert.AreEqual("", writer.Path); + await writer.WriteStartObjectAsync(); + Assert.AreEqual("[0]", writer.Path); + await writer.WritePropertyNameAsync("Property1"); + Assert.AreEqual("[0].Property1", writer.Path); + await writer.WriteStartArrayAsync(); + Assert.AreEqual("[0].Property1", writer.Path); + await writer.WriteValueAsync(1); + Assert.AreEqual("[0].Property1[0]", writer.Path); + await writer.WriteStartArrayAsync(); + Assert.AreEqual("[0].Property1[1]", writer.Path); + await writer.WriteStartArrayAsync(); + Assert.AreEqual("[0].Property1[1][0]", writer.Path); + await writer.WriteStartArrayAsync(); + Assert.AreEqual("[0].Property1[1][0][0]", writer.Path); + await writer.WriteEndObjectAsync(); + Assert.AreEqual("[0]", writer.Path); + await writer.WriteStartObjectAsync(); + Assert.AreEqual("[1]", writer.Path); + await writer.WritePropertyNameAsync("Property2"); + Assert.AreEqual("[1].Property2", writer.Path); + await writer.WriteStartConstructorAsync("Constructor1"); + Assert.AreEqual("[1].Property2", writer.Path); + await writer.WriteNullAsync(); + Assert.AreEqual("[1].Property2[0]", writer.Path); + await writer.WriteStartArrayAsync(); + Assert.AreEqual("[1].Property2[1]", writer.Path); + await writer.WriteValueAsync(1); + Assert.AreEqual("[1].Property2[1][0]", writer.Path); + await writer.WriteEndAsync(); + Assert.AreEqual("[1].Property2[1]", writer.Path); + await writer.WriteEndObjectAsync(); + Assert.AreEqual("[1]", writer.Path); + await writer.WriteEndArrayAsync(); + Assert.AreEqual("", writer.Path); + } + + StringAssert.AreEqual(@"[ + { + ""Property1"": [ + 1, + [ + [ + [] + ] + ] + ] + }, + { + ""Property2"": new Constructor1( + null, + [ + 1 + ] + ) + } +]", sb.ToString()); + } + + [Test] + public async Task DateTimeZoneHandlingAsync() + { + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw) + { + DateTimeZoneHandling = DateTimeZoneHandling.Utc + }; + + await writer.WriteValueAsync(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Unspecified)); + + Assert.AreEqual(@"""2000-01-01T01:01:01Z""", sw.ToString()); + } + + [Test] + public async Task HtmlStringEscapeHandlingAsync() + { + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw) + { + StringEscapeHandling = StringEscapeHandling.EscapeHtml + }; + + string script = @""; + + await writer.WriteValueAsync(script); + + string json = sw.ToString(); + + Assert.AreEqual(@"""\u003cscript type=\u0022text/javascript\u0022\u003ealert(\u0027hi\u0027);\u003c/script\u003e""", json); + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.AreEqual(script, reader.ReadAsString()); + } + + [Test] + public async Task NonAsciiStringEscapeHandlingAsync() + { + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw) + { + StringEscapeHandling = StringEscapeHandling.EscapeNonAscii + }; + + string unicode = "\u5f20"; + + await writer.WriteValueAsync(unicode); + + string json = sw.ToString(); + + Assert.AreEqual(8, json.Length); + Assert.AreEqual(@"""\u5f20""", json); + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + + Assert.AreEqual(unicode, reader.ReadAsString()); + + sw = new StringWriter(); + writer = new JsonTextWriter(sw) + { + StringEscapeHandling = StringEscapeHandling.Default + }; + + await writer.WriteValueAsync(unicode); + + json = sw.ToString(); + + Assert.AreEqual(3, json.Length); + Assert.AreEqual("\"\u5f20\"", json); + } + + [Test] + public async Task WriteEndOnPropertyAsync() + { + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw); + writer.QuoteChar = '\''; + + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("Blah"); + await writer.WriteEndAsync(); + + Assert.AreEqual("{'Blah':null}", sw.ToString()); + } + + [Test] + public async Task QuoteCharAsync() + { + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw); + writer.Formatting = Formatting.Indented; + writer.QuoteChar = '\''; + + await writer.WriteStartArrayAsync(); + + await writer.WriteValueAsync(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc)); + await writer.WriteValueAsync(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero)); + + writer.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat; + await writer.WriteValueAsync(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc)); + await writer.WriteValueAsync(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero)); + + writer.DateFormatString = "yyyy gg"; + await writer.WriteValueAsync(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc)); + await writer.WriteValueAsync(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero)); + + await writer.WriteValueAsync(new byte[] { 1, 2, 3 }); + await writer.WriteValueAsync(TimeSpan.Zero); + await writer.WriteValueAsync(new Uri("http://www.google.com/")); + await writer.WriteValueAsync(Guid.Empty); + + await writer.WriteEndAsync(); + + StringAssert.AreEqual(@"[ + '2000-01-01T01:01:01Z', + '2000-01-01T01:01:01+00:00', + '\/Date(946688461000)\/', + '\/Date(946688461000+0000)\/', + '2000 A.D.', + '2000 A.D.', + 'AQID', + '00:00:00', + 'http://www.google.com/', + '00000000-0000-0000-0000-000000000000' +]", sw.ToString()); + } + + [Test] + public async Task CultureAsync() + { + CultureInfo culture = new CultureInfo("en-NZ"); + culture.DateTimeFormat.AMDesignator = "a.m."; + culture.DateTimeFormat.PMDesignator = "p.m."; + + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw); + writer.Formatting = Formatting.Indented; + writer.DateFormatString = "yyyy tt"; + writer.Culture = culture; + writer.QuoteChar = '\''; + + await writer.WriteStartArrayAsync(); + + await writer.WriteValueAsync(new DateTime(2000, 1, 1, 1, 1, 1, DateTimeKind.Utc)); + await writer.WriteValueAsync(new DateTimeOffset(2000, 1, 1, 1, 1, 1, TimeSpan.Zero)); + + await writer.WriteEndAsync(); + + StringAssert.AreEqual(@"[ + '2000 a.m.', + '2000 a.m.' +]", sw.ToString()); + } + + [Test] + public async Task CompareNewStringEscapingWithOldAsync() + { + char c = (char)0; + + do + { + StringWriter swNew = new StringWriter(); + JsonTextWriter jsonWriter = new JsonTextWriter(new StreamWriter(Stream.Null)); + await JavaScriptUtils.WriteEscapedJavaScriptStringAsync(swNew, c.ToString(), '"', true, JavaScriptUtils.DoubleQuoteCharEscapeFlags, StringEscapeHandling.Default, jsonWriter, null); + + StringWriter swOld = new StringWriter(); + WriteEscapedJavaScriptStringOld(swOld, c.ToString(), '"', true); + + string newText = swNew.ToString(); + string oldText = swOld.ToString(); + + if (newText != oldText) + { + throw new Exception("Difference for char '{0}' (value {1}). Old text: {2}, New text: {3}".FormatWith(CultureInfo.InvariantCulture, c, (int)c, oldText, newText)); + } + + c++; + } while (c != char.MaxValue); + } + + private const string EscapedUnicodeText = "!"; + + private static void WriteEscapedJavaScriptStringOld(TextWriter writer, string s, char delimiter, bool appendDelimiters) + { + // leading delimiter + if (appendDelimiters) + { + writer.Write(delimiter); + } + + if (s != null) + { + char[] chars = null; + char[] unicodeBuffer = null; + int lastWritePosition = 0; + + for (int i = 0; i < s.Length; i++) + { + var c = s[i]; + + // don't escape standard text/numbers except '\' and the text delimiter + if (c >= ' ' && c < 128 && c != '\\' && c != delimiter) + { + continue; + } + + string escapedValue; + + switch (c) + { + case '\t': + escapedValue = @"\t"; + break; + case '\n': + escapedValue = @"\n"; + break; + case '\r': + escapedValue = @"\r"; + break; + case '\f': + escapedValue = @"\f"; + break; + case '\b': + escapedValue = @"\b"; + break; + case '\\': + escapedValue = @"\\"; + break; + case '\u0085': // Next Line + escapedValue = @"\u0085"; + break; + case '\u2028': // Line Separator + escapedValue = @"\u2028"; + break; + case '\u2029': // Paragraph Separator + escapedValue = @"\u2029"; + break; + case '\'': + // this charater is being used as the delimiter + escapedValue = @"\'"; + break; + case '"': + // this charater is being used as the delimiter + escapedValue = "\\\""; + break; + default: + if (c <= '\u001f') + { + if (unicodeBuffer == null) + { + unicodeBuffer = new char[6]; + } + + StringUtils.ToCharAsUnicode(c, unicodeBuffer); + + // slightly hacky but it saves multiple conditions in if test + escapedValue = EscapedUnicodeText; + } + else + { + escapedValue = null; + } + break; + } + + if (escapedValue == null) + { + continue; + } + + if (i > lastWritePosition) + { + if (chars == null) + { + chars = s.ToCharArray(); + } + + // write unchanged chars before writing escaped text + writer.Write(chars, lastWritePosition, i - lastWritePosition); + } + + lastWritePosition = i + 1; + if (!string.Equals(escapedValue, EscapedUnicodeText)) + { + writer.Write(escapedValue); + } + else + { + writer.Write(unicodeBuffer); + } + } + + if (lastWritePosition == 0) + { + // no escaped text, write entire string + writer.Write(s); + } + else + { + if (chars == null) + { + chars = s.ToCharArray(); + } + + // write remaining text + writer.Write(chars, lastWritePosition, s.Length - lastWritePosition); + } + } + + // trailing delimiter + if (appendDelimiters) + { + writer.Write(delimiter); + } + } + + [Test] + public async Task CustomJsonTextWriterTestsAsync() + { + StringWriter sw = new StringWriter(); + CustomJsonTextWriter writer = new CustomAsyncJsonTextWriter(sw) { Formatting = Formatting.Indented }; + await writer.WriteStartObjectAsync(); + Assert.AreEqual(WriteState.Object, writer.WriteState); + await writer.WritePropertyNameAsync("Property1"); + Assert.AreEqual(WriteState.Property, writer.WriteState); + Assert.AreEqual("Property1", writer.Path); + await writer.WriteNullAsync(); + Assert.AreEqual(WriteState.Object, writer.WriteState); + await writer.WriteEndObjectAsync(); + Assert.AreEqual(WriteState.Start, writer.WriteState); + + StringAssert.AreEqual(@"{{{ + ""1ytreporP"": NULL!!! +}}}", sw.ToString()); + } + + [Test] + public async Task QuoteDictionaryNamesAsync() + { + var d = new Dictionary + { + { "a", 1 }, + }; + var jsonSerializerSettings = new JsonSerializerSettings + { + Formatting = Formatting.Indented, + }; + var serializer = JsonSerializer.Create(jsonSerializerSettings); + using (var stringWriter = new StringWriter()) + { + using (var writer = new JsonTextWriter(stringWriter) { QuoteName = false }) + { + serializer.Serialize(writer, d); + await writer.CloseAsync(); + } + + StringAssert.AreEqual(@"{ + a: 1 +}", stringWriter.ToString()); + } + } + + [Test] + public async Task WriteCommentsAsync() + { + string json = @"//comment*//*hi*/ +{//comment +Name://comment +true//comment after true" + StringUtils.CarriageReturn + @" +,//comment after comma" + StringUtils.CarriageReturnLineFeed + @" +""ExpiryDate""://comment" + StringUtils.LineFeed + @" +new +" + StringUtils.LineFeed + + @"Constructor +(//comment +null//comment +), + ""Price"": 3.99, + ""Sizes"": //comment +[//comment + + ""Small""//comment +]//comment +}//comment +//comment 1 "; + + JsonTextReader r = new JsonTextReader(new StringReader(json)); + + StringWriter sw = new StringWriter(); + JsonTextWriter w = new JsonTextWriter(sw); + w.Formatting = Formatting.Indented; + + await w.WriteTokenAsync(r, true); + + StringAssert.AreEqual(@"/*comment*//*hi*/*/{/*comment*/ + ""Name"": /*comment*/ true/*comment after true*//*comment after comma*/, + ""ExpiryDate"": /*comment*/ new Constructor( + /*comment*/, + null + /*comment*/ + ), + ""Price"": 3.99, + ""Sizes"": /*comment*/ [ + /*comment*/ + ""Small"" + /*comment*/ + ]/*comment*/ +}/*comment *//*comment 1 */", sw.ToString()); + } + + [Test] + public void AsyncMethodsAlreadyCancelled() + { + CancellationTokenSource source = new CancellationTokenSource(); + CancellationToken token = source.Token; + source.Cancel(); + + var writer = new JsonTextWriter(new StreamWriter(Stream.Null)); + + Assert.IsTrue(writer.CloseAsync(token).IsCanceled); + Assert.IsTrue(writer.FlushAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteCommentAsync("test", token).IsCanceled); + Assert.IsTrue(writer.WriteEndArrayAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteEndAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteEndConstructorAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteEndObjectAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteNullAsync(token).IsCanceled); + Assert.IsTrue(writer.WritePropertyNameAsync("test", token).IsCanceled); + Assert.IsTrue(writer.WritePropertyNameAsync("test", false, token).IsCanceled); + Assert.IsTrue(writer.WriteRawAsync("{}", token).IsCanceled); + Assert.IsTrue(writer.WriteRawValueAsync("{}", token).IsCanceled); + Assert.IsTrue(writer.WriteStartArrayAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteStartConstructorAsync("test", token).IsCanceled); + Assert.IsTrue(writer.WriteStartObjectAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteTokenAsync(JsonToken.Comment, token).IsCanceled); + Assert.IsTrue(writer.WriteTokenAsync(JsonToken.Boolean, true, token).IsCanceled); + JsonTextReader reader = new JsonTextReader(new StringReader("[1,2,3,4,5]")); + Assert.IsTrue(writer.WriteTokenAsync(reader, token).IsCanceled); + Assert.IsTrue(writer.WriteUndefinedAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(bool), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(bool?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(byte), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(byte?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(byte[]), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(char), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(char?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTime), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTime?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTimeOffset), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTimeOffset?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(decimal), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(decimal?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(double), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(double?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(float), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(float?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(Guid), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(Guid?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(int), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(int?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(long), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(long?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(object), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(sbyte), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(sbyte?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(short), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(short?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(TimeSpan), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(TimeSpan?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(uint), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(uint?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ulong), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ulong?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(Uri), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ushort), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ushort?), token).IsCanceled); + Assert.IsTrue(writer.WriteWhitespaceAsync(" ", token).IsCanceled); + } + + private class NoOverridesDerivedJsonTextWriter : JsonTextWriter + { + public NoOverridesDerivedJsonTextWriter(TextWriter textWriter) : base(textWriter) + { + } + } + + private class MinimalOverridesDerivedJsonWriter : JsonWriter + { + public override void Flush() + { + } + } + + [Test] + public void AsyncMethodsAlreadyCancelledOnTextWriterSubclass() + { + CancellationTokenSource source = new CancellationTokenSource(); + CancellationToken token = source.Token; + source.Cancel(); + + var writer = new NoOverridesDerivedJsonTextWriter(new StreamWriter(Stream.Null)); + + Assert.IsTrue(writer.CloseAsync(token).IsCanceled); + Assert.IsTrue(writer.FlushAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteCommentAsync("test", token).IsCanceled); + Assert.IsTrue(writer.WriteEndArrayAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteEndAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteEndConstructorAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteEndObjectAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteNullAsync(token).IsCanceled); + Assert.IsTrue(writer.WritePropertyNameAsync("test", token).IsCanceled); + Assert.IsTrue(writer.WritePropertyNameAsync("test", false, token).IsCanceled); + Assert.IsTrue(writer.WriteRawAsync("{}", token).IsCanceled); + Assert.IsTrue(writer.WriteRawValueAsync("{}", token).IsCanceled); + Assert.IsTrue(writer.WriteStartArrayAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteStartConstructorAsync("test", token).IsCanceled); + Assert.IsTrue(writer.WriteStartObjectAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteTokenAsync(JsonToken.Comment, token).IsCanceled); + Assert.IsTrue(writer.WriteTokenAsync(JsonToken.Boolean, true, token).IsCanceled); + JsonTextReader reader = new JsonTextReader(new StringReader("[1,2,3,4,5]")); + Assert.IsTrue(writer.WriteTokenAsync(reader, token).IsCanceled); + Assert.IsTrue(writer.WriteUndefinedAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(bool), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(bool?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(byte), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(byte?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(byte[]), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(char), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(char?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTime), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTime?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTimeOffset), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTimeOffset?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(decimal), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(decimal?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(double), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(double?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(float), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(float?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(Guid), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(Guid?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(int), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(int?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(long), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(long?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(object), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(sbyte), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(sbyte?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(short), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(short?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(TimeSpan), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(TimeSpan?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(uint), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(uint?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ulong), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ulong?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(Uri), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ushort), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ushort?), token).IsCanceled); + Assert.IsTrue(writer.WriteWhitespaceAsync(" ", token).IsCanceled); + } + + [Test] + public void AsyncMethodsAlreadyCancelledOnWriterSubclass() + { + CancellationTokenSource source = new CancellationTokenSource(); + CancellationToken token = source.Token; + source.Cancel(); + + var writer = new MinimalOverridesDerivedJsonWriter(); + + Assert.IsTrue(writer.CloseAsync(token).IsCanceled); + Assert.IsTrue(writer.FlushAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteCommentAsync("test", token).IsCanceled); + Assert.IsTrue(writer.WriteEndArrayAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteEndAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteEndConstructorAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteEndObjectAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteNullAsync(token).IsCanceled); + Assert.IsTrue(writer.WritePropertyNameAsync("test", token).IsCanceled); + Assert.IsTrue(writer.WritePropertyNameAsync("test", false, token).IsCanceled); + Assert.IsTrue(writer.WriteRawAsync("{}", token).IsCanceled); + Assert.IsTrue(writer.WriteRawValueAsync("{}", token).IsCanceled); + Assert.IsTrue(writer.WriteStartArrayAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteStartConstructorAsync("test", token).IsCanceled); + Assert.IsTrue(writer.WriteStartObjectAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteTokenAsync(JsonToken.Comment, token).IsCanceled); + Assert.IsTrue(writer.WriteTokenAsync(JsonToken.Boolean, true, token).IsCanceled); + JsonTextReader reader = new JsonTextReader(new StringReader("[1,2,3,4,5]")); + Assert.IsTrue(writer.WriteTokenAsync(reader, token).IsCanceled); + Assert.IsTrue(writer.WriteUndefinedAsync(token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(bool), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(bool?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(byte), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(byte?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(byte[]), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(char), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(char?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTime), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTime?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTimeOffset), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(DateTimeOffset?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(decimal), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(decimal?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(double), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(double?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(float), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(float?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(Guid), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(Guid?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(int), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(int?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(long), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(long?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(object), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(sbyte), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(sbyte?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(short), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(short?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(TimeSpan), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(TimeSpan?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(uint), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(uint?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ulong), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ulong?), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(Uri), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ushort), token).IsCanceled); + Assert.IsTrue(writer.WriteValueAsync(default(ushort?), token).IsCanceled); + Assert.IsTrue(writer.WriteWhitespaceAsync(" ", token).IsCanceled); + } + } + + public class CustomAsyncJsonTextWriter : CustomJsonTextWriter + { + public CustomAsyncJsonTextWriter(TextWriter textWriter) : base(textWriter) + { + } + + public override Task WritePropertyNameAsync(string name, CancellationToken cancellationToken = default(CancellationToken)) + { + return WritePropertyNameAsync(name, true, cancellationToken); + } + + public override async Task WritePropertyNameAsync(string name, bool escape, CancellationToken cancellationToken = default(CancellationToken)) + { + await SetWriteStateAsync(JsonToken.PropertyName, name, cancellationToken); + + if (QuoteName) + { + await _writer.WriteAsync(QuoteChar); + } + + await _writer.WriteAsync(new string(name.ToCharArray().Reverse().ToArray())); + + if (QuoteName) + { + await _writer.WriteAsync(QuoteChar); + } + + await _writer.WriteAsync(':'); + } + + public override async Task WriteNullAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + await SetWriteStateAsync(JsonToken.Null, null, cancellationToken); + + await _writer.WriteAsync("NULL!!!"); + } + + public override async Task WriteStartObjectAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + await SetWriteStateAsync(JsonToken.StartObject, null, cancellationToken); + + await _writer.WriteAsync("{{{"); + } + + public override Task WriteEndObjectAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return SetWriteStateAsync(JsonToken.EndObject, null, cancellationToken); + } + + protected override Task WriteEndAsync(JsonToken token, CancellationToken cancellationToken) + { + if (token == JsonToken.EndObject) + { + return _writer.WriteAsync("}}}"); + } + else + { + return base.WriteEndAsync(token, cancellationToken); + } + } + } +} +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs index b9ec01a3..fb8afb4c 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs @@ -1649,7 +1649,7 @@ null//comment public class CustomJsonTextWriter : JsonTextWriter { - private readonly TextWriter _writer; + protected readonly TextWriter _writer; public CustomJsonTextWriter(TextWriter textWriter) : base(textWriter) { diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj index 206e25dc..a103c53d 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj @@ -107,8 +107,10 @@ - + + + @@ -122,10 +124,15 @@ + + + + + @@ -341,6 +348,7 @@ + @@ -430,4 +438,4 @@ --> - \ No newline at end of file + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj index 70194a20..c892f073 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj @@ -49,7 +49,9 @@ + + @@ -222,8 +224,14 @@ + + + + + + diff --git a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs index c58da075..7dacd84c 100644 --- a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs +++ b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs @@ -43,6 +43,7 @@ using Newtonsoft.Json.Bson; using System.Runtime.Serialization.Formatters.Binary; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Converters; +using System.Threading.Tasks; namespace Newtonsoft.Json.Tests { @@ -69,8 +70,8 @@ namespace Newtonsoft.Json.Tests #if DEBUG public int Iterations = 1; #else - public int Iterations = 100; - //public int Iterations = 10000; + //public int Iterations = 100; + public int Iterations = 10000; #endif #region Data @@ -99,6 +100,7 @@ namespace Newtonsoft.Json.Tests JsonNetBinary, JsonNetLinq, JsonNetManual, + JsonNetManualAsync, BinaryFormatter, JavaScriptSerializer, DataContractSerializer, @@ -136,6 +138,14 @@ namespace Newtonsoft.Json.Tests SerializeTests(test); } + [Test] + public async Task SerializeAsync() + { + TestClass test = CreateSerializationObject(); + + await BenchmarkSerializeMethodAsync(SerializeMethod.JsonNetManualAsync, test); + } + [Test] public void ReadLargeJson() { @@ -238,6 +248,12 @@ namespace Newtonsoft.Json.Tests BenchmarkDeserializeMethod(SerializeMethod.JsonNetBinary, HexToBytes(BsonHex)); } + [Test] + public async Task DeserializeAsync() + { + await BenchmarkDeserializeMethodAsync(SerializeMethod.JsonNetManualAsync, JsonText); + } + public void DeserializeTests(string json) { BenchmarkDeserializeMethod(SerializeMethod.JavaScriptSerializer, json); @@ -641,6 +657,27 @@ If attributes are not mentioned, default values are used in each case. #region Serialize private static readonly byte[] Buffer = new byte[4096]; + public async Task BenchmarkSerializeMethodAsync(SerializeMethod method, object value) + { + await SerializeAsync(method, value); + + Stopwatch timed = new Stopwatch(); + timed.Start(); + + string json = null; + for (int x = 0; x < Iterations; x++) + { + json = await SerializeAsync(method, value); + } + + timed.Stop(); + + Console.WriteLine("Serialize method: {0}", method); + Console.WriteLine("{0} ms", timed.ElapsedMilliseconds); + Console.WriteLine(json); + Console.WriteLine(); + } + public void BenchmarkSerializeMethod(SerializeMethod method, object value) { Serialize(method, value); @@ -738,6 +775,83 @@ If attributes are not mentioned, default values are used in each case. } } + private async Task SerializeAsync(SerializeMethod method, object value) + { + string json; + + switch (method) + { + case SerializeMethod.JsonNetManualAsync: + { + TestClass c = value as TestClass; + if (c != null) + { + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw); + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("strings"); + await writer.WriteStartArrayAsync(); + foreach (string s in c.strings) + { + await writer.WriteValueAsync(s); + } + await writer.WriteEndArrayAsync(); + await writer.WritePropertyNameAsync("dictionary"); + await writer.WriteStartObjectAsync(); + foreach (KeyValuePair keyValuePair in c.dictionary) + { + await writer.WritePropertyNameAsync(keyValuePair.Key); + await writer.WriteValueAsync(keyValuePair.Value); + } + await writer.WriteEndObjectAsync(); + await writer.WritePropertyNameAsync("Name"); + await writer.WriteValueAsync(c.Name); + await writer.WritePropertyNameAsync("Now"); + await writer.WriteValueAsync(c.Now); + await writer.WritePropertyNameAsync("BigNumber"); + await writer.WriteValueAsync(c.BigNumber); + await writer.WritePropertyNameAsync("Address1"); + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("Street"); + await writer.WriteValueAsync(c.BigNumber); + await writer.WritePropertyNameAsync("Street"); + await writer.WriteValueAsync(c.BigNumber); + await writer.WritePropertyNameAsync("Street"); + await writer.WriteValueAsync(c.BigNumber); + await writer.WriteEndObjectAsync(); + await writer.WritePropertyNameAsync("Addresses"); + await writer.WriteStartArrayAsync(); + foreach (Address address in c.Addresses) + { + await writer.WriteStartObjectAsync(); + await writer.WritePropertyNameAsync("Street"); + await writer.WriteValueAsync(address.Street); + await writer.WritePropertyNameAsync("Phone"); + await writer.WriteValueAsync(address.Phone); + await writer.WritePropertyNameAsync("Entered"); + await writer.WriteValueAsync(address.Entered); + await writer.WriteEndObjectAsync(); + } + await writer.WriteEndArrayAsync(); + await writer.WriteEndObjectAsync(); + + await writer.FlushAsync(); + json = sw.ToString(); + } + else + { + json = string.Empty; + } + break; + } + default: + json = string.Empty; + break; + } + + return json; + } + private string Serialize(SerializeMethod method, object value) { string json; @@ -751,111 +865,111 @@ If attributes are not mentioned, default values are used in each case. json = JsonConvert.SerializeObject(value, new IsoDateTimeConverter()); break; case SerializeMethod.JsonNetLinq: - { - TestClass c = value as TestClass; - if (c != null) { - JObject o = new JObject( - new JProperty("strings", new JArray( - c.strings - )), - new JProperty("dictionary", new JObject(c.dictionary.Select(d => new JProperty(d.Key, d.Value)))), - new JProperty("Name", c.Name), - new JProperty("Now", c.Now), - new JProperty("BigNumber", c.BigNumber), - new JProperty("Address1", new JObject( - new JProperty("Street", c.Address1.Street), - new JProperty("Phone", c.Address1.Phone), - new JProperty("Entered", c.Address1.Entered))), - new JProperty("Addresses", new JArray(c.Addresses.Select(a => - new JObject( - new JProperty("Street", a.Street), - new JProperty("Phone", a.Phone), - new JProperty("Entered", a.Entered))))) - ); + TestClass c = value as TestClass; + if (c != null) + { + JObject o = new JObject( + new JProperty("strings", new JArray( + c.strings + )), + new JProperty("dictionary", new JObject(c.dictionary.Select(d => new JProperty(d.Key, d.Value)))), + new JProperty("Name", c.Name), + new JProperty("Now", c.Now), + new JProperty("BigNumber", c.BigNumber), + new JProperty("Address1", new JObject( + new JProperty("Street", c.Address1.Street), + new JProperty("Phone", c.Address1.Phone), + new JProperty("Entered", c.Address1.Entered))), + new JProperty("Addresses", new JArray(c.Addresses.Select(a => + new JObject( + new JProperty("Street", a.Street), + new JProperty("Phone", a.Phone), + new JProperty("Entered", a.Entered))))) + ); - json = o.ToString(Formatting.None); + json = o.ToString(Formatting.None); + } + else + { + json = string.Empty; + } + break; } - else - { - json = string.Empty; - } - break; - } case SerializeMethod.JsonNetManual: - { - TestClass c = value as TestClass; - if (c != null) { - StringWriter sw = new StringWriter(); - JsonTextWriter writer = new JsonTextWriter(sw); - writer.WriteStartObject(); - writer.WritePropertyName("strings"); - writer.WriteStartArray(); - foreach (string s in c.strings) - { - writer.WriteValue(s); - } - writer.WriteEndArray(); - writer.WritePropertyName("dictionary"); - writer.WriteStartObject(); - foreach (KeyValuePair keyValuePair in c.dictionary) - { - writer.WritePropertyName(keyValuePair.Key); - writer.WriteValue(keyValuePair.Value); - } - writer.WriteEndObject(); - writer.WritePropertyName("Name"); - writer.WriteValue(c.Name); - writer.WritePropertyName("Now"); - writer.WriteValue(c.Now); - writer.WritePropertyName("BigNumber"); - writer.WriteValue(c.BigNumber); - writer.WritePropertyName("Address1"); - writer.WriteStartObject(); - writer.WritePropertyName("Street"); - writer.WriteValue(c.BigNumber); - writer.WritePropertyName("Street"); - writer.WriteValue(c.BigNumber); - writer.WritePropertyName("Street"); - writer.WriteValue(c.BigNumber); - writer.WriteEndObject(); - writer.WritePropertyName("Addresses"); - writer.WriteStartArray(); - foreach (Address address in c.Addresses) + TestClass c = value as TestClass; + if (c != null) { + StringWriter sw = new StringWriter(); + JsonTextWriter writer = new JsonTextWriter(sw); + writer.WriteStartObject(); + writer.WritePropertyName("strings"); + writer.WriteStartArray(); + foreach (string s in c.strings) + { + writer.WriteValue(s); + } + writer.WriteEndArray(); + writer.WritePropertyName("dictionary"); + writer.WriteStartObject(); + foreach (KeyValuePair keyValuePair in c.dictionary) + { + writer.WritePropertyName(keyValuePair.Key); + writer.WriteValue(keyValuePair.Value); + } + writer.WriteEndObject(); + writer.WritePropertyName("Name"); + writer.WriteValue(c.Name); + writer.WritePropertyName("Now"); + writer.WriteValue(c.Now); + writer.WritePropertyName("BigNumber"); + writer.WriteValue(c.BigNumber); + writer.WritePropertyName("Address1"); writer.WriteStartObject(); writer.WritePropertyName("Street"); - writer.WriteValue(address.Street); - writer.WritePropertyName("Phone"); - writer.WriteValue(address.Phone); - writer.WritePropertyName("Entered"); - writer.WriteValue(address.Entered); + writer.WriteValue(c.BigNumber); + writer.WritePropertyName("Street"); + writer.WriteValue(c.BigNumber); + writer.WritePropertyName("Street"); + writer.WriteValue(c.BigNumber); writer.WriteEndObject(); + writer.WritePropertyName("Addresses"); + writer.WriteStartArray(); + foreach (Address address in c.Addresses) + { + writer.WriteStartObject(); + writer.WritePropertyName("Street"); + writer.WriteValue(address.Street); + writer.WritePropertyName("Phone"); + writer.WriteValue(address.Phone); + writer.WritePropertyName("Entered"); + writer.WriteValue(address.Entered); + writer.WriteEndObject(); + } + writer.WriteEndArray(); + writer.WriteEndObject(); + + writer.Flush(); + json = sw.ToString(); } - writer.WriteEndArray(); - writer.WriteEndObject(); - - writer.Flush(); - json = sw.ToString(); + else + { + json = string.Empty; + } + break; } - else - { - json = string.Empty; - } - break; - } case SerializeMethod.JsonNetBinary: - { - MemoryStream ms = new MemoryStream(Buffer); - JsonSerializer serializer = new JsonSerializer(); - BsonWriter writer = new BsonWriter(ms); - serializer.Serialize(writer, value); + { + MemoryStream ms = new MemoryStream(Buffer); + JsonSerializer serializer = new JsonSerializer(); + BsonWriter writer = new BsonWriter(ms); + serializer.Serialize(writer, value); - //json = BitConverter.ToString(ms.ToArray(), 0, (int)ms.Position); - json = "Bytes = " + ms.Position; - break; - } + //json = BitConverter.ToString(ms.ToArray(), 0, (int)ms.Position); + json = "Bytes = " + ms.Position; + break; + } case SerializeMethod.JavaScriptSerializer: json = SerializeWebExtensions(value); break; @@ -918,6 +1032,35 @@ If attributes are not mentioned, default values are used in each case. Console.WriteLine(); } + public async Task BenchmarkDeserializeMethodAsync(SerializeMethod method, object json, int? iterations = null, bool warmUp = true) + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + + if (warmUp) + { + await DeserializeAsync(method, json); + } + + Stopwatch timed = new Stopwatch(); + timed.Start(); + + iterations = iterations ?? Iterations; + + T value = default(T); + for (int x = 0; x < iterations.Value; x++) + { + value = await DeserializeAsync(method, json); + } + + timed.Stop(); + + Console.WriteLine("Deserialize method: {0}", method); + Console.WriteLine("{0} ms", timed.ElapsedMilliseconds); + Console.WriteLine(value); + Console.WriteLine(); + } + public T DeserializeJsonNet(string json, bool isoDateTimeConverter) { Type type = typeof(T); @@ -940,6 +1083,66 @@ If attributes are not mentioned, default values are used in each case. } } + public async Task DeserializeJsonNetManualAsync(string json) + { + TestClass c = new TestClass(); + + JsonTextReader reader = new JsonTextReader(new StringReader(json)); + await reader.ReadAsync(); + while (await reader.ReadAsync()) + { + if (reader.TokenType == JsonToken.PropertyName) + { + string propertyName = (string)reader.Value; + switch (propertyName) + { + case "strings": + await reader.ReadAsync(); + while (await reader.ReadAsync() && reader.TokenType != JsonToken.EndArray) + { + c.strings.Add((string)reader.Value); + } + break; + case "dictionary": + await reader.ReadAsync(); + while (await reader.ReadAsync() && reader.TokenType != JsonToken.EndObject) + { + string key = (string)reader.Value; + c.dictionary.Add(key, reader.ReadAsInt32().GetValueOrDefault()); + } + break; + case "Name": + c.Name = reader.ReadAsString(); + break; + case "Now": + c.Now = reader.ReadAsDateTime().GetValueOrDefault(); + break; + case "BigNumber": + c.BigNumber = reader.ReadAsDecimal().GetValueOrDefault(); + break; + case "Address1": + await reader.ReadAsync(); + c.Address1 = CreateAddress(reader); + break; + case "Addresses": + await reader.ReadAsync(); + while (await reader.ReadAsync() && reader.TokenType != JsonToken.EndArray) + { + var address = CreateAddress(reader); + c.Addresses.Add(address); + } + break; + } + } + else + { + break; + } + } + + return c; + } + public TestClass DeserializeJsonNetManual(string json) { TestClass c = new TestClass(); @@ -1056,6 +1259,22 @@ If attributes are not mentioned, default values are used in each case. return (T)dataContractSerializer.ReadObject(ms); } + private async Task DeserializeAsync(SerializeMethod method, object json) + { + switch (method) + { + case SerializeMethod.JsonNetManualAsync: + if (typeof(T) == typeof(TestClass)) + { + return (T)(object)await DeserializeJsonNetManualAsync((string)json); + } + + return default(T); + default: + throw new ArgumentOutOfRangeException(nameof(method)); + } + } + private T Deserialize(SerializeMethod method, object json) { switch (method) diff --git a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs index 5e9c1deb..35eeabfd 100644 --- a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs +++ b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs @@ -45,6 +45,9 @@ using NUnit.Framework; #endif using Newtonsoft.Json.Utilities; using System.Collections; +#if !(NET20 || NET35 || NET40 || PORTABLE40) +using System.Threading.Tasks; +#endif #if NET20 using Newtonsoft.Json.Utilities.LinqBridge; #else @@ -379,5 +382,40 @@ namespace Newtonsoft.Json.Tests throw new Exception(string.Format("Exception of type {0} expected; got exception of type {1}.", typeof(TException).Name, ex.GetType().Name), ex); } } + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + public static async Task ThrowsAsync(Func action, params string[] possibleMessages) + where TException : Exception + { + try + { + await action(); + + Assert.Fail("Exception of type {0} expected. No exception thrown.", typeof(TException).Name); + return null; + } + catch (TException ex) + { + if (possibleMessages == null || possibleMessages.Length == 0) + { + return ex; + } + foreach (string possibleMessage in possibleMessages) + { + if (StringAssert.Equals(possibleMessage, ex.Message)) + { + return ex; + } + } + + throw new Exception("Unexpected exception message." + Environment.NewLine + "Expected one of: " + string.Join(Environment.NewLine, possibleMessages) + Environment.NewLine + "Got: " + ex.Message + Environment.NewLine + Environment.NewLine + ex); + } + catch (Exception ex) + { + throw new Exception(string.Format("Exception of type {0} expected; got exception of type {1}.", typeof(TException).Name, ex.GetType().Name), ex); + } + } +#endif + } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonReader.Async.cs b/Src/Newtonsoft.Json/JsonReader.Async.cs new file mode 100644 index 00000000..4ae6b2e0 --- /dev/null +++ b/Src/Newtonsoft.Json/JsonReader.Async.cs @@ -0,0 +1,210 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Threading; +using System.Threading.Tasks; +using Newtonsoft.Json.Utilities; + +namespace Newtonsoft.Json +{ + public abstract partial class JsonReader + { + /// + /// Asynchronously reads the next JSON token from the source. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns true if the next token was read successfully; false if there are no more tokens to read. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task ReadAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return cancellationToken.CancelIfRequestedAsync() ?? Read().ToAsync(); + } + + /// + /// Asynchronously skips the children of the current token. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public async Task SkipAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (TokenType == JsonToken.PropertyName) + { + await ReadAsync(cancellationToken).ConfigureAwait(false); + } + + if (JsonTokenUtils.IsStartToken(TokenType)) + { + int depth = Depth; + + while (await ReadAsync(cancellationToken).ConfigureAwait(false) && depth < Depth) + { + } + } + } + + internal async Task ReaderReadAndAssertAsync(CancellationToken cancellationToken) + { + if (!await ReadAsync(cancellationToken).ConfigureAwait(false)) + { + throw CreateUnexpectedEndException(); + } + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task ReadAsBooleanAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsBoolean()); + } + + /// + /// Asynchronously reads the next JSON token from the source as a []. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the []. This result will be null at the end of an array. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task ReadAsBytesAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsBytes()); + } + + internal async Task ReadArrayIntoByteArrayAsync(CancellationToken cancellationToken) + { + List buffer = new List(); + + while (true) + { + if (!await ReadAsync(cancellationToken).ConfigureAwait(false)) + { + SetToken(JsonToken.None); + } + + if (ReadArrayElementIntoByteArrayReportDone(buffer)) + { + byte[] d = buffer.ToArray(); + SetToken(JsonToken.Bytes, d, false); + return d; + } + } + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task ReadAsDateTimeAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsDateTime()); + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task ReadAsDateTimeOffsetAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsDateTimeOffset()); + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task ReadAsDecimalAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsDecimal()); + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task ReadAsDoubleAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return Task.FromResult(ReadAsDouble()); + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task ReadAsInt32Async(CancellationToken cancellationToken = default(CancellationToken)) + { + return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsInt32()); + } + + /// + /// Asynchronously reads the next JSON token from the source as a . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the . This result will be null at the end of an array. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task ReadAsStringAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsString()); + } + } +} + +#endif diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index 4255a42e..d271d8a7 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -43,7 +43,7 @@ namespace Newtonsoft.Json /// /// Represents a reader that provides fast, non-cached, forward-only access to serialized JSON data. /// - public abstract class JsonReader : IDisposable + public abstract partial class JsonReader : IDisposable { /// /// Specifies the state of the reader. @@ -583,21 +583,35 @@ namespace Newtonsoft.Json while (true) { - JsonToken t = GetContentToken(); - switch (t) + if (!Read()) { - case JsonToken.None: - throw JsonReaderException.Create(this, "Unexpected end when reading bytes."); - case JsonToken.Integer: - buffer.Add(Convert.ToByte(Value, CultureInfo.InvariantCulture)); - break; - case JsonToken.EndArray: - byte[] d = buffer.ToArray(); - SetToken(JsonToken.Bytes, d, false); - return d; - default: - throw JsonReaderException.Create(this, "Unexpected token when reading bytes: {0}.".FormatWith(CultureInfo.InvariantCulture, t)); + SetToken(JsonToken.None); } + + if (ReadArrayElementIntoByteArrayReportDone(buffer)) + { + byte[] d = buffer.ToArray(); + SetToken(JsonToken.Bytes, d, false); + return d; + } + } + } + + private bool ReadArrayElementIntoByteArrayReportDone(List buffer) + { + switch (TokenType) + { + case JsonToken.None: + throw JsonReaderException.Create(this, "Unexpected end when reading bytes."); + case JsonToken.Integer: + buffer.Add(Convert.ToByte(Value, CultureInfo.InvariantCulture)); + return false; + case JsonToken.EndArray: + return true; + case JsonToken.Comment: + return false; + default: + throw JsonReaderException.Create(this, "Unexpected token when reading bytes: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); } } diff --git a/Src/Newtonsoft.Json/JsonTextReader.Async.cs b/Src/Newtonsoft.Json/JsonTextReader.Async.cs new file mode 100644 index 00000000..f7b963d7 --- /dev/null +++ b/Src/Newtonsoft.Json/JsonTextReader.Async.cs @@ -0,0 +1,1715 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +using System.Globalization; +using System.Threading; +#if !PORTABLE || NETSTANDARD1_1 +using System.Numerics; +#endif +using System.Threading.Tasks; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json.Utilities; + +namespace Newtonsoft.Json +{ + public partial class JsonTextReader + { + // It's not safe to perform the async methods here in a derived class as if the synchronous equivalent + // has been overriden then the asychronous method will no longer be doing the same operation +#if !(NET20 || NET35 || NET40 || PORTABLE40) // Double-check this isn't included inappropriately. + private readonly bool _safeAsync; +#endif + + /// + /// Asynchronously reads the next JSON token from the source. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns true if the next token was read successfully; false if there are no more tokens to read. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task ReadAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoReadAsync(cancellationToken) : base.ReadAsync(cancellationToken); + } + + internal async Task DoReadAsync(CancellationToken cancellationToken) + { + EnsureBuffer(); + + while (true) + { + switch (_currentState) + { + case State.Start: + case State.Property: + case State.Array: + case State.ArrayStart: + case State.Constructor: + case State.ConstructorStart: + return await ParseValueAsync(cancellationToken).ConfigureAwait(false); + case State.Object: + case State.ObjectStart: + return await ParseObjectAsync(cancellationToken).ConfigureAwait(false); + case State.PostValue: + + // returns true if it hits + // end of object or array + if (await ParsePostValueAsync(cancellationToken).ConfigureAwait(false)) + { + return true; + } + + break; + case State.Finished: + if (await EnsureCharsAsync(0, false, cancellationToken).ConfigureAwait(false)) + { + await EatWhitespaceAsync(false, cancellationToken).ConfigureAwait(false); + if (_isEndOfFile) + { + SetToken(JsonToken.None); + return false; + } + + if (_chars[_charPos] == '/') + { + await ParseCommentAsync(true, cancellationToken).ConfigureAwait(false); + return true; + } + + throw JsonReaderException.Create(this, "Additional text encountered after finished reading JSON content: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos])); + } + + SetToken(JsonToken.None); + return false; + default: + throw JsonReaderException.Create(this, "Unexpected state: {0}.".FormatWith(CultureInfo.InvariantCulture, CurrentState)); + } + } + } + + private Task ReadDataAsync(bool append, CancellationToken cancellationToken) + { + return ReadDataAsync(append, 0, cancellationToken); + } + + private async Task ReadDataAsync(bool append, int charsRequired, CancellationToken cancellationToken) + { + if (_isEndOfFile) + { + return 0; + } + + PrepareBufferForReadData(append, charsRequired); + + int charsRead = await _reader.ReadAsync(_chars, _charsUsed, _chars.Length - _charsUsed - 1, cancellationToken).ConfigureAwait(false); + + _charsUsed += charsRead; + + if (charsRead == 0) + { + _isEndOfFile = true; + } + + _chars[_charsUsed] = '\0'; + return charsRead; + } + + private async Task ParseValueAsync(CancellationToken cancellationToken) + { + while (true) + { + char currentChar = _chars[_charPos]; + + switch (currentChar) + { + case '\0': + if (_charsUsed == _charPos) + { + if (await ReadDataAsync(false, cancellationToken).ConfigureAwait(false) == 0) + { + return false; + } + } + else + { + _charPos++; + } + + break; + case '"': + case '\'': + await ParseStringAsync(currentChar, ReadType.Read, cancellationToken).ConfigureAwait(false); + return true; + case 't': + await ParseTrueAsync(cancellationToken).ConfigureAwait(false); + return true; + case 'f': + await ParseFalseAsync(cancellationToken).ConfigureAwait(false); + return true; + case 'n': + if (await EnsureCharsAsync(1, true, cancellationToken).ConfigureAwait(false)) + { + switch (_chars[_charPos + 1]) + { + case 'u': + await ParseNullAsync(cancellationToken).ConfigureAwait(false); + break; + case 'e': + await ParseConstructorAsync(cancellationToken).ConfigureAwait(false); + break; + default: + throw CreateUnexpectedCharacterException(_chars[_charPos]); + } + } + else + { + _charPos++; + throw CreateUnexpectedEndException(); + } + + return true; + case 'N': + await ParseNumberNaNAsync(ReadType.Read, cancellationToken).ConfigureAwait(false); + return true; + case 'I': + await ParseNumberPositiveInfinityAsync(ReadType.Read, cancellationToken).ConfigureAwait(false); + return true; + case '-': + if (await EnsureCharsAsync(1, true, cancellationToken).ConfigureAwait(false) && _chars[_charPos + 1] == 'I') + { + await ParseNumberNegativeInfinityAsync(ReadType.Read, cancellationToken).ConfigureAwait(false); + } + else + { + await ParseNumberAsync(ReadType.Read, cancellationToken).ConfigureAwait(false); + } + return true; + case '/': + await ParseCommentAsync(true, cancellationToken).ConfigureAwait(false); + return true; + case 'u': + await ParseUndefinedAsync(cancellationToken).ConfigureAwait(false); + return true; + case '{': + _charPos++; + SetToken(JsonToken.StartObject); + return true; + case '[': + _charPos++; + SetToken(JsonToken.StartArray); + return true; + case ']': + _charPos++; + SetToken(JsonToken.EndArray); + return true; + case ',': + + // don't increment position, the next call to read will handle comma + // this is done to handle multiple empty comma values + SetToken(JsonToken.Undefined); + return true; + case ')': + _charPos++; + SetToken(JsonToken.EndConstructor); + return true; + case StringUtils.CarriageReturn: + await ProcessCarriageReturnAsync(false, cancellationToken).ConfigureAwait(false); + break; + case StringUtils.LineFeed: + ProcessLineFeed(); + break; + case ' ': + case StringUtils.Tab: + + // eat + _charPos++; + break; + default: + if (char.IsWhiteSpace(currentChar)) + { + // eat + _charPos++; + break; + } + + if (char.IsNumber(currentChar) || currentChar == '-' || currentChar == '.') + { + ParseNumber(ReadType.Read); + return true; + } + + throw CreateUnexpectedCharacterException(currentChar); + } + } + } + + private async Task ReadStringIntoBufferAsync(char quote, CancellationToken cancellationToken) + { + int charPos = _charPos; + int initialPosition = _charPos; + int lastWritePosition = _charPos; + _stringBuffer.Position = 0; + + while (true) + { + switch (_chars[charPos++]) + { + case '\0': + if (_charsUsed == charPos - 1) + { + charPos--; + + if (await ReadDataAsync(true, cancellationToken).ConfigureAwait(false) == 0) + { + _charPos = charPos; + throw JsonReaderException.Create(this, "Unterminated string. Expected delimiter: {0}.".FormatWith(CultureInfo.InvariantCulture, quote)); + } + } + + break; + case '\\': + _charPos = charPos; + if (!await EnsureCharsAsync(0, true, cancellationToken).ConfigureAwait(false)) + { + throw JsonReaderException.Create(this, "Unterminated string. Expected delimiter: {0}.".FormatWith(CultureInfo.InvariantCulture, quote)); + } + + // start of escape sequence + int escapeStartPos = charPos - 1; + + char currentChar = _chars[charPos]; + charPos++; + + char writeChar; + + switch (currentChar) + { + case 'b': + writeChar = '\b'; + break; + case 't': + writeChar = '\t'; + break; + case 'n': + writeChar = '\n'; + break; + case 'f': + writeChar = '\f'; + break; + case 'r': + writeChar = '\r'; + break; + case '\\': + writeChar = '\\'; + break; + case '"': + case '\'': + case '/': + writeChar = currentChar; + break; + case 'u': + _charPos = charPos; + writeChar = await ParseUnicodeAsync(cancellationToken).ConfigureAwait(false); + + if (StringUtils.IsLowSurrogate(writeChar)) + { + // low surrogate with no preceding high surrogate; this char is replaced + writeChar = UnicodeReplacementChar; + } + else if (StringUtils.IsHighSurrogate(writeChar)) + { + bool anotherHighSurrogate; + + // loop for handling situations where there are multiple consecutive high surrogates + do + { + anotherHighSurrogate = false; + + // potential start of a surrogate pair + if (await EnsureCharsAsync(2, true, cancellationToken).ConfigureAwait(false) && _chars[_charPos] == '\\' && _chars[_charPos + 1] == 'u') + { + char highSurrogate = writeChar; + + _charPos += 2; + writeChar = await ParseUnicodeAsync(cancellationToken).ConfigureAwait(false); + + if (StringUtils.IsLowSurrogate(writeChar)) + { + // a valid surrogate pair! + } + else if (StringUtils.IsHighSurrogate(writeChar)) + { + // another high surrogate; replace current and start check over + highSurrogate = UnicodeReplacementChar; + anotherHighSurrogate = true; + } + else + { + // high surrogate not followed by low surrogate; original char is replaced + highSurrogate = UnicodeReplacementChar; + } + + EnsureBufferNotEmpty(); + + WriteCharToBuffer(highSurrogate, lastWritePosition, escapeStartPos); + lastWritePosition = _charPos; + } + else + { + // there are not enough remaining chars for the low surrogate or is not follow by unicode sequence + // replace high surrogate and continue on as usual + writeChar = UnicodeReplacementChar; + } + } while (anotherHighSurrogate); + } + + charPos = _charPos; + break; + default: + _charPos = charPos; + throw JsonReaderException.Create(this, "Bad JSON escape sequence: {0}.".FormatWith(CultureInfo.InvariantCulture, @"\" + currentChar)); + } + + EnsureBufferNotEmpty(); + WriteCharToBuffer(writeChar, lastWritePosition, escapeStartPos); + + lastWritePosition = charPos; + break; + case StringUtils.CarriageReturn: + _charPos = charPos - 1; + await ProcessCarriageReturnAsync(true, cancellationToken).ConfigureAwait(false); + charPos = _charPos; + break; + case StringUtils.LineFeed: + _charPos = charPos - 1; + ProcessLineFeed(); + charPos = _charPos; + break; + case '"': + case '\'': + if (_chars[charPos - 1] == quote) + { + FinishReadStringIntoBuffer(charPos - 1, initialPosition, lastWritePosition); + return; + } + + break; + } + } + } + + private async Task ProcessCarriageReturnAsync(bool append, CancellationToken cancellationToken) + { + _charPos++; + + if (await EnsureCharsAsync(1, append, cancellationToken).ConfigureAwait(false) && _chars[_charPos] == StringUtils.LineFeed) + { + _charPos++; + } + + OnNewLine(_charPos); + } + + private async Task ParseUnicodeAsync(CancellationToken cancellationToken) + { + return ConvertUnicode(await EnsureCharsAsync(4, true, cancellationToken).ConfigureAwait(false)); + } + + private Task EnsureCharsAsync(int relativePosition, bool append, CancellationToken cancellationToken) + { + if (_charPos + relativePosition < _charsUsed) + { + return AsyncUtils.True; + } + + if (_isEndOfFile) + { + return AsyncUtils.False; + } + + return ReadCharsAsync(relativePosition, append, cancellationToken); + } + + private async Task ReadCharsAsync(int relativePosition, bool append, CancellationToken cancellationToken) + { + int charsRequired = _charPos + relativePosition - _charsUsed + 1; + + // it is possible that the TextReader doesn't return all data at once + // repeat read until the required text is returned or the reader is out of content + do + { + int charsRead = await ReadDataAsync(append, charsRequired, cancellationToken).ConfigureAwait(false); + + // no more content + if (charsRead == 0) + { + return false; + } + + charsRequired -= charsRead; + } while (charsRequired > 0); + + return true; + } + + private async Task ParseObjectAsync(CancellationToken cancellationToken) + { + while (true) + { + char currentChar = _chars[_charPos]; + + switch (currentChar) + { + case '\0': + if (_charsUsed == _charPos) + { + if (await ReadDataAsync(false, cancellationToken).ConfigureAwait(false) == 0) + { + return false; + } + } + else + { + _charPos++; + } + + break; + case '}': + SetToken(JsonToken.EndObject); + _charPos++; + return true; + case '/': + await ParseCommentAsync(true, cancellationToken).ConfigureAwait(false); + return true; + case StringUtils.CarriageReturn: + await ProcessCarriageReturnAsync(false, cancellationToken).ConfigureAwait(false); + break; + case StringUtils.LineFeed: + ProcessLineFeed(); + break; + case ' ': + case StringUtils.Tab: + + // eat + _charPos++; + break; + default: + if (char.IsWhiteSpace(currentChar)) + { + // eat + _charPos++; + } + else + { + return await ParsePropertyAsync(cancellationToken).ConfigureAwait(false); + } + + break; + } + } + } + + private async Task ParseCommentAsync(bool setToken, CancellationToken cancellationToken) + { + // should have already parsed / character before reaching this method + _charPos++; + + if (!await EnsureCharsAsync(1, false, cancellationToken).ConfigureAwait(false)) + { + throw JsonReaderException.Create(this, "Unexpected end while parsing comment."); + } + + bool singlelineComment; + + if (_chars[_charPos] == '*') + { + singlelineComment = false; + } + else if (_chars[_charPos] == '/') + { + singlelineComment = true; + } + else + { + throw JsonReaderException.Create(this, "Error parsing comment. Expected: *, got {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos])); + } + + _charPos++; + + int initialPosition = _charPos; + + while (true) + { + switch (_chars[_charPos]) + { + case '\0': + if (_charsUsed == _charPos) + { + if (await ReadDataAsync(true, cancellationToken).ConfigureAwait(false) == 0) + { + if (!singlelineComment) + { + throw JsonReaderException.Create(this, "Unexpected end while parsing comment."); + } + + EndComment(setToken, initialPosition, _charPos); + return; + } + } + else + { + _charPos++; + } + + break; + case '*': + _charPos++; + + if (!singlelineComment) + { + if (await EnsureCharsAsync(0, true, cancellationToken).ConfigureAwait(false)) + { + if (_chars[_charPos] == '/') + { + EndComment(setToken, initialPosition, _charPos - 1); + + _charPos++; + return; + } + } + } + + break; + case StringUtils.CarriageReturn: + if (singlelineComment) + { + EndComment(setToken, initialPosition, _charPos); + return; + } + + await ProcessCarriageReturnAsync(true, cancellationToken).ConfigureAwait(false); + break; + case StringUtils.LineFeed: + if (singlelineComment) + { + EndComment(setToken, initialPosition, _charPos); + return; + } + + ProcessLineFeed(); + break; + default: + _charPos++; + break; + } + } + } + + private async Task ParsePostValueAsync(CancellationToken cancellationToken) + { + while (true) + { + char currentChar = _chars[_charPos]; + + switch (currentChar) + { + case '\0': + if (_charsUsed == _charPos) + { + if (await ReadDataAsync(false, cancellationToken).ConfigureAwait(false) == 0) + { + _currentState = State.Finished; + return false; + } + } + else + { + _charPos++; + } + + break; + case '}': + _charPos++; + SetToken(JsonToken.EndObject); + return true; + case ']': + _charPos++; + SetToken(JsonToken.EndArray); + return true; + case ')': + _charPos++; + SetToken(JsonToken.EndConstructor); + return true; + case '/': + await ParseCommentAsync(true, cancellationToken).ConfigureAwait(false); + return true; + case ',': + _charPos++; + + // finished parsing + SetStateBasedOnCurrent(); + return false; + case ' ': + case StringUtils.Tab: + + // eat + _charPos++; + break; + case StringUtils.CarriageReturn: + await ProcessCarriageReturnAsync(false, cancellationToken).ConfigureAwait(false); + break; + case StringUtils.LineFeed: + ProcessLineFeed(); + break; + default: + if (char.IsWhiteSpace(currentChar)) + { + // eat + _charPos++; + } + else + { + throw JsonReaderException.Create(this, "After parsing a value an unexpected character was encountered: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar)); + } + + break; + } + } + } + + private async Task EatWhitespaceAsync(bool oneOrMore, CancellationToken cancellationToken) + { + bool finished = false; + bool ateWhitespace = false; + while (!finished) + { + char currentChar = _chars[_charPos]; + + switch (currentChar) + { + case '\0': + if (_charsUsed == _charPos) + { + if (await ReadDataAsync(false, cancellationToken).ConfigureAwait(false) == 0) + { + finished = true; + } + } + else + { + _charPos++; + } + break; + case StringUtils.CarriageReturn: + await ProcessCarriageReturnAsync(false, cancellationToken).ConfigureAwait(false); + break; + case StringUtils.LineFeed: + ProcessLineFeed(); + break; + default: + if (currentChar == ' ' || char.IsWhiteSpace(currentChar)) + { + ateWhitespace = true; + _charPos++; + } + else + { + finished = true; + } + break; + } + } + + return !oneOrMore || ateWhitespace; + } + + private async Task ParseStringAsync(char quote, ReadType readType, CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + _charPos++; + + ShiftBufferIfNeeded(); + await ReadStringIntoBufferAsync(quote, cancellationToken).ConfigureAwait(false); + ParseReadString(quote, readType); + } + + private async Task MatchValueAsync(string value, CancellationToken cancellationToken) + { + return MatchValue(await EnsureCharsAsync(value.Length - 1, true, cancellationToken).ConfigureAwait(false), value); + } + + private async Task MatchValueWithTrailingSeparatorAsync(string value, CancellationToken cancellationToken) + { + // will match value and then move to the next character, checking that it is a separator character + if (!await MatchValueAsync(value, cancellationToken).ConfigureAwait(false)) + { + return false; + } + + if (!await EnsureCharsAsync(0, false, cancellationToken).ConfigureAwait(false)) + { + return true; + } + + return IsSeparator(_chars[_charPos]) || _chars[_charPos] == '\0'; + } + + private async Task MatchAndSetAsync(string value, JsonToken newToken, object tokenValue, CancellationToken cancellationToken) + { + if (await MatchValueWithTrailingSeparatorAsync(value, cancellationToken).ConfigureAwait(false)) + { + SetToken(newToken, tokenValue); + } + else + { + throw JsonReaderException.Create(this, "Error parsing " + newToken.ToString().ToLowerInvariant() + " value."); + } + } + + private Task ParseTrueAsync(CancellationToken cancellationToken) + { + return MatchAndSetAsync(JsonConvert.True, JsonToken.Boolean, true, cancellationToken); + } + + private Task ParseFalseAsync(CancellationToken cancellationToken) + { + return MatchAndSetAsync(JsonConvert.False, JsonToken.Boolean, false, cancellationToken); + } + + private Task ParseNullAsync(CancellationToken cancellationToken) + { + return MatchAndSetAsync(JsonConvert.Null, JsonToken.Null, null, cancellationToken); + } + + private async Task ParseConstructorAsync(CancellationToken cancellationToken) + { + if (await MatchValueWithTrailingSeparatorAsync("new", cancellationToken).ConfigureAwait(false)) + { + await EatWhitespaceAsync(false, cancellationToken).ConfigureAwait(false); + + int initialPosition = _charPos; + int endPosition; + + while (true) + { + char currentChar = _chars[_charPos]; + if (currentChar == '\0') + { + if (_charsUsed == _charPos) + { + if (await ReadDataAsync(true, cancellationToken).ConfigureAwait(false) == 0) + { + throw JsonReaderException.Create(this, "Unexpected end while parsing constructor."); + } + } + else + { + endPosition = _charPos; + _charPos++; + break; + } + } + else if (char.IsLetterOrDigit(currentChar)) + { + _charPos++; + } + else if (currentChar == StringUtils.CarriageReturn) + { + endPosition = _charPos; + await ProcessCarriageReturnAsync(true, cancellationToken).ConfigureAwait(false); + break; + } + else if (currentChar == StringUtils.LineFeed) + { + endPosition = _charPos; + ProcessLineFeed(); + break; + } + else if (char.IsWhiteSpace(currentChar)) + { + endPosition = _charPos; + _charPos++; + break; + } + else if (currentChar == '(') + { + endPosition = _charPos; + break; + } + else + { + throw JsonReaderException.Create(this, "Unexpected character while parsing constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar)); + } + } + + _stringReference = new StringReference(_chars, initialPosition, endPosition - initialPosition); + string constructorName = _stringReference.ToString(); + + EatWhitespace(false); + + if (_chars[_charPos] != '(') + { + throw JsonReaderException.Create(this, "Unexpected character while parsing constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos])); + } + + _charPos++; + + ClearRecentString(); + + SetToken(JsonToken.StartConstructor, constructorName); + } + else + { + throw JsonReaderException.Create(this, "Unexpected content while parsing JSON."); + } + } + + private async Task ParseNumberNaNAsync(ReadType readType, CancellationToken cancellationToken) + { + return ParseNumberNaN(readType, await MatchValueWithTrailingSeparatorAsync(JsonConvert.NaN, cancellationToken).ConfigureAwait(false)); + } + + private async Task ParseNumberPositiveInfinityAsync(ReadType readType, CancellationToken cancellationToken) + { + return ParseNumberPositiveInfinity(readType, await MatchValueWithTrailingSeparatorAsync(JsonConvert.PositiveInfinity, cancellationToken).ConfigureAwait(false)); + } + + private async Task ParseNumberNegativeInfinityAsync(ReadType readType, CancellationToken cancellationToken) + { + return ParseNumberNegativeInfinity(readType, await MatchValueWithTrailingSeparatorAsync(JsonConvert.NegativeInfinity, cancellationToken).ConfigureAwait(false)); + } + + private async Task ParseNumberAsync(ReadType readType, CancellationToken cancellationToken) + { + ShiftBufferIfNeeded(); + + char firstChar = _chars[_charPos]; + int initialPosition = _charPos; + + await ReadNumberIntoBufferAsync(cancellationToken).ConfigureAwait(false); + + ParseReadNumber(readType, firstChar, initialPosition); + } + + private Task ParseUndefinedAsync(CancellationToken cancellationToken) + { + return MatchAndSetAsync(JsonConvert.Undefined, JsonToken.Undefined, null, cancellationToken); + } + + private async Task ParsePropertyAsync(CancellationToken cancellationToken) + { + char firstChar = _chars[_charPos]; + char quoteChar; + + if (firstChar == '"' || firstChar == '\'') + { + _charPos++; + quoteChar = firstChar; + ShiftBufferIfNeeded(); + await ReadStringIntoBufferAsync(quoteChar, cancellationToken).ConfigureAwait(false); + } + else if (ValidIdentifierChar(firstChar)) + { + quoteChar = '\0'; + ShiftBufferIfNeeded(); + await ParseUnquotedPropertyAsync(cancellationToken).ConfigureAwait(false); + } + else + { + throw JsonReaderException.Create(this, "Invalid property identifier character: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos])); + } + + string propertyName; + + if (NameTable != null) + { + propertyName = NameTable.Get(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length) + // no match in name table + ?? _stringReference.ToString(); + } + else + { + propertyName = _stringReference.ToString(); + } + + await EatWhitespaceAsync(false, cancellationToken).ConfigureAwait(false); + + if (_chars[_charPos] != ':') + { + throw JsonReaderException.Create(this, "Invalid character after parsing property name. Expected ':' but got: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos])); + } + + _charPos++; + + SetToken(JsonToken.PropertyName, propertyName); + _quoteChar = quoteChar; + ClearRecentString(); + + return true; + } + + private async Task ReadNumberIntoBufferAsync(CancellationToken cancellationToken) + { + int charPos = _charPos; + + while (true) + { + char currentChar = _chars[charPos]; + if (currentChar == '\0') + { + _charPos = charPos; + + if (_charsUsed == charPos) + { + if (await ReadDataAsync(true, cancellationToken).ConfigureAwait(false) == 0) + { + return; + } + } + else + { + return; + } + } + else if (ReadNumberCharIntoBuffer(currentChar, charPos)) + { + return; + } + else + { + charPos++; + } + } + } + + private async Task ParseUnquotedPropertyAsync(CancellationToken cancellationToken) + { + int initialPosition = _charPos; + + // parse unquoted property name until whitespace or colon + while (true) + { + char currentChar = _chars[_charPos]; + if (currentChar == '\0') + { + if (_charsUsed == _charPos) + { + if (await ReadDataAsync(true, cancellationToken).ConfigureAwait(false) == 0) + { + throw JsonReaderException.Create(this, "Unexpected end while parsing unquoted property name."); + } + + continue; + } + + _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); + return; + } + + if (ReadUnquotedPropertyReportIfDone(currentChar, initialPosition)) + { + return; + } + } + } + + private async Task ReadNullCharAsync(CancellationToken cancellationToken) + { + if (_charsUsed == _charPos) + { + if (await ReadDataAsync(false, cancellationToken).ConfigureAwait(false) == 0) + { + _isEndOfFile = true; + return true; + } + } + else + { + _charPos++; + } + + return false; + } + + private async Task HandleNullAsync(CancellationToken cancellationToken) + { + if (await EnsureCharsAsync(1, true, cancellationToken).ConfigureAwait(false)) + { + if (_chars[_charPos + 1] == 'u') + { + await ParseNullAsync(cancellationToken).ConfigureAwait(false); + return; + } + + _charPos += 2; + throw CreateUnexpectedCharacterException(_chars[_charPos - 1]); + } + + _charPos = _charsUsed; + throw CreateUnexpectedEndException(); + } + + private async Task ReadFinishedAsync(CancellationToken cancellationToken) + { + if (await EnsureCharsAsync(0, false, cancellationToken).ConfigureAwait(false)) + { + await EatWhitespaceAsync(false, cancellationToken).ConfigureAwait(false); + if (_isEndOfFile) + { + return; + } + + if (_chars[_charPos] == '/') + { + await ParseCommentAsync(false, cancellationToken).ConfigureAwait(false); + } + else + { + throw JsonReaderException.Create(this, "Additional text encountered after finished reading JSON content: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos])); + } + } + + SetToken(JsonToken.None); + } + + private async Task ReadStringValueAsync(ReadType readType, CancellationToken cancellationToken) + { + EnsureBuffer(); + + switch (_currentState) + { + case State.Start: + case State.Property: + case State.Array: + case State.ArrayStart: + case State.Constructor: + case State.ConstructorStart: + case State.PostValue: + while (true) + { + char currentChar = _chars[_charPos]; + + switch (currentChar) + { + case '\0': + if (await ReadNullCharAsync(cancellationToken).ConfigureAwait(false)) + { + SetToken(JsonToken.None, null, false); + return null; + } + + break; + case '"': + case '\'': + await ParseStringAsync(currentChar, readType, cancellationToken).ConfigureAwait(false); + return FinishReadQuotedStringValue(readType); + case '-': + if (await EnsureCharsAsync(1, true, cancellationToken).ConfigureAwait(false) && _chars[_charPos + 1] == 'I') + { + return ParseNumberNegativeInfinity(readType); + } + else + { + await ParseNumberAsync(readType, cancellationToken).ConfigureAwait(false); + return Value; + } + case '.': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + if (readType != ReadType.ReadAsString) + { + _charPos++; + throw CreateUnexpectedCharacterException(currentChar); + } + + await ParseNumberAsync(ReadType.ReadAsString, cancellationToken).ConfigureAwait(false); + return Value; + case 't': + case 'f': + if (readType != ReadType.ReadAsString) + { + _charPos++; + throw CreateUnexpectedCharacterException(currentChar); + } + + string expected = currentChar == 't' ? JsonConvert.True : JsonConvert.False; + if (!await MatchValueWithTrailingSeparatorAsync(expected, cancellationToken).ConfigureAwait(false)) + { + throw CreateUnexpectedCharacterException(_chars[_charPos]); + } + + SetToken(JsonToken.String, expected); + return expected; + case 'I': + return await ParseNumberPositiveInfinityAsync(readType, cancellationToken).ConfigureAwait(false); + case 'N': + return await ParseNumberNaNAsync(readType, cancellationToken).ConfigureAwait(false); + case 'n': + await HandleNullAsync(cancellationToken).ConfigureAwait(false); + return null; + case '/': + await ParseCommentAsync(false, cancellationToken).ConfigureAwait(false); + break; + case ',': + ProcessValueComma(); + break; + case ']': + _charPos++; + if (_currentState == State.Array || _currentState == State.ArrayStart || _currentState == State.PostValue) + { + SetToken(JsonToken.EndArray); + return null; + } + + throw CreateUnexpectedCharacterException(currentChar); + case StringUtils.CarriageReturn: + await ProcessCarriageReturnAsync(false, cancellationToken).ConfigureAwait(false); + break; + case StringUtils.LineFeed: + ProcessLineFeed(); + break; + case ' ': + case StringUtils.Tab: + + // eat + _charPos++; + break; + default: + _charPos++; + + if (!char.IsWhiteSpace(currentChar)) + { + throw CreateUnexpectedCharacterException(currentChar); + } + + // eat + break; + } + } + case State.Finished: + await ReadFinishedAsync(cancellationToken).ConfigureAwait(false); + return null; + default: + throw JsonReaderException.Create(this, "Unexpected state: {0}.".FormatWith(CultureInfo.InvariantCulture, CurrentState)); + } + } + + private async Task ReadNumberValueAsync(ReadType readType, CancellationToken cancellationToken) + { + EnsureBuffer(); + + switch (_currentState) + { + case State.Start: + case State.Property: + case State.Array: + case State.ArrayStart: + case State.Constructor: + case State.ConstructorStart: + case State.PostValue: + while (true) + { + char currentChar = _chars[_charPos]; + + switch (currentChar) + { + case '\0': + if (await ReadNullCharAsync(cancellationToken).ConfigureAwait(false)) + { + SetToken(JsonToken.None, null, false); + return null; + } + + break; + case '"': + case '\'': + await ParseStringAsync(currentChar, readType, cancellationToken).ConfigureAwait(false); + return FinishReadQuotedNumber(readType); + case 'n': + await HandleNullAsync(cancellationToken).ConfigureAwait(false); + return null; + case 'N': + return await ParseNumberNaNAsync(readType, cancellationToken).ConfigureAwait(false); + case 'I': + return await ParseNumberPositiveInfinityAsync(readType, cancellationToken).ConfigureAwait(false); + case '-': + if (await EnsureCharsAsync(1, true, cancellationToken).ConfigureAwait(false) && _chars[_charPos + 1] == 'I') + { + return await ParseNumberNegativeInfinityAsync(readType, cancellationToken).ConfigureAwait(false); + } + else + { + await ParseNumberAsync(readType, cancellationToken).ConfigureAwait(false); + return Value; + } + case '.': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + await ParseNumberAsync(readType, cancellationToken).ConfigureAwait(false); + return Value; + case '/': + await ParseCommentAsync(false, cancellationToken).ConfigureAwait(false); + break; + case ',': + ProcessValueComma(); + break; + case ']': + _charPos++; + if (_currentState == State.Array || _currentState == State.ArrayStart || _currentState == State.PostValue) + { + SetToken(JsonToken.EndArray); + return null; + } + + throw CreateUnexpectedCharacterException(currentChar); + case StringUtils.CarriageReturn: + await ProcessCarriageReturnAsync(false, cancellationToken).ConfigureAwait(false); + break; + case StringUtils.LineFeed: + ProcessLineFeed(); + break; + case ' ': + case StringUtils.Tab: + + // eat + _charPos++; + break; + default: + _charPos++; + + if (!char.IsWhiteSpace(currentChar)) + { + throw CreateUnexpectedCharacterException(currentChar); + } + + // eat + break; + } + } + case State.Finished: + await ReadFinishedAsync(cancellationToken).ConfigureAwait(false); + return null; + default: + throw JsonReaderException.Create(this, "Unexpected state: {0}.".FormatWith(CultureInfo.InvariantCulture, CurrentState)); + } + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task ReadAsBooleanAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoReadAsBooleanAsync(cancellationToken) : base.ReadAsBooleanAsync(cancellationToken); + } + + internal async Task DoReadAsBooleanAsync(CancellationToken cancellationToken) + { + EnsureBuffer(); + + switch (_currentState) + { + case State.Start: + case State.Property: + case State.Array: + case State.ArrayStart: + case State.Constructor: + case State.ConstructorStart: + case State.PostValue: + while (true) + { + char currentChar = _chars[_charPos]; + + switch (currentChar) + { + case '\0': + if (await ReadNullCharAsync(cancellationToken).ConfigureAwait(false)) + { + SetToken(JsonToken.None, null, false); + return null; + } + + break; + case '"': + case '\'': + await ParseStringAsync(currentChar, ReadType.Read, cancellationToken).ConfigureAwait(false); + return ReadBooleanString(_stringReference.ToString()); + case 'n': + await HandleNullAsync(cancellationToken).ConfigureAwait(false); + return null; + case '-': + case '.': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + await ParseNumberAsync(ReadType.Read, cancellationToken).ConfigureAwait(false); + bool b; +#if !PORTABLE || NETSTANDARD1_1 + if (Value is BigInteger) + { + b = (BigInteger)Value != 0; + } + else +#endif + { + b = Convert.ToBoolean(Value, CultureInfo.InvariantCulture); + } + SetToken(JsonToken.Boolean, b, false); + return b; + case 't': + case 'f': + bool isTrue = currentChar == 't'; + if (!await MatchValueWithTrailingSeparatorAsync(isTrue ? JsonConvert.True : JsonConvert.False, cancellationToken).ConfigureAwait(false)) + { + throw CreateUnexpectedCharacterException(_chars[_charPos]); + } + + SetToken(JsonToken.Boolean, isTrue); + return isTrue; + case '/': + await ParseCommentAsync(false, cancellationToken).ConfigureAwait(false); + break; + case ',': + ProcessValueComma(); + break; + case ']': + _charPos++; + if (_currentState == State.Array || _currentState == State.ArrayStart || _currentState == State.PostValue) + { + SetToken(JsonToken.EndArray); + return null; + } + + throw CreateUnexpectedCharacterException(currentChar); + case StringUtils.CarriageReturn: + await ProcessCarriageReturnAsync(false, cancellationToken).ConfigureAwait(false); + break; + case StringUtils.LineFeed: + ProcessLineFeed(); + break; + case ' ': + case StringUtils.Tab: + + // eat + _charPos++; + break; + default: + _charPos++; + + if (!char.IsWhiteSpace(currentChar)) + { + throw CreateUnexpectedCharacterException(currentChar); + } + + // eat + break; + } + } + case State.Finished: + await ReadFinishedAsync(cancellationToken).ConfigureAwait(false); + return null; + default: + throw JsonReaderException.Create(this, "Unexpected state: {0}.".FormatWith(CultureInfo.InvariantCulture, CurrentState)); + } + } + + /// + /// Asynchronously reads the next JSON token from the source as a []. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the []. This result will be null at the end of an array. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task ReadAsBytesAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoReadAsBytesAsync(cancellationToken) : base.ReadAsBytesAsync(cancellationToken); + } + + internal async Task DoReadAsBytesAsync(CancellationToken cancellationToken) + { + EnsureBuffer(); + bool isWrapped = false; + + switch (_currentState) + { + case State.Start: + case State.Property: + case State.Array: + case State.ArrayStart: + case State.Constructor: + case State.ConstructorStart: + case State.PostValue: + while (true) + { + char currentChar = _chars[_charPos]; + + switch (currentChar) + { + case '\0': + if (await ReadNullCharAsync(cancellationToken).ConfigureAwait(false)) + { + SetToken(JsonToken.None, null, false); + return null; + } + + break; + case '"': + case '\'': + await ParseStringAsync(currentChar, ReadType.ReadAsBytes, cancellationToken).ConfigureAwait(false); + byte[] data = (byte[])Value; + if (isWrapped) + { + await ReaderReadAndAssertAsync(cancellationToken).ConfigureAwait(false); + if (TokenType != JsonToken.EndObject) + { + throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType)); + } + + SetToken(JsonToken.Bytes, data, false); + } + + return data; + case '{': + _charPos++; + SetToken(JsonToken.StartObject); + await ReadIntoWrappedTypeObjectAsync(cancellationToken).ConfigureAwait(false); + isWrapped = true; + break; + case '[': + _charPos++; + SetToken(JsonToken.StartArray); + return await ReadArrayIntoByteArrayAsync(cancellationToken).ConfigureAwait(false); + case 'n': + await HandleNullAsync(cancellationToken).ConfigureAwait(false); + return null; + case '/': + await ParseCommentAsync(false, cancellationToken).ConfigureAwait(false); + break; + case ',': + ProcessValueComma(); + break; + case ']': + _charPos++; + if (_currentState == State.Array || _currentState == State.ArrayStart || _currentState == State.PostValue) + { + SetToken(JsonToken.EndArray); + return null; + } + + throw CreateUnexpectedCharacterException(currentChar); + case StringUtils.CarriageReturn: + await ProcessCarriageReturnAsync(false, cancellationToken).ConfigureAwait(false); + break; + case StringUtils.LineFeed: + ProcessLineFeed(); + break; + case ' ': + case StringUtils.Tab: + + // eat + _charPos++; + break; + default: + _charPos++; + + if (!char.IsWhiteSpace(currentChar)) + { + throw CreateUnexpectedCharacterException(currentChar); + } + + // eat + break; + } + } + case State.Finished: + await ReadFinishedAsync(cancellationToken).ConfigureAwait(false); + return null; + default: + throw JsonReaderException.Create(this, "Unexpected state: {0}.".FormatWith(CultureInfo.InvariantCulture, CurrentState)); + } + } + + private async Task ReadIntoWrappedTypeObjectAsync(CancellationToken cancellationToken) + { + await ReaderReadAndAssertAsync(cancellationToken).ConfigureAwait(false); + if (Value != null && Value.ToString() == JsonTypeReflector.TypePropertyName) + { + await ReaderReadAndAssertAsync(cancellationToken).ConfigureAwait(false); + if (Value != null && Value.ToString().StartsWith("System.Byte[]", StringComparison.Ordinal)) + { + await ReaderReadAndAssertAsync(cancellationToken).ConfigureAwait(false); + if (Value.ToString() == JsonTypeReflector.ValuePropertyName) + { + return; + } + } + } + + throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, JsonToken.StartObject)); + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task ReadAsDateTimeAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoReadAsDateTimeAsync(cancellationToken) : base.ReadAsDateTimeAsync(cancellationToken); + } + + internal async Task DoReadAsDateTimeAsync(CancellationToken cancellationToken) + { + return (DateTime?)await ReadStringValueAsync(ReadType.ReadAsDateTime, cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task ReadAsDateTimeOffsetAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoReadAsDateTimeOffsetAsync(cancellationToken) : base.ReadAsDateTimeOffsetAsync(cancellationToken); + } + + internal async Task DoReadAsDateTimeOffsetAsync(CancellationToken cancellationToken) + { + return (DateTimeOffset?)await ReadStringValueAsync(ReadType.ReadAsDateTimeOffset, cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task ReadAsDecimalAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoReadAsDecimalAsync(cancellationToken) : base.ReadAsDecimalAsync(cancellationToken); + } + + internal async Task DoReadAsDecimalAsync(CancellationToken cancellationToken) + { + return (decimal?)await ReadNumberValueAsync(ReadType.ReadAsDecimal, cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task ReadAsDoubleAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoReadAsDoubleAsync(cancellationToken) : base.ReadAsDoubleAsync(cancellationToken); + } + + internal async Task DoReadAsDoubleAsync(CancellationToken cancellationToken) + { + return (double?)await ReadNumberValueAsync(ReadType.ReadAsDouble, cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously reads the next JSON token from the source as a of . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the of . This result will be null at the end of an array. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task ReadAsInt32Async(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoReadAsInt32Async(cancellationToken) : base.ReadAsInt32Async(cancellationToken); + } + + internal async Task DoReadAsInt32Async(CancellationToken cancellationToken) + { + return (int?)await ReadNumberValueAsync(ReadType.ReadAsInt32, cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously reads the next JSON token from the source as a . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous read. The + /// property returns the . This result will be null at the end of an array. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task ReadAsStringAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoReadAsStringAsync(cancellationToken) : base.ReadAsStringAsync(cancellationToken); + } + + internal async Task DoReadAsStringAsync(CancellationToken cancellationToken) + { + return (string)await ReadStringValueAsync(ReadType.ReadAsString, cancellationToken).ConfigureAwait(false); + } + } +} + +#endif diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index 389ab585..7db4820a 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -24,10 +24,7 @@ #endregion using System; -using System.Collections.Generic; -using System.Diagnostics; using System.Runtime.CompilerServices; -using System.Text; using System.IO; using System.Globalization; #if !(PORTABLE || PORTABLE40 || NET35 || NET20) || NETSTANDARD1_1 @@ -55,7 +52,7 @@ namespace Newtonsoft.Json /// /// Represents a reader that provides fast, non-cached, forward-only access to JSON text data. /// - public class JsonTextReader : JsonReader, IJsonLineInfo + public partial class JsonTextReader : JsonReader, IJsonLineInfo { private const char UnicodeReplacementChar = '\uFFFD'; private const int MaximumJavascriptIntegerCharacterLength = 380; @@ -85,6 +82,10 @@ namespace Newtonsoft.Json _reader = reader; _lineNumber = 1; + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + _safeAsync = GetType() == typeof(JsonTextReader); +#endif } #if DEBUG @@ -131,6 +132,11 @@ namespace Newtonsoft.Json ShiftBufferIfNeeded(); ReadStringIntoBuffer(quote); + ParseReadString(quote, readType); + } + + private void ParseReadString(char quote, ReadType readType) + { SetPostValueState(true); switch (readType) @@ -243,13 +249,8 @@ namespace Newtonsoft.Json return ReadData(append, 0); } - private int ReadData(bool append, int charsRequired) + private void PrepareBufferForReadData(bool append, int charsRequired) { - if (_isEndOfFile) - { - return 0; - } - // char buffer is full if (_charsUsed + charsRequired >= _chars.Length - 1) { @@ -299,6 +300,16 @@ namespace Newtonsoft.Json _charsUsed = remainingCharCount; } } + } + + private int ReadData(bool append, int charsRequired) + { + if (_isEndOfFile) + { + return 0; + } + + PrepareBufferForReadData(append, charsRequired); int attemptCharReadCount = _chars.Length - _charsUsed - 1; @@ -574,29 +585,7 @@ namespace Newtonsoft.Json case '"': case '\'': ParseString(currentChar, readType); - switch (readType) - { - case ReadType.ReadAsBytes: - return Value; - case ReadType.ReadAsString: - return Value; - case ReadType.ReadAsDateTime: - if (Value is DateTime) - { - return (DateTime)Value; - } - return ReadDateTimeString((string)Value); -#if !NET20 - case ReadType.ReadAsDateTimeOffset: - if (Value is DateTimeOffset) - { - return (DateTimeOffset)Value; - } - return ReadDateTimeOffsetString((string)Value); -#endif - default: - throw new ArgumentOutOfRangeException(nameof(readType)); - } + return FinishReadQuotedStringValue(readType); case '-': if (EnsureChars(1, true) && _chars[_charPos + 1] == 'I') { @@ -691,6 +680,34 @@ namespace Newtonsoft.Json } } + private object FinishReadQuotedStringValue(ReadType readType) + { + switch (readType) + { + case ReadType.ReadAsBytes: + case ReadType.ReadAsString: + return Value; + case ReadType.ReadAsDateTime: + if (Value is DateTime) + { + return (DateTime)Value; + } + + return ReadDateTimeString((string)Value); +#if !NET20 + case ReadType.ReadAsDateTimeOffset: + if (Value is DateTimeOffset) + { + return (DateTimeOffset)Value; + } + + return ReadDateTimeOffsetString((string)Value); +#endif + default: + throw new ArgumentOutOfRangeException(nameof(readType)); + } + } + private JsonReaderException CreateUnexpectedCharacterException(char c) { return JsonReaderException.Create(this, "Unexpected character encountered while parsing value: {0}.".FormatWith(CultureInfo.InvariantCulture, c)); @@ -857,17 +874,7 @@ namespace Newtonsoft.Json case '"': case '\'': ParseString(currentChar, readType); - switch (readType) - { - case ReadType.ReadAsInt32: - return ReadInt32String(_stringReference.ToString()); - case ReadType.ReadAsDecimal: - return ReadDecimalString(_stringReference.ToString()); - case ReadType.ReadAsDouble: - return ReadDoubleString(_stringReference.ToString()); - default: - throw new ArgumentOutOfRangeException(nameof(readType)); - } + return FinishReadQuotedNumber(readType); case 'n': HandleNull(); return null; @@ -943,6 +950,21 @@ namespace Newtonsoft.Json } } + private object FinishReadQuotedNumber(ReadType readType) + { + switch (readType) + { + case ReadType.ReadAsInt32: + return ReadInt32String(_stringReference.ToString()); + case ReadType.ReadAsDecimal: + return ReadDecimalString(_stringReference.ToString()); + case ReadType.ReadAsDouble: + return ReadDoubleString(_stringReference.ToString()); + default: + throw new ArgumentOutOfRangeException(nameof(readType)); + } + } + #if !NET20 /// /// Reads the next JSON token from the underlying as a of . @@ -1186,26 +1208,7 @@ namespace Newtonsoft.Json case '\'': if (_chars[charPos - 1] == quote) { - charPos--; - - if (initialPosition == lastWritePosition) - { - _stringReference = new StringReference(_chars, initialPosition, charPos - initialPosition); - } - else - { - EnsureBufferNotEmpty(); - - if (charPos > lastWritePosition) - { - _stringBuffer.Append(_arrayPool, _chars, lastWritePosition, charPos - lastWritePosition); - } - - _stringReference = new StringReference(_stringBuffer.InternalBuffer, 0, _stringBuffer.Position); - } - - charPos++; - _charPos = charPos; + FinishReadStringIntoBuffer(charPos - 1, initialPosition, lastWritePosition); return; } break; @@ -1213,6 +1216,27 @@ namespace Newtonsoft.Json } } + private void FinishReadStringIntoBuffer(int charPos, int initialPosition, int lastWritePosition) + { + if (initialPosition == lastWritePosition) + { + _stringReference = new StringReference(_chars, initialPosition, charPos - initialPosition); + } + else + { + EnsureBufferNotEmpty(); + + if (charPos > lastWritePosition) + { + _stringBuffer.Append(_arrayPool, _chars, lastWritePosition, charPos - lastWritePosition); + } + + _stringReference = new StringReference(_stringBuffer.InternalBuffer, 0, _stringBuffer.Position); + } + + _charPos = charPos + 1; + } + private void WriteCharToBuffer(char writeChar, int lastWritePosition, int writeToPosition) { if (writeToPosition > lastWritePosition) @@ -1223,21 +1247,23 @@ namespace Newtonsoft.Json _stringBuffer.Append(_arrayPool, writeChar); } - private char ParseUnicode() + private char ConvertUnicode(bool enoughChars) { - char writeChar; - if (EnsureChars(4, true)) + if (enoughChars) { char hexChar = Convert.ToChar(ConvertUtils.HexTextToInt(_chars, _charPos, _charPos + 4)); - writeChar = hexChar; - _charPos += 4; + return hexChar; } else { throw JsonReaderException.Create(this, "Unexpected end while parsing Unicode character."); } - return writeChar; + } + + private char ParseUnicode() + { + return ConvertUnicode(EnsureChars(4, true)); } private void ReadNumberIntoBuffer() @@ -1246,63 +1272,75 @@ namespace Newtonsoft.Json while (true) { - switch (_chars[charPos]) + char currentChar = _chars[charPos]; + if (currentChar == '\0') { - case '\0': - _charPos = charPos; + _charPos = charPos; - if (_charsUsed == charPos) - { - if (ReadData(true) == 0) - { - return; - } - } - else + if (_charsUsed == charPos) + { + if (ReadData(true) == 0) { return; } - break; - case '-': - case '+': - case 'a': - case 'A': - case 'b': - case 'B': - case 'c': - case 'C': - case 'd': - case 'D': - case 'e': - case 'E': - case 'f': - case 'F': - case 'x': - case 'X': - case '.': - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - charPos++; - break; - default: - _charPos = charPos; - - char currentChar = _chars[_charPos]; - if (char.IsWhiteSpace(currentChar) || currentChar == ',' || currentChar == '}' || currentChar == ']' || currentChar == ')' || currentChar == '/') - { - return; - } - - throw JsonReaderException.Create(this, "Unexpected character encountered while parsing number: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar)); + } + else + { + return; + } } + else if (ReadNumberCharIntoBuffer(currentChar, charPos)) + { + return; + } + else + { + charPos++; + } + } + } + + private bool ReadNumberCharIntoBuffer(char currentChar, int charPos) + { + switch (currentChar) + { + case '-': + case '+': + case 'a': + case 'A': + case 'b': + case 'B': + case 'c': + case 'C': + case 'd': + case 'D': + case 'e': + case 'E': + case 'f': + case 'F': + case 'x': + case 'X': + case '.': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + return false; + default: + _charPos = charPos; + + if (char.IsWhiteSpace(currentChar) || currentChar == ',' || currentChar == '}' || currentChar == ']' || currentChar == ')' || currentChar == '/') + { + return true; + } + + throw JsonReaderException.Create(this, "Unexpected character encountered while parsing number: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar)); } } @@ -1503,40 +1541,47 @@ namespace Newtonsoft.Json // parse unquoted property name until whitespace or colon while (true) { - switch (_chars[_charPos]) + char currentChar = _chars[_charPos]; + if (currentChar == '\0') { - case '\0': - if (_charsUsed == _charPos) + if (_charsUsed == _charPos) + { + if (ReadData(true) == 0) { - if (ReadData(true) == 0) - { - throw JsonReaderException.Create(this, "Unexpected end while parsing unquoted property name."); - } - - break; + throw JsonReaderException.Create(this, "Unexpected end while parsing unquoted property name."); } - _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); - return; - default: - char currentChar = _chars[_charPos]; + continue; + } - if (ValidIdentifierChar(currentChar)) - { - _charPos++; - break; - } - else if (char.IsWhiteSpace(currentChar) || currentChar == ':') - { - _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); - return; - } + _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); + return; + } - throw JsonReaderException.Create(this, "Invalid JavaScript property identifier character: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar)); + if (ReadUnquotedPropertyReportIfDone(currentChar, initialPosition)) + { + return; } } } + private bool ReadUnquotedPropertyReportIfDone(char currentChar, int initialPosition) + { + if (ValidIdentifierChar(currentChar)) + { + _charPos++; + return false; + } + + if (char.IsWhiteSpace(currentChar) || currentChar == ':') + { + _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); + return true; + } + + throw JsonReaderException.Create(this, "Invalid JavaScript property identifier character: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar)); + } + private bool ParseValue() { while (true) @@ -1820,6 +1865,11 @@ namespace Newtonsoft.Json ReadNumberIntoBuffer(); + ParseReadNumber(readType, firstChar, initialPosition); + } + + private void ParseReadNumber(ReadType readType, char firstChar, int initialPosition) + { // set state to PostValue now so that if there is an error parsing the number then the reader can continue SetPostValueState(true); @@ -2196,7 +2246,12 @@ namespace Newtonsoft.Json private bool MatchValue(string value) { - if (!EnsureChars(value.Length - 1, true)) + return MatchValue(EnsureChars(value.Length - 1, true), value); + } + + private bool MatchValue(bool enoughChars, string value) + { + if (!enoughChars) { _charPos = _charsUsed; throw CreateUnexpectedEndException(); @@ -2327,7 +2382,12 @@ namespace Newtonsoft.Json private object ParseNumberNegativeInfinity(ReadType readType) { - if (MatchValueWithTrailingSeparator(JsonConvert.NegativeInfinity)) + return ParseNumberNegativeInfinity(readType, MatchValueWithTrailingSeparator(JsonConvert.NegativeInfinity)); + } + + private object ParseNumberNegativeInfinity(ReadType readType, bool matched) + { + if (matched) { switch (readType) { @@ -2352,7 +2412,11 @@ namespace Newtonsoft.Json private object ParseNumberPositiveInfinity(ReadType readType) { - if (MatchValueWithTrailingSeparator(JsonConvert.PositiveInfinity)) + return ParseNumberPositiveInfinity(readType, MatchValueWithTrailingSeparator(JsonConvert.PositiveInfinity)); + } + private object ParseNumberPositiveInfinity(ReadType readType, bool matched) + { + if (matched) { switch (readType) { @@ -2377,7 +2441,12 @@ namespace Newtonsoft.Json private object ParseNumberNaN(ReadType readType) { - if (MatchValueWithTrailingSeparator(JsonConvert.NaN)) + return ParseNumberNaN(readType, MatchValueWithTrailingSeparator(JsonConvert.NaN)); + } + + private object ParseNumberNaN(ReadType readType, bool matched) + { + if (matched) { switch (readType) { @@ -2467,4 +2536,4 @@ namespace Newtonsoft.Json get { return _charPos - _lineStartPos; } } } -} \ No newline at end of file +} diff --git a/Src/Newtonsoft.Json/JsonTextWriter.Async.cs b/Src/Newtonsoft.Json/JsonTextWriter.Async.cs new file mode 100644 index 00000000..4f0a281f --- /dev/null +++ b/Src/Newtonsoft.Json/JsonTextWriter.Async.cs @@ -0,0 +1,1342 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +using System.Globalization; +using System.Threading; +#if !PORTABLE || NETSTANDARD1_1 +using System.Numerics; +#endif +using System.Threading.Tasks; +using Newtonsoft.Json.Utilities; + +namespace Newtonsoft.Json +{ + public partial class JsonTextWriter + { + // It's not safe to perform the async methods here in a derived class as if the synchronous equivalent + // has been overriden then the asychronous method will no longer be doing the same operation. +#if !(NET20 || NET35 || NET40 || PORTABLE40) // Double-check this isn't included inappropriately. + private readonly bool _safeAsync; +#endif + + /// + /// Asynchronously flushes whatever is in the buffer to the destination and also flushes the destination. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoFlushAsync(cancellationToken) : base.FlushAsync(cancellationToken); + } + + internal Task DoFlushAsync(CancellationToken cancellationToken) + { + return cancellationToken.CancelIfRequestedAsync() ?? _writer.FlushAsync(); + } + + /// + /// Asynchronously writes the JSON value delimiter. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + protected override Task WriteValueDelimiterAsync(CancellationToken cancellationToken) + { + return _safeAsync ? DoWriteValueDelimiterAsync(cancellationToken) : base.WriteValueDelimiterAsync(cancellationToken); + } + + internal Task DoWriteValueDelimiterAsync(CancellationToken cancellationToken) + { + return _writer.WriteAsync(',', cancellationToken); + } + + /// + /// Asynchronously writes the specified end token. + /// + /// The end token to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + protected override Task WriteEndAsync(JsonToken token, CancellationToken cancellationToken) + { + return _safeAsync ? DoWriteEndAsync(token, cancellationToken) : base.WriteEndAsync(token, cancellationToken); + } + + internal Task DoWriteEndAsync(JsonToken token, CancellationToken cancellationToken) + { + switch (token) + { + case JsonToken.EndObject: + return _writer.WriteAsync('}', cancellationToken); + case JsonToken.EndArray: + return _writer.WriteAsync(']', cancellationToken); + case JsonToken.EndConstructor: + return _writer.WriteAsync(')', cancellationToken); + default: + throw JsonWriterException.Create(this, "Invalid JsonToken: " + token, null); + } + } + + /// + /// Asynchronously closes this writer. + /// If is set to true, the destination is also closed. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task CloseAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoCloseAsync(cancellationToken) : base.CloseAsync(cancellationToken); + } + + internal async Task DoCloseAsync(CancellationToken cancellationToken) + { + if (Top == 0) // otherwise will happen in calls to WriteEndAsync + { + cancellationToken.ThrowIfCancellationRequested(); + } + + while (Top > 0) + { + await WriteEndAsync(cancellationToken).ConfigureAwait(false); + } + + CloseBufferAndWriter(); + } + + /// + /// Asynchronously writes the end of the current JSON object or array. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteEndAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? WriteEndInternalAsync(cancellationToken) : base.WriteEndAsync(cancellationToken); + } + + /// + /// Asynchronously writes indent characters. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + protected override Task WriteIndentAsync(CancellationToken cancellationToken) + { + return _safeAsync ? DoWriteIndentAsync(cancellationToken) : base.WriteIndentAsync(cancellationToken); + } + + internal Task DoWriteIndentAsync(CancellationToken cancellationToken) + { + // levels of indentation multiplied by the indent count + int currentIndentCount = Top * _indentation; + + int newLineLen = SetIndentChars(); + + if (currentIndentCount <= IndentCharBufferSize) + { + return _writer.WriteAsync(_indentChars, 0, newLineLen + currentIndentCount, cancellationToken); + } + + return WriteIndentAsync(currentIndentCount, newLineLen, cancellationToken); + } + + private async Task WriteIndentAsync(int currentIndentCount, int newLineLen, CancellationToken cancellationToken) + { + await _writer.WriteAsync(_indentChars, 0, newLineLen + Math.Min(currentIndentCount, IndentCharBufferSize), cancellationToken).ConfigureAwait(false); + + while ((currentIndentCount -= IndentCharBufferSize) > 0) + { + await _writer.WriteAsync(_indentChars, newLineLen, Math.Min(currentIndentCount, IndentCharBufferSize), cancellationToken).ConfigureAwait(false); + } + } + + private Task WriteValueInternalAsync(JsonToken token, string value, CancellationToken cancellationToken) + { + Task task = InternalWriteValueAsync(token, cancellationToken); + if (task.IsCompleted) + { + return _writer.WriteAsync(value, cancellationToken); + } + + return WriteValueInternalAsync(task, value, cancellationToken); + } + + private async Task WriteValueInternalAsync(Task task, string value, CancellationToken cancellationToken) + { + await task.ConfigureAwait(false); + await _writer.WriteAsync(value, cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously writes an indent space. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + protected override Task WriteIndentSpaceAsync(CancellationToken cancellationToken) + { + return _safeAsync ? DoWriteIndentSpaceAsync(cancellationToken) : base.WriteIndentSpaceAsync(cancellationToken); + } + + internal Task DoWriteIndentSpaceAsync(CancellationToken cancellationToken) + { + return _writer.WriteAsync(' ', cancellationToken); + } + + /// + /// Asynchronously writes raw JSON without changing the writer's state. + /// + /// The raw JSON to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteRawAsync(string json, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteRawAsync(json, cancellationToken) : base.WriteRawAsync(json, cancellationToken); + } + + internal Task DoWriteRawAsync(string json, CancellationToken cancellationToken) + { + return _writer.WriteAsync(json, cancellationToken); + } + + /// + /// Asynchronously writes a null value. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteNullAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteNullAsync(cancellationToken) : base.WriteNullAsync(cancellationToken); + } + + internal Task DoWriteNullAsync(CancellationToken cancellationToken) + { + return WriteValueInternalAsync(JsonToken.Null, JsonConvert.Null, cancellationToken); + } + + private Task WriteDigitsAsync(ulong uvalue, bool negative, CancellationToken cancellationToken) + { + if (uvalue <= 9 & !negative) + { + return _writer.WriteAsync((char)('0' + uvalue), cancellationToken); + } + + int length = WriteNumberToBuffer(uvalue, negative); + return _writer.WriteAsync(_writeBuffer, 0, length, cancellationToken); + } + + private Task WriteIntegerValueAsync(ulong uvalue, bool negative, CancellationToken cancellationToken) + { + Task task = InternalWriteValueAsync(JsonToken.Integer, cancellationToken); + if (task.IsCompleted) + { + return WriteDigitsAsync(uvalue, negative, cancellationToken); + } + + return WriteIntegerValueAsync(task, uvalue, negative, cancellationToken); + } + + private async Task WriteIntegerValueAsync(Task task, ulong uvalue, bool negative, CancellationToken cancellationToken) + { + await task.ConfigureAwait(false); + await WriteDigitsAsync(uvalue, negative, cancellationToken).ConfigureAwait(false); + } + + internal Task WriteIntegerValueAsync(long value, CancellationToken cancellationToken) + { + bool negative = value < 0; + if (negative) + { + value = -value; + } + + return WriteIntegerValueAsync((ulong)value, negative, cancellationToken); + } + + internal Task WriteIntegerValueAsync(ulong uvalue, CancellationToken cancellationToken) + { + return WriteIntegerValueAsync(uvalue, false, cancellationToken); + } + + private Task WriteEscapedStringAsync(string value, bool quote, CancellationToken cancellationToken) + { + return JavaScriptUtils.WriteEscapedJavaScriptStringAsync(_writer, value, _quoteChar, quote, _charEscapeFlags, StringEscapeHandling, this, _writeBuffer, cancellationToken); + } + + /// + /// Asynchronously writes the property name of a name/value pair of a JSON object. + /// + /// The name of the property. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WritePropertyNameAsync(string name, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWritePropertyNameAsync(name, cancellationToken) : base.WritePropertyNameAsync(name, cancellationToken); + } + + internal async Task DoWritePropertyNameAsync(string name, CancellationToken cancellationToken) + { + await InternalWritePropertyNameAsync(name, cancellationToken).ConfigureAwait(false); + + await WriteEscapedStringAsync(name, _quoteName, cancellationToken).ConfigureAwait(false); + + await _writer.WriteAsync(':').ConfigureAwait(false); + } + + /// + /// Asynchronously writes the property name of a name/value pair of a JSON object. + /// + /// The name of the property. + /// A flag to indicate whether the text should be escaped when it is written as a JSON property name. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WritePropertyNameAsync(string name, bool escape, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWritePropertyNameAsync(name, escape, cancellationToken) : base.WritePropertyNameAsync(name, escape, cancellationToken); + } + + internal async Task DoWritePropertyNameAsync(string name, bool escape, CancellationToken cancellationToken) + { + await InternalWritePropertyNameAsync(name, cancellationToken).ConfigureAwait(false); + + if (escape) + { + await WriteEscapedStringAsync(name, _quoteName, cancellationToken).ConfigureAwait(false); + } + else + { + if (_quoteName) + { + await _writer.WriteAsync(_quoteChar).ConfigureAwait(false); + } + + await _writer.WriteAsync(name, cancellationToken).ConfigureAwait(false); + + if (_quoteName) + { + await _writer.WriteAsync(_quoteChar).ConfigureAwait(false); + } + } + + await _writer.WriteAsync(':').ConfigureAwait(false); + } + + /// + /// Asynchronously writes the beginning of a JSON array. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteStartArrayAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteStartArrayAsync(cancellationToken) : base.WriteStartArrayAsync(cancellationToken); + } + + internal Task DoWriteStartArrayAsync(CancellationToken cancellationToken) + { + Task task = InternalWriteStartAsync(JsonToken.StartArray, JsonContainerType.Array, cancellationToken); + if (task.IsCompleted) + { + return _writer.WriteAsync('[', cancellationToken); + } + + return DoWriteStartArrayAsync(task, cancellationToken); + } + + internal async Task DoWriteStartArrayAsync(Task task, CancellationToken cancellationToken) + { + await task.ConfigureAwait(false); + + await _writer.WriteAsync('[').ConfigureAwait(false); + } + + /// + /// Asynchronously writes the beginning of a JSON object. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteStartObjectAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteStartObjectAsync(cancellationToken) : base.WriteStartObjectAsync(cancellationToken); + } + + internal Task DoWriteStartObjectAsync(CancellationToken cancellationToken) + { + Task task = InternalWriteStartAsync(JsonToken.StartObject, JsonContainerType.Object, cancellationToken); + if (task.IsCompleted) + { + return _writer.WriteAsync('{', cancellationToken); + } + + return DoWriteStartObjectAsync(task, cancellationToken); + } + + internal async Task DoWriteStartObjectAsync(Task task, CancellationToken cancellationToken) + { + await task.ConfigureAwait(false); + + await _writer.WriteAsync('{').ConfigureAwait(false); + } + + /// + /// Asynchronously writes the start of a constructor with the given name. + /// + /// The name of the constructor. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteStartConstructorAsync(string name, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteStartConstructorAsync(name, cancellationToken) : base.WriteStartConstructorAsync(name, cancellationToken); + } + + private async Task DoWriteStartConstructorAsync(string name, CancellationToken cancellationToken) + { + await InternalWriteStartAsync(JsonToken.StartConstructor, JsonContainerType.Constructor, cancellationToken).ConfigureAwait(false); + + await _writer.WriteAsync("new ", cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync(name, cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync('(').ConfigureAwait(false); + } + + /// + /// Asynchronously writes an undefined value. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteUndefinedAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteUndefinedAsync(cancellationToken) : base.WriteUndefinedAsync(cancellationToken); + } + + internal Task DoWriteUndefinedAsync(CancellationToken cancellationToken) + { + Task task = InternalWriteValueAsync(JsonToken.Undefined, cancellationToken); + if (task.IsCompleted) + { + return _writer.WriteAsync(JsonConvert.Undefined, cancellationToken); + } + + return DoWriteUndefinedAsync(task, cancellationToken); + } + + private async Task DoWriteUndefinedAsync(Task task, CancellationToken cancellationToken) + { + await task.ConfigureAwait(false); + await _writer.WriteAsync(JsonConvert.Undefined, cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously writes the given white space. + /// + /// The string of white space characters. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteWhitespaceAsync(string ws, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteWhitespaceAsync(ws, cancellationToken) : base.WriteWhitespaceAsync(ws, cancellationToken); + } + + internal Task DoWriteWhitespaceAsync(string ws, CancellationToken cancellationToken) + { + InternalWriteWhitespace(ws); + return _writer.WriteAsync(ws, cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(bool value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(bool value, CancellationToken cancellationToken) + { + return WriteValueInternalAsync(JsonToken.Boolean, JsonConvert.ToString(value), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(bool? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(bool? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : DoWriteValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(byte value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? WriteIntegerValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(byte? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(byte? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : WriteIntegerValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a [] value. + /// + /// The [] value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(byte[] value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? (value == null ? WriteNullAsync(cancellationToken) : WriteValueNonNullAsync(value, cancellationToken)) : base.WriteValueAsync(value, cancellationToken); + } + + internal async Task WriteValueNonNullAsync(byte[] value, CancellationToken cancellationToken) + { + await InternalWriteValueAsync(JsonToken.Bytes, cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync(_quoteChar).ConfigureAwait(false); + await Base64Encoder.EncodeAsync(value, 0, value.Length, cancellationToken).ConfigureAwait(false); + await Base64Encoder.FlushAsync(cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync(_quoteChar).ConfigureAwait(false); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(char value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(char value, CancellationToken cancellationToken) + { + return WriteValueInternalAsync(JsonToken.String, JsonConvert.ToString(value), cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(char? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(char? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : DoWriteValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(DateTime value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal async Task DoWriteValueAsync(DateTime value, CancellationToken cancellationToken) + { + await InternalWriteValueAsync(JsonToken.Date, cancellationToken).ConfigureAwait(false); + value = DateTimeUtils.EnsureDateTime(value, DateTimeZoneHandling); + + if (string.IsNullOrEmpty(DateFormatString)) + { + int length = WriteValueToBuffer(value); + + await _writer.WriteAsync(_writeBuffer, 0, length, cancellationToken).ConfigureAwait(false); + } + else + { + await _writer.WriteAsync(_quoteChar).ConfigureAwait(false); + await _writer.WriteAsync(value.ToString(DateFormatString, Culture), cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync(_quoteChar).ConfigureAwait(false); + } + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(DateTime? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(DateTime? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : DoWriteValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(DateTimeOffset value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal async Task DoWriteValueAsync(DateTimeOffset value, CancellationToken cancellationToken) + { + await InternalWriteValueAsync(JsonToken.Date, cancellationToken).ConfigureAwait(false); + + if (string.IsNullOrEmpty(DateFormatString)) + { + int length = WriteValueToBuffer(value); + + await _writer.WriteAsync(_writeBuffer, 0, length, cancellationToken).ConfigureAwait(false); + } + else + { + await _writer.WriteAsync(_quoteChar).ConfigureAwait(false); + await _writer.WriteAsync(value.ToString(DateFormatString, Culture), cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync(_quoteChar).ConfigureAwait(false); + } + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(DateTimeOffset? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(DateTimeOffset? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : DoWriteValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(decimal value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(decimal value, CancellationToken cancellationToken) + { + return WriteValueInternalAsync(JsonToken.Float, JsonConvert.ToString(value), cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(decimal? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(decimal? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : DoWriteValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(double value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? WriteValueAsync(value, false, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task WriteValueAsync(double value, bool nullable, CancellationToken cancellationToken) + { + return WriteValueInternalAsync(JsonToken.Float, JsonConvert.ToString(value, FloatFormatHandling, QuoteChar, nullable), cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(double? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? (value.HasValue ? WriteValueAsync(value.GetValueOrDefault(), true, cancellationToken) : WriteNullAsync(cancellationToken)) : base.WriteValueAsync(value, cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(float value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? WriteValueAsync(value, false, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task WriteValueAsync(float value, bool nullable, CancellationToken cancellationToken) + { + return WriteValueInternalAsync(JsonToken.Float, JsonConvert.ToString(value, FloatFormatHandling, QuoteChar, nullable), cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(float? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? (value.HasValue ? WriteValueAsync(value.GetValueOrDefault(), true, cancellationToken) : WriteNullAsync(cancellationToken)) : base.WriteValueAsync(value, cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(Guid value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal async Task DoWriteValueAsync(Guid value, CancellationToken cancellationToken) + { + await InternalWriteValueAsync(JsonToken.String, cancellationToken).ConfigureAwait(false); + + await _writer.WriteAsync(_quoteChar).ConfigureAwait(false); +#if !(DOTNET || PORTABLE) + await _writer.WriteAsync(value.ToString("D", CultureInfo.InvariantCulture), cancellationToken).ConfigureAwait(false); +#else + await _writer.WriteAsync(value.ToString("D"), cancellationToken).ConfigureAwait(false); +#endif + await _writer.WriteAsync(_quoteChar).ConfigureAwait(false); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(Guid? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(Guid? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : DoWriteValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(int value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? WriteIntegerValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(int? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(int? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : WriteIntegerValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(long value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? WriteIntegerValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(long? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(long? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : WriteIntegerValueAsync(value.GetValueOrDefault(), cancellationToken); + } + +#if !PORTABLE || NETSTANDARD1_1 + internal Task WriteValueAsync(BigInteger value, CancellationToken cancellationToken) + { + return WriteValueInternalAsync(JsonToken.Integer, value.ToString(CultureInfo.InvariantCulture), cancellationToken); + } +#endif + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(object value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (_safeAsync) + { + if (value == null) + { + return WriteNullAsync(cancellationToken); + } +#if !PORTABLE || NETSTANDARD1_1 + if (value is BigInteger) + { + return WriteValueAsync((BigInteger)value, cancellationToken); + } +#endif + + return WriteValueAsync(this, ConvertUtils.GetTypeCode(value.GetType()), value, cancellationToken); + } + + return base.WriteValueAsync(value, cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + [CLSCompliant(false)] + public override Task WriteValueAsync(sbyte value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? WriteIntegerValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + [CLSCompliant(false)] + public override Task WriteValueAsync(sbyte? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(sbyte? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : WriteIntegerValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(short value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? WriteIntegerValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(short? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(short? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : WriteIntegerValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(string value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(string value, CancellationToken cancellationToken) + { + Task task = InternalWriteValueAsync(JsonToken.String, cancellationToken); + if (task.IsCompleted) + { + return value == null ? _writer.WriteAsync(JsonConvert.Null, cancellationToken) : WriteEscapedStringAsync(value, true, cancellationToken); + } + + return DoWriteValueAsync(task, value, cancellationToken); + } + + private async Task DoWriteValueAsync(Task task, string value, CancellationToken cancellationToken) + { + await task.ConfigureAwait(false); + await (value == null ? _writer.WriteAsync(JsonConvert.Null, cancellationToken) : WriteEscapedStringAsync(value, true, cancellationToken)).ConfigureAwait(false); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(TimeSpan value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal async Task DoWriteValueAsync(TimeSpan value, CancellationToken cancellationToken) + { + await InternalWriteValueAsync(JsonToken.String, cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync(_quoteChar, cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync(value.ToString(null, CultureInfo.InvariantCulture), cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync(_quoteChar, cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(TimeSpan? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(TimeSpan? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : DoWriteValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + [CLSCompliant(false)] + public override Task WriteValueAsync(uint value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? WriteIntegerValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + [CLSCompliant(false)] + public override Task WriteValueAsync(uint? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(uint? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : WriteIntegerValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + [CLSCompliant(false)] + public override Task WriteValueAsync(ulong value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? WriteIntegerValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + [CLSCompliant(false)] + public override Task WriteValueAsync(ulong? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(ulong? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : WriteIntegerValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteValueAsync(Uri value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? (value == null ? WriteNullAsync(cancellationToken) : WriteValueNotNullAsync(value, cancellationToken)) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task WriteValueNotNullAsync(Uri value, CancellationToken cancellationToken) + { + Task task = InternalWriteValueAsync(JsonToken.String, cancellationToken); + if (task.IsCompleted) + { + return WriteEscapedStringAsync(value.OriginalString, true, cancellationToken); + } + + return WriteValueNotNullAsync(task, value, cancellationToken); + } + + internal async Task WriteValueNotNullAsync(Task task, Uri value, CancellationToken cancellationToken) + { + await task.ConfigureAwait(false); + await WriteEscapedStringAsync(value.OriginalString, true, cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + [CLSCompliant(false)] + public override Task WriteValueAsync(ushort value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? WriteIntegerValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + [CLSCompliant(false)] + public override Task WriteValueAsync(ushort? value, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteValueAsync(value, cancellationToken) : base.WriteValueAsync(value, cancellationToken); + } + + internal Task DoWriteValueAsync(ushort? value, CancellationToken cancellationToken) + { + return value == null ? DoWriteNullAsync(cancellationToken) : WriteIntegerValueAsync(value.GetValueOrDefault(), cancellationToken); + } + + /// + /// Asynchronously writes a comment /*...*/ containing the specified text. + /// + /// Text to place inside the comment. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteCommentAsync(string text, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteCommentAsync(text, cancellationToken) : base.WriteCommentAsync(text, cancellationToken); + } + + internal async Task DoWriteCommentAsync(string text, CancellationToken cancellationToken) + { + await InternalWriteCommentAsync(cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync("/*", cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync(text, cancellationToken).ConfigureAwait(false); + await _writer.WriteAsync("*/", cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously writes the end of an array. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteEndArrayAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? InternalWriteEndAsync(JsonContainerType.Array, cancellationToken) : base.WriteEndArrayAsync(cancellationToken); + } + + /// + /// Asynchronously writes the end of a constructor. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteEndConstructorAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? InternalWriteEndAsync(JsonContainerType.Constructor, cancellationToken) : base.WriteEndConstructorAsync(cancellationToken); + } + + /// + /// Asynchronously writes the end of a JSON object. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteEndObjectAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? InternalWriteEndAsync(JsonContainerType.Object, cancellationToken) : base.WriteEndObjectAsync(cancellationToken); + } + + /// + /// Asynchronously writes raw JSON where a value is expected and updates the writer's state. + /// + /// The raw JSON to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// Derived classes must override this method to get asynchronous behaviour. Otherwise it will + /// execute synchronously, returning an already-completed task. + public override Task WriteRawValueAsync(string json, CancellationToken cancellationToken = default(CancellationToken)) + { + return _safeAsync ? DoWriteRawValueAsync(json, cancellationToken) : base.WriteRawValueAsync(json, cancellationToken); + } + + internal Task DoWriteRawValueAsync(string json, CancellationToken cancellationToken) + { + UpdateScopeWithFinishedValue(); + Task task = AutoCompleteAsync(JsonToken.Undefined, cancellationToken); + if (task.IsCompleted) + { + return WriteRawAsync(json, cancellationToken); + } + + return DoWriteRawValueAsync(task, json, cancellationToken); + } + + private async Task DoWriteRawValueAsync(Task task, string json, CancellationToken cancellationToken) + { + await task.ConfigureAwait(false); + await WriteRawAsync(json, cancellationToken).ConfigureAwait(false); + } + + internal char[] EnsureWriteBuffer(int length, int copyTo) + { + if (length < 35) + { + length = 35; + } + + char[] buffer = _writeBuffer; + if (buffer == null) + { + return _writeBuffer = BufferUtils.RentBuffer(_arrayPool, length); + } + + if (buffer.Length >= length) + { + return buffer; + } + + char[] newBuffer = BufferUtils.RentBuffer(_arrayPool, length); + if (copyTo != 0) + { + Array.Copy(buffer, newBuffer, copyTo); + } + + BufferUtils.ReturnBuffer(_arrayPool, buffer); + _writeBuffer = newBuffer; + return newBuffer; + } + } +} + +#endif + diff --git a/Src/Newtonsoft.Json/JsonTextWriter.cs b/Src/Newtonsoft.Json/JsonTextWriter.cs index 21d78393..22c52995 100644 --- a/Src/Newtonsoft.Json/JsonTextWriter.cs +++ b/Src/Newtonsoft.Json/JsonTextWriter.cs @@ -39,8 +39,9 @@ namespace Newtonsoft.Json /// /// Represents a writer that provides a fast, non-cached, forward-only way of generating JSON data. /// - public class JsonTextWriter : JsonWriter + public partial class JsonTextWriter : JsonWriter { + private const int IndentCharBufferSize = 12; private readonly TextWriter _writer; private Base64Encoder _base64Encoder; private char _indentChar; @@ -160,6 +161,10 @@ namespace Newtonsoft.Json _indentation = 2; UpdateCharEscapeFlags(); + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + _safeAsync = GetType() == typeof(JsonTextWriter); +#endif } /// @@ -178,6 +183,11 @@ namespace Newtonsoft.Json { base.Close(); + CloseBufferAndWriter(); + } + + private void CloseBufferAndWriter() + { if (_writeBuffer != null) { BufferUtils.ReturnBuffer(_arrayPool, _writeBuffer); @@ -308,27 +318,45 @@ namespace Newtonsoft.Json /// protected override void WriteIndent() { - _writer.WriteLine(); - // levels of indentation multiplied by the indent count int currentIndentCount = Top * _indentation; - if (currentIndentCount > 0) + int newLineLen = SetIndentChars(); + + _writer.Write(_indentChars, 0, newLineLen + Math.Min(currentIndentCount, IndentCharBufferSize)); + + while ((currentIndentCount -= IndentCharBufferSize) > 0) { - if (_indentChars == null) + _writer.Write(_indentChars, newLineLen, Math.Min(currentIndentCount, IndentCharBufferSize)); + } + } + + private int SetIndentChars() + { + // Set _indentChars to be a newline followed by IndentCharBufferSize indent characters. + string writerNewLine = _writer.NewLine; + int newLineLen = writerNewLine.Length; + bool match = _indentChars != null && _indentChars.Length == IndentCharBufferSize + newLineLen; + if (match) + { + for (int i = 0; i != newLineLen; ++i) { - _indentChars = new string(_indentChar, 10).ToCharArray(); - } - - while (currentIndentCount > 0) - { - int writeCount = Math.Min(currentIndentCount, 10); - - _writer.Write(_indentChars, 0, writeCount); - - currentIndentCount -= writeCount; + if (writerNewLine[i] != _indentChars[i]) + { + match = false; + break; + } } } + + if (!match) + { + // If we're here, either _indentChars hasn't been set yet, or _writer.NewLine + // has been changed, or _indentChar has been changed. + _indentChars = (writerNewLine + new string(_indentChar, IndentCharBufferSize)).ToCharArray(); + } + + return newLineLen; } /// @@ -465,7 +493,7 @@ namespace Newtonsoft.Json public override void WriteValue(ulong value) { InternalWriteValue(JsonToken.Integer); - WriteIntegerValue(value); + WriteIntegerValue(value, false); } /// @@ -605,14 +633,9 @@ namespace Newtonsoft.Json if (string.IsNullOrEmpty(DateFormatString)) { - EnsureWriteBuffer(); + int length = WriteValueToBuffer(value); - int pos = 0; - _writeBuffer[pos++] = _quoteChar; - pos = DateTimeUtils.WriteDateTimeString(_writeBuffer, pos, value, null, value.Kind, DateFormatHandling); - _writeBuffer[pos++] = _quoteChar; - - _writer.Write(_writeBuffer, 0, pos); + _writer.Write(_writeBuffer, 0, length); } else { @@ -622,6 +645,17 @@ namespace Newtonsoft.Json } } + private int WriteValueToBuffer(DateTime value) + { + EnsureWriteBuffer(); + + int pos = 0; + _writeBuffer[pos++] = _quoteChar; + pos = DateTimeUtils.WriteDateTimeString(_writeBuffer, pos, value, null, value.Kind, DateFormatHandling); + _writeBuffer[pos++] = _quoteChar; + return pos; + } + /// /// Writes a [] value. /// @@ -653,14 +687,9 @@ namespace Newtonsoft.Json if (string.IsNullOrEmpty(DateFormatString)) { - EnsureWriteBuffer(); + int length = WriteValueToBuffer(value); - int pos = 0; - _writeBuffer[pos++] = _quoteChar; - pos = DateTimeUtils.WriteDateTimeString(_writeBuffer, pos, (DateFormatHandling == DateFormatHandling.IsoDateFormat) ? value.DateTime : value.UtcDateTime, value.Offset, DateTimeKind.Local, DateFormatHandling); - _writeBuffer[pos++] = _quoteChar; - - _writer.Write(_writeBuffer, 0, pos); + _writer.Write(_writeBuffer, 0, length); } else { @@ -669,6 +698,17 @@ namespace Newtonsoft.Json _writer.Write(_quoteChar); } } + + private int WriteValueToBuffer(DateTimeOffset value) + { + EnsureWriteBuffer(); + + int pos = 0; + _writeBuffer[pos++] = _quoteChar; + pos = DateTimeUtils.WriteDateTimeString(_writeBuffer, pos, (DateFormatHandling == DateFormatHandling.IsoDateFormat) ? value.DateTime : value.UtcDateTime, value.Offset, DateTimeKind.Local, DateFormatHandling); + _writeBuffer[pos++] = _quoteChar; + return pos; + } #endif /// @@ -771,38 +811,45 @@ namespace Newtonsoft.Json } else { - ulong uvalue = (value < 0) ? (ulong)-value : (ulong)value; - - if (value < 0) - { - _writer.Write('-'); - } - - WriteIntegerValue(uvalue); + bool negative = value < 0; + WriteIntegerValue(negative ? (ulong)-value : (ulong)value, negative); } } - private void WriteIntegerValue(ulong uvalue) + private void WriteIntegerValue(ulong uvalue, bool negative) { - if (uvalue <= 9) + if (!negative & uvalue <= 9) { _writer.Write((char)('0' + uvalue)); } else { - EnsureWriteBuffer(); - - int totalLength = MathUtils.IntLength(uvalue); - int length = 0; - - do - { - _writeBuffer[totalLength - ++length] = (char)('0' + (uvalue % 10)); - uvalue /= 10; - } while (uvalue != 0); - + int length = WriteNumberToBuffer(uvalue, negative); _writer.Write(_writeBuffer, 0, length); } } + + private int WriteNumberToBuffer(ulong value, bool negative) + { + EnsureWriteBuffer(); + + int totalLength = MathUtils.IntLength(value); + + if (negative) + { + totalLength++; + _writeBuffer[0] = '-'; + } + + int index = totalLength; + + do + { + _writeBuffer[--index] = (char)('0' + value % 10); + value /= 10; + } while (value != 0); + + return totalLength; + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonWriter.Async.cs b/Src/Newtonsoft.Json/JsonWriter.Async.cs new file mode 100644 index 00000000..61a9f17f --- /dev/null +++ b/Src/Newtonsoft.Json/JsonWriter.Async.cs @@ -0,0 +1,1676 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System; +using System.Diagnostics; +using System.Globalization; +using System.Threading; +#if !PORTABLE || NETSTANDARD1_1 +using System.Numerics; +#endif +using System.Threading.Tasks; +using Newtonsoft.Json.Utilities; + +namespace Newtonsoft.Json +{ + public abstract partial class JsonWriter + { + internal Task AutoCompleteAsync(JsonToken tokenBeingWritten, CancellationToken cancellationToken) + { + State oldState = _currentState; + + // gets new state based on the current state and what is being written + State newState = StateArray[(int)tokenBeingWritten][(int)oldState]; + + if (newState == State.Error) + { + throw JsonWriterException.Create(this, "Token {0} in state {1} would result in an invalid JSON object.".FormatWith(CultureInfo.InvariantCulture, tokenBeingWritten.ToString(), oldState.ToString()), null); + } + + _currentState = newState; + + if (_formatting == Formatting.Indented) + { + switch (oldState) + { + case State.Start: + break; + case State.Property: + return WriteIndentSpaceAsync(cancellationToken); + case State.ArrayStart: + case State.ConstructorStart: + return WriteIndentAsync(cancellationToken); + case State.Array: + case State.Constructor: + return tokenBeingWritten == JsonToken.Comment ? WriteIndentAsync(cancellationToken) : AutoCompleteAsync(cancellationToken); + case State.Object: + switch (tokenBeingWritten) + { + case JsonToken.Comment: + break; + case JsonToken.PropertyName: + return AutoCompleteAsync(cancellationToken); + default: + return WriteValueDelimiterAsync(cancellationToken); + } + + break; + default: + if (tokenBeingWritten == JsonToken.PropertyName) + { + return WriteIndentAsync(cancellationToken); + } + + break; + } + } + else if (tokenBeingWritten != JsonToken.Comment) + { + switch (oldState) + { + case State.Object: + case State.Array: + case State.Constructor: + return WriteValueDelimiterAsync(cancellationToken); + } + } + + return AsyncUtils.CompletedTask; + } + + private async Task AutoCompleteAsync(CancellationToken cancellationToken) + { + await WriteValueDelimiterAsync(cancellationToken).ConfigureAwait(false); + await WriteIndentAsync(cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously closes this writer. + /// If is set to true, the destination is also closed. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task CloseAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + Close(); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously flushes whatever is in the buffer to the destination and also flushes the destination. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + Flush(); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes the specified end token. + /// + /// The end token to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + protected virtual Task WriteEndAsync(JsonToken token, CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteEnd(token); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes indent characters. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + protected virtual Task WriteIndentAsync(CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteIndent(); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes the JSON value delimiter. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + protected virtual Task WriteValueDelimiterAsync(CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValueDelimiter(); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes an indent space. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + protected virtual Task WriteIndentSpaceAsync(CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteIndentSpace(); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes raw JSON without changing the writer's state. + /// + /// The raw JSON to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteRawAsync(string json, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteRaw(json); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes the end of the current JSON object or array. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteEndAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteEnd(); + return AsyncUtils.CompletedTask; + } + + internal Task WriteEndInternalAsync(CancellationToken cancellationToken) + { + JsonContainerType type = Peek(); + switch (type) + { + case JsonContainerType.Object: + return WriteEndObjectAsync(cancellationToken); + case JsonContainerType.Array: + return WriteEndArrayAsync(cancellationToken); + case JsonContainerType.Constructor: + return WriteEndConstructorAsync(cancellationToken); + default: + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + throw JsonWriterException.Create(this, "Unexpected type when writing end: " + type, null); + } + } + + internal async Task InternalWriteEndAsync(JsonContainerType type, CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + int levelsToComplete = CalculateLevelsToComplete(type); + + while (levelsToComplete-- > 0) + { + JsonToken token = GetCloseTokenForType(Pop()); + + if (_currentState == State.Property) + { + await WriteNullAsync(cancellationToken).ConfigureAwait(false); + } + + if (_formatting == Formatting.Indented) + { + if (_currentState != State.ObjectStart && _currentState != State.ArrayStart) + { + await WriteIndentAsync(cancellationToken).ConfigureAwait(false); + } + } + + await WriteEndAsync(token, cancellationToken).ConfigureAwait(false); + + UpdateCurrentState(); + } + } + + /// + /// Asynchronously writes the end of an array. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteEndArrayAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteEndArray(); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes the end of a constructor. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteEndConstructorAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteEndConstructor(); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes the end of a JSON object. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteEndObjectAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteEndObject(); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a null value. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteNullAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteNull(); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes the property name of a name/value pair of a JSON object. + /// + /// The name of the property. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WritePropertyNameAsync(string name, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WritePropertyName(name); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes the property name of a name/value pair of a JSON object. + /// + /// The name of the property. + /// A flag to indicate whether the text should be escaped when it is written as a JSON property name. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WritePropertyNameAsync(string name, bool escape, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WritePropertyName(name, escape); + return AsyncUtils.CompletedTask; + } + + internal Task InternalWritePropertyNameAsync(string name, CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + _currentPosition.PropertyName = name; + return AutoCompleteAsync(JsonToken.PropertyName, cancellationToken); + } + + /// + /// Asynchronously writes the beginning of a JSON array. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteStartArrayAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteStartArray(); + return AsyncUtils.CompletedTask; + } + + internal async Task InternalWriteStartAsync(JsonToken token, JsonContainerType container, CancellationToken cancellationToken) + { + UpdateScopeWithFinishedValue(); + await AutoCompleteAsync(token, cancellationToken).ConfigureAwait(false); + Push(container); + } + + /// + /// Asynchronously writes a comment /*...*/ containing the specified text. + /// + /// Text to place inside the comment. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteCommentAsync(string text, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteComment(text); + return AsyncUtils.CompletedTask; + } + + internal Task InternalWriteCommentAsync(CancellationToken cancellationToken) + { + return AutoCompleteAsync(JsonToken.Comment, cancellationToken); + } + + /// + /// Asynchronously writes raw JSON where a value is expected and updates the writer's state. + /// + /// The raw JSON to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteRawValueAsync(string json, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteRawValue(json); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes the start of a constructor with the given name. + /// + /// The name of the constructor. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteStartConstructorAsync(string name, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteStartConstructor(name); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes the beginning of a JSON object. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteStartObjectAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteStartObject(); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes the current token. + /// + /// The to read the token from. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public Task WriteTokenAsync(JsonReader reader, CancellationToken cancellationToken = default(CancellationToken)) + { + return WriteTokenAsync(reader, true, cancellationToken); + } + + /// + /// Asynchronously writes the current token. + /// + /// The to read the token from. + /// A flag indicating whether the current token's children should be written. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public Task WriteTokenAsync(JsonReader reader, bool writeChildren, CancellationToken cancellationToken = default(CancellationToken)) + { + ValidationUtils.ArgumentNotNull(reader, nameof(reader)); + + return WriteTokenAsync(reader, writeChildren, true, true, cancellationToken); + } + + /// + /// Asynchronously writes the token and its value. + /// + /// The to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public Task WriteTokenAsync(JsonToken token, CancellationToken cancellationToken = default(CancellationToken)) + { + return WriteTokenAsync(token, null, cancellationToken); + } + + /// + /// Asynchronously writes the token and its value. + /// + /// The to write. + /// + /// The value to write. + /// A value is only required for tokens that have an associated value, e.g. the property name for . + /// null can be passed to the method for tokens that don't have a value, e.g. . + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public Task WriteTokenAsync(JsonToken token, object value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + switch (token) + { + case JsonToken.None: + // read to next + return AsyncUtils.CompletedTask; + case JsonToken.StartObject: + return WriteStartObjectAsync(cancellationToken); + case JsonToken.StartArray: + return WriteStartArrayAsync(cancellationToken); + case JsonToken.StartConstructor: + ValidationUtils.ArgumentNotNull(value, nameof(value)); + return WriteStartConstructorAsync(value.ToString(), cancellationToken); + case JsonToken.PropertyName: + ValidationUtils.ArgumentNotNull(value, nameof(value)); + return WritePropertyNameAsync(value.ToString(), cancellationToken); + case JsonToken.Comment: + return WriteCommentAsync(value?.ToString(), cancellationToken); + case JsonToken.Integer: + ValidationUtils.ArgumentNotNull(value, nameof(value)); + return +#if !PORTABLE || NETSTANDARD1_1 + value is BigInteger ? WriteValueAsync((BigInteger)value, cancellationToken) : +#endif + WriteValueAsync(Convert.ToInt64(value, CultureInfo.InvariantCulture), cancellationToken); + case JsonToken.Float: + ValidationUtils.ArgumentNotNull(value, nameof(value)); + if (value is decimal) + { + return WriteValueAsync((decimal)value, cancellationToken); + } + + if (value is double) + { + return WriteValueAsync((double)value, cancellationToken); + } + + if (value is float) + { + return WriteValueAsync((float)value, cancellationToken); + } + + return WriteValueAsync(Convert.ToDouble(value, CultureInfo.InvariantCulture), cancellationToken); + case JsonToken.String: + ValidationUtils.ArgumentNotNull(value, nameof(value)); + return WriteValueAsync(value.ToString(), cancellationToken); + case JsonToken.Boolean: + ValidationUtils.ArgumentNotNull(value, nameof(value)); + return WriteValueAsync(Convert.ToBoolean(value, CultureInfo.InvariantCulture), cancellationToken); + case JsonToken.Null: + return WriteNullAsync(cancellationToken); + case JsonToken.Undefined: + return WriteUndefinedAsync(cancellationToken); + case JsonToken.EndObject: + return WriteEndObjectAsync(cancellationToken); + case JsonToken.EndArray: + return WriteEndArrayAsync(cancellationToken); + case JsonToken.EndConstructor: + return WriteEndConstructorAsync(cancellationToken); + case JsonToken.Date: + ValidationUtils.ArgumentNotNull(value, nameof(value)); + if (value is DateTimeOffset) + { + return WriteValueAsync((DateTimeOffset)value, cancellationToken); + } + + return WriteValueAsync(Convert.ToDateTime(value, CultureInfo.InvariantCulture), cancellationToken); + case JsonToken.Raw: + return WriteRawValueAsync(value?.ToString(), cancellationToken); + case JsonToken.Bytes: + ValidationUtils.ArgumentNotNull(value, nameof(value)); + if (value is Guid) + { + return WriteValueAsync((Guid)value, cancellationToken); + } + + return WriteValueAsync((byte[])value, cancellationToken); + default: + throw MiscellaneousUtils.CreateArgumentOutOfRangeException(nameof(token), token, "Unexpected token type."); + } + } + + internal virtual async Task WriteTokenAsync(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments, CancellationToken cancellationToken) + { + int initialDepth = CalculateWriteTokenDepth(reader); + + do + { + // write a JValue date when the constructor is for a date + if (writeDateConstructorAsDate && reader.TokenType == JsonToken.StartConstructor && string.Equals(reader.Value.ToString(), "Date", StringComparison.Ordinal)) + { + await WriteConstructorDateAsync(reader, cancellationToken).ConfigureAwait(false); + } + else + { + if (writeComments || reader.TokenType != JsonToken.Comment) + { + await WriteTokenAsync(reader.TokenType, reader.Value, cancellationToken).ConfigureAwait(false); + } + } + } while ( + // stop if we have reached the end of the token being read + initialDepth - 1 < reader.Depth - (JsonTokenUtils.IsEndToken(reader.TokenType) ? 1 : 0) + && writeChildren + && await reader.ReadAsync(cancellationToken).ConfigureAwait(false)); + } + + private async Task WriteConstructorDateAsync(JsonReader reader, CancellationToken cancellationToken) + { + if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null); + } + if (reader.TokenType != JsonToken.Integer) + { + throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected Integer, got " + reader.TokenType, null); + } + + DateTime date = DateTimeUtils.ConvertJavaScriptTicksToDateTime((long)reader.Value); + + if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) + { + throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null); + } + if (reader.TokenType != JsonToken.EndConstructor) + { + throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected EndConstructor, got " + reader.TokenType, null); + } + + await WriteValueAsync(date, cancellationToken).ConfigureAwait(false); + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(bool value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(bool? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(byte value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(byte? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a [] value. + /// + /// The [] value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(byte[] value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(char value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(char? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(DateTime value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(DateTime? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(DateTimeOffset value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(DateTimeOffset? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(decimal value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(decimal? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(double value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(double? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(float value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(float? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(Guid value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(Guid? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(int value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(int? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(long value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(long? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(object value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + [CLSCompliant(false)] + public virtual Task WriteValueAsync(sbyte value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + [CLSCompliant(false)] + public virtual Task WriteValueAsync(sbyte? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(short value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(short? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(string value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(TimeSpan value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(TimeSpan? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + [CLSCompliant(false)] + public virtual Task WriteValueAsync(uint value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + [CLSCompliant(false)] + public virtual Task WriteValueAsync(uint? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + [CLSCompliant(false)] + public virtual Task WriteValueAsync(ulong value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + [CLSCompliant(false)] + public virtual Task WriteValueAsync(ulong? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteValueAsync(Uri value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a value. + /// + /// The value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + [CLSCompliant(false)] + public virtual Task WriteValueAsync(ushort value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes a of value. + /// + /// The of value to write. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + [CLSCompliant(false)] + public virtual Task WriteValueAsync(ushort? value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteValue(value); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes an undefined value. + /// + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteUndefinedAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteUndefined(); + return AsyncUtils.CompletedTask; + } + + /// + /// Asynchronously writes the given white space. + /// + /// The string of white space characters. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + public virtual Task WriteWhitespaceAsync(string ws, CancellationToken cancellationToken = default(CancellationToken)) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + WriteWhitespace(ws); + return AsyncUtils.CompletedTask; + } + + internal Task InternalWriteValueAsync(JsonToken token, CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + UpdateScopeWithFinishedValue(); + return AutoCompleteAsync(token, cancellationToken); + } + + /// + /// Asynchronously ets the state of the . + /// + /// The being written. + /// The value being written. + /// The token to monitor for cancellation requests. The default value is . + /// A that represents the asynchronous operation. + /// The default behaviour is to execute synchronously, returning an already-completed task. Derived + /// classes can override this behaviour for true asychronousity. + protected Task SetWriteStateAsync(JsonToken token, object value, CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + switch (token) + { + case JsonToken.StartObject: + return InternalWriteStartAsync(token, JsonContainerType.Object, cancellationToken); + case JsonToken.StartArray: + return InternalWriteStartAsync(token, JsonContainerType.Array, cancellationToken); + case JsonToken.StartConstructor: + return InternalWriteStartAsync(token, JsonContainerType.Constructor, cancellationToken); + case JsonToken.PropertyName: + if (!(value is string)) + { + throw new ArgumentException("A name is required when setting property name state.", nameof(value)); + } + + return InternalWritePropertyNameAsync((string)value, cancellationToken); + case JsonToken.Comment: + return InternalWriteCommentAsync(cancellationToken); + case JsonToken.Raw: + return AsyncUtils.CompletedTask; + case JsonToken.Integer: + case JsonToken.Float: + case JsonToken.String: + case JsonToken.Boolean: + case JsonToken.Date: + case JsonToken.Bytes: + case JsonToken.Null: + case JsonToken.Undefined: + return InternalWriteValueAsync(token, cancellationToken); + case JsonToken.EndObject: + return InternalWriteEndAsync(JsonContainerType.Object, cancellationToken); + case JsonToken.EndArray: + return InternalWriteEndAsync(JsonContainerType.Array, cancellationToken); + case JsonToken.EndConstructor: + return InternalWriteEndAsync(JsonContainerType.Constructor, cancellationToken); + default: + throw new ArgumentOutOfRangeException(nameof(token)); + } + } + + internal static Task WriteValueAsync(JsonWriter writer, PrimitiveTypeCode typeCode, object value, CancellationToken cancellationToken) + { + switch (typeCode) + { + case PrimitiveTypeCode.Char: + return writer.WriteValueAsync((char)value, cancellationToken); + case PrimitiveTypeCode.CharNullable: + return writer.WriteValueAsync(value == null ? (char?)null : (char)value, cancellationToken); + case PrimitiveTypeCode.Boolean: + return writer.WriteValueAsync((bool)value, cancellationToken); + case PrimitiveTypeCode.BooleanNullable: + return writer.WriteValueAsync(value == null ? (bool?)null : (bool)value, cancellationToken); + case PrimitiveTypeCode.SByte: + return writer.WriteValueAsync((sbyte)value, cancellationToken); + case PrimitiveTypeCode.SByteNullable: + return writer.WriteValueAsync(value == null ? (sbyte?)null : (sbyte)value, cancellationToken); + case PrimitiveTypeCode.Int16: + return writer.WriteValueAsync((short)value, cancellationToken); + case PrimitiveTypeCode.Int16Nullable: + return writer.WriteValueAsync(value == null ? (short?)null : (short)value, cancellationToken); + case PrimitiveTypeCode.UInt16: + return writer.WriteValueAsync((ushort)value, cancellationToken); + case PrimitiveTypeCode.UInt16Nullable: + return writer.WriteValueAsync(value == null ? (ushort?)null : (ushort)value, cancellationToken); + case PrimitiveTypeCode.Int32: + return writer.WriteValueAsync((int)value, cancellationToken); + case PrimitiveTypeCode.Int32Nullable: + return writer.WriteValueAsync(value == null ? (int?)null : (int)value, cancellationToken); + case PrimitiveTypeCode.Byte: + return writer.WriteValueAsync((byte)value, cancellationToken); + case PrimitiveTypeCode.ByteNullable: + return writer.WriteValueAsync(value == null ? (byte?)null : (byte)value, cancellationToken); + case PrimitiveTypeCode.UInt32: + return writer.WriteValueAsync((uint)value, cancellationToken); + case PrimitiveTypeCode.UInt32Nullable: + return writer.WriteValueAsync(value == null ? (uint?)null : (uint)value, cancellationToken); + case PrimitiveTypeCode.Int64: + return writer.WriteValueAsync((long)value, cancellationToken); + case PrimitiveTypeCode.Int64Nullable: + return writer.WriteValueAsync(value == null ? (long?)null : (long)value, cancellationToken); + case PrimitiveTypeCode.UInt64: + return writer.WriteValueAsync((ulong)value, cancellationToken); + case PrimitiveTypeCode.UInt64Nullable: + return writer.WriteValueAsync(value == null ? (ulong?)null : (ulong)value, cancellationToken); + case PrimitiveTypeCode.Single: + return writer.WriteValueAsync((float)value, cancellationToken); + case PrimitiveTypeCode.SingleNullable: + return writer.WriteValueAsync(value == null ? (float?)null : (float)value, cancellationToken); + case PrimitiveTypeCode.Double: + return writer.WriteValueAsync((double)value, cancellationToken); + case PrimitiveTypeCode.DoubleNullable: + return writer.WriteValueAsync(value == null ? (double?)null : (double)value, cancellationToken); + case PrimitiveTypeCode.DateTime: + return writer.WriteValueAsync((DateTime)value, cancellationToken); + case PrimitiveTypeCode.DateTimeNullable: + return writer.WriteValueAsync(value == null ? (DateTime?)null : (DateTime)value, cancellationToken); + case PrimitiveTypeCode.DateTimeOffset: + return writer.WriteValueAsync((DateTimeOffset)value, cancellationToken); + case PrimitiveTypeCode.DateTimeOffsetNullable: + return writer.WriteValueAsync(value == null ? (DateTimeOffset?)null : (DateTimeOffset)value, cancellationToken); + case PrimitiveTypeCode.Decimal: + return writer.WriteValueAsync((decimal)value, cancellationToken); + case PrimitiveTypeCode.DecimalNullable: + return writer.WriteValueAsync(value == null ? (decimal?)null : (decimal)value, cancellationToken); + case PrimitiveTypeCode.Guid: + return writer.WriteValueAsync((Guid)value, cancellationToken); + case PrimitiveTypeCode.GuidNullable: + return writer.WriteValueAsync(value == null ? (Guid?)null : (Guid)value, cancellationToken); + case PrimitiveTypeCode.TimeSpan: + return writer.WriteValueAsync((TimeSpan)value, cancellationToken); + case PrimitiveTypeCode.TimeSpanNullable: + return writer.WriteValueAsync(value == null ? (TimeSpan?)null : (TimeSpan)value, cancellationToken); +#if !PORTABLE || NETSTANDARD1_1 + case PrimitiveTypeCode.BigInteger: + + // this will call to WriteValueAsync(object) + return writer.WriteValueAsync((BigInteger)value, cancellationToken); + case PrimitiveTypeCode.BigIntegerNullable: + + // this will call to WriteValueAsync(object) + return writer.WriteValueAsync(value == null ? (BigInteger?)null : (BigInteger)value, cancellationToken); +#endif + case PrimitiveTypeCode.Uri: + return writer.WriteValueAsync((Uri)value, cancellationToken); + case PrimitiveTypeCode.String: + return writer.WriteValueAsync((string)value, cancellationToken); + case PrimitiveTypeCode.Bytes: + return writer.WriteValueAsync((byte[])value, cancellationToken); +#if !(PORTABLE || DOTNET) + case PrimitiveTypeCode.DBNull: + return writer.WriteNullAsync(cancellationToken); +#endif + default: +#if !PORTABLE + IConvertible convertable = value as IConvertible; + if (convertable != null) + { + // the value is a non-standard IConvertible + // convert to the underlying value and retry + TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertable); + + // if convertible has an underlying typecode of Object then attempt to convert it to a string + PrimitiveTypeCode resolvedTypeCode = typeInformation.TypeCode == PrimitiveTypeCode.Object ? PrimitiveTypeCode.String : typeInformation.TypeCode; + Type resolvedType = typeInformation.TypeCode == PrimitiveTypeCode.Object ? typeof(string) : typeInformation.Type; + + object convertedValue = convertable.ToType(resolvedType, CultureInfo.InvariantCulture); + + return WriteValueAsync(writer, resolvedTypeCode, convertedValue, cancellationToken); + } +#endif + throw CreateUnsupportedTypeException(writer, value); + } + } + } +} + +#endif diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 53c86652..7e985472 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -43,7 +43,7 @@ namespace Newtonsoft.Json /// /// Represents a writer that provides a fast, non-cached, forward-only way of generating JSON data. /// - public abstract class JsonWriter : IDisposable + public abstract partial class JsonWriter : IDisposable { internal enum State { @@ -628,20 +628,7 @@ namespace Newtonsoft.Json internal virtual void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments) { - int initialDepth; - - if (reader.TokenType == JsonToken.None) - { - initialDepth = -1; - } - else if (!JsonTokenUtils.IsStartToken(reader.TokenType)) - { - initialDepth = reader.Depth + 1; - } - else - { - initialDepth = reader.Depth; - } + int initialDepth = CalculateWriteTokenDepth(reader); do { @@ -664,6 +651,17 @@ namespace Newtonsoft.Json && reader.Read()); } + private int CalculateWriteTokenDepth(JsonReader reader) + { + JsonToken type = reader.TokenType; + if (type == JsonToken.None) + { + return -1; + } + + return JsonTokenUtils.IsStartToken(type) ? reader.Depth : reader.Depth + 1; + } + private void WriteConstructorDate(JsonReader reader) { if (!reader.Read()) @@ -733,7 +731,33 @@ namespace Newtonsoft.Json private void AutoCompleteClose(JsonContainerType type) { - // write closing symbol and calculate new state + int levelsToComplete = CalculateLevelsToComplete(type); + + for (int i = 0; i < levelsToComplete; i++) + { + JsonToken token = GetCloseTokenForType(Pop()); + + if (_currentState == State.Property) + { + WriteNull(); + } + + if (_formatting == Formatting.Indented) + { + if (_currentState != State.ObjectStart && _currentState != State.ArrayStart) + { + WriteIndent(); + } + } + + WriteEnd(token); + + UpdateCurrentState(); + } + } + + private int CalculateLevelsToComplete(JsonContainerType type) + { int levelsToComplete = 0; if (_currentPosition.Type == type) @@ -760,44 +784,29 @@ namespace Newtonsoft.Json throw JsonWriterException.Create(this, "No token to close.", null); } - for (int i = 0; i < levelsToComplete; i++) + return levelsToComplete; + } + + private void UpdateCurrentState() + { + JsonContainerType currentLevelType = Peek(); + + switch (currentLevelType) { - JsonToken token = GetCloseTokenForType(Pop()); - - if (_currentState == State.Property) - { - WriteNull(); - } - - if (_formatting == Formatting.Indented) - { - if (_currentState != State.ObjectStart && _currentState != State.ArrayStart) - { - WriteIndent(); - } - } - - WriteEnd(token); - - JsonContainerType currentLevelType = Peek(); - - switch (currentLevelType) - { - case JsonContainerType.Object: - _currentState = State.Object; - break; - case JsonContainerType.Array: - _currentState = State.Array; - break; - case JsonContainerType.Constructor: - _currentState = State.Array; - break; - case JsonContainerType.None: - _currentState = State.Start; - break; - default: - throw JsonWriterException.Create(this, "Unknown JsonType: " + currentLevelType, null); - } + case JsonContainerType.Object: + _currentState = State.Object; + break; + case JsonContainerType.Array: + _currentState = State.Array; + break; + case JsonContainerType.Constructor: + _currentState = State.Array; + break; + case JsonContainerType.None: + _currentState = State.Start; + break; + default: + throw JsonWriterException.Create(this, "Unknown JsonType: " + currentLevelType, null); } } diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj index 6bd41106..986e37b9 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj @@ -93,13 +93,17 @@ + + + + @@ -215,6 +219,7 @@ + @@ -261,4 +266,4 @@ --> - \ No newline at end of file + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj index e9e37c4e..e814725a 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj @@ -96,16 +96,20 @@ + + + + @@ -218,6 +222,7 @@ + diff --git a/Src/Newtonsoft.Json/Utilities/AsyncUtils.cs b/Src/Newtonsoft.Json/Utilities/AsyncUtils.cs new file mode 100644 index 00000000..6209e79a --- /dev/null +++ b/Src/Newtonsoft.Json/Utilities/AsyncUtils.cs @@ -0,0 +1,104 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + +using System.Diagnostics; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace Newtonsoft.Json.Utilities +{ + internal static class AsyncUtils + { + // Pre-allocate to avoid wasted allocations. + public static readonly Task False = Task.FromResult(false); + public static readonly Task True = Task.FromResult(true); + + internal static Task ToAsync(this bool value) => value ? True : False; + + public static Task CancelIfRequestedAsync(this CancellationToken cancellationToken) + { + return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : null; + } + + public static Task CancelIfRequestedAsync(this CancellationToken cancellationToken) + { + return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : null; + } + + // From 4.6 on we could use Task.FromCanceled(), but we need an equivalent for + // previous frameworks. + public static Task FromCanceled(this CancellationToken cancellationToken) + { + Debug.Assert(cancellationToken.IsCancellationRequested); + return new Task(() => {}, cancellationToken); + } + + public static Task FromCanceled(this CancellationToken cancellationToken) + { + Debug.Assert(cancellationToken.IsCancellationRequested); + return new Task(() => default(T), cancellationToken); + } + + // Task.Delay(0) is optimised as a cached task within the framework, and indeed + // the same cached task that Task.CompletedTask returns as of 4.6, but we'll add + // our own cached field for previous frameworks. + internal static readonly Task CompletedTask = Task.Delay(0); + + public static Task WriteAsync(this TextWriter writer, char value, CancellationToken cancellationToken) + { + Debug.Assert(writer != null); + return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : writer.WriteAsync(value); + } + + public static Task WriteAsync(this TextWriter writer, string value, CancellationToken cancellationToken) + { + Debug.Assert(writer != null); + return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : writer.WriteAsync(value); + } + + public static Task WriteAsync(this TextWriter writer, char[] value, CancellationToken cancellationToken) + { + Debug.Assert(writer != null); + return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : writer.WriteAsync(value); + } + + public static Task WriteAsync(this TextWriter writer, char[] value, int start, int count, CancellationToken cancellationToken) + { + Debug.Assert(writer != null); + return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : writer.WriteAsync(value, start, count); + } + + public static Task ReadAsync(this TextReader reader, char[] buffer, int index, int count, CancellationToken cancellationToken) + { + Debug.Assert(reader != null); + return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : reader.ReadAsync(buffer, index, count); + } + } +} + +#endif diff --git a/Src/Newtonsoft.Json/Utilities/Base64Encoder.cs b/Src/Newtonsoft.Json/Utilities/Base64Encoder.cs index 8f298d13..c49ee08b 100644 --- a/Src/Newtonsoft.Json/Utilities/Base64Encoder.cs +++ b/Src/Newtonsoft.Json/Utilities/Base64Encoder.cs @@ -25,6 +25,10 @@ using System; using System.IO; +#if !(NET20 || NET35 || NET40 || PORTABLE40) +using System.Threading; +using System.Threading.Tasks; +#endif namespace Newtonsoft.Json.Utilities { @@ -45,7 +49,7 @@ namespace Newtonsoft.Json.Utilities _writer = writer; } - public void Encode(byte[] buffer, int index, int count) + private void ValidateEncode(byte[] buffer, int index, int count) { if (buffer == null) { @@ -66,36 +70,25 @@ namespace Newtonsoft.Json.Utilities { throw new ArgumentOutOfRangeException(nameof(count)); } + } + + public void Encode(byte[] buffer, int index, int count) + { + ValidateEncode(buffer, index, count); if (_leftOverBytesCount > 0) { - int leftOverBytesCount = _leftOverBytesCount; - while (leftOverBytesCount < 3 && count > 0) + if(FulfillFromLeftover(buffer, index, ref count)) { - _leftOverBytes[leftOverBytesCount++] = buffer[index++]; - count--; - } - if (count == 0 && leftOverBytesCount < 3) - { - _leftOverBytesCount = leftOverBytesCount; return; } + int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0); WriteChars(_charsLine, 0, num2); } - _leftOverBytesCount = count % 3; - if (_leftOverBytesCount > 0) - { - count -= _leftOverBytesCount; - if (_leftOverBytes == null) - { - _leftOverBytes = new byte[3]; - } - for (int i = 0; i < _leftOverBytesCount; i++) - { - _leftOverBytes[i] = buffer[(index + count) + i]; - } - } + + StoreLeftOverBytes(buffer, index, ref count); + int num4 = index + count; int length = LineSizeInBytes; while (index < num4) @@ -110,6 +103,44 @@ namespace Newtonsoft.Json.Utilities } } + private void StoreLeftOverBytes(byte[] buffer, int index, ref int count) + { + int leftOverBytesCount = count % 3; + if (leftOverBytesCount > 0) + { + count -= leftOverBytesCount; + if (_leftOverBytes == null) + { + _leftOverBytes = new byte[3]; + } + + for (int i = 0; i < leftOverBytesCount; i++) + { + _leftOverBytes[i] = buffer[index + count + i]; + } + } + + _leftOverBytesCount = leftOverBytesCount; + } + + private bool FulfillFromLeftover(byte[] buffer, int index, ref int count) + { + int leftOverBytesCount = _leftOverBytesCount; + while (leftOverBytesCount < 3 && count > 0) + { + _leftOverBytes[leftOverBytesCount++] = buffer[index++]; + count--; + } + + if (count == 0 && leftOverBytesCount < 3) + { + _leftOverBytesCount = leftOverBytesCount; + return true; + } + + return false; + } + public void Flush() { if (_leftOverBytesCount > 0) @@ -124,5 +155,63 @@ namespace Newtonsoft.Json.Utilities { _writer.Write(chars, index, count); } + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + + public async Task EncodeAsync(byte[] buffer, int index, int count, CancellationToken cancellationToken) + { + ValidateEncode(buffer, index, count); + + if (_leftOverBytesCount > 0) + { + if (FulfillFromLeftover(buffer, index, ref count)) + { + return; + } + + int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0); + await WriteCharsAsync(_charsLine, 0, num2, cancellationToken).ConfigureAwait(false); + } + + StoreLeftOverBytes(buffer, index, ref count); + + int num4 = index + count; + int length = LineSizeInBytes; + while (index < num4) + { + if (index + length > num4) + { + length = num4 - index; + } + int num6 = Convert.ToBase64CharArray(buffer, index, length, _charsLine, 0); + await WriteCharsAsync(_charsLine, 0, num6, cancellationToken).ConfigureAwait(false); + index += length; + } + } + + private Task WriteCharsAsync(char[] chars, int index, int count, CancellationToken cancellationToken) + { + return _writer.WriteAsync(chars, index, count, cancellationToken); + } + + public Task FlushAsync(CancellationToken cancellationToken) + { + if (cancellationToken.IsCancellationRequested) + { + return cancellationToken.FromCanceled(); + } + + if (_leftOverBytesCount > 0) + { + int count = Convert.ToBase64CharArray(_leftOverBytes, 0, _leftOverBytesCount, _charsLine, 0); + _leftOverBytesCount = 0; + return WriteCharsAsync(_charsLine, 0, count, cancellationToken); + } + + return AsyncUtils.CompletedTask; + } + +#endif + } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs index 9d33387d..6e3ee247 100644 --- a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs @@ -24,17 +24,16 @@ #endregion using System; -using System.Collections; -using System.Globalization; using System.IO; -using System.Text; -using System.Text.RegularExpressions; +#if !(NET20 || NET35 || NET40 || PORTABLE40) +using System.Threading; +using System.Threading.Tasks; +#endif using System.Collections.Generic; #if NET20 using Newtonsoft.Json.Utilities.LinqBridge; #else using System.Linq; - #endif namespace Newtonsoft.Json.Utilities @@ -141,6 +140,38 @@ namespace Newtonsoft.Json.Utilities return false; } + private static int FirstCharToEscape(string s, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling) + { + for (int i = 0; i != s.Length; i++) + { + char c = s[i]; + + if (c < charEscapeFlags.Length) + { + if (charEscapeFlags[c]) + { + return i; + } + } + else if (stringEscapeHandling == StringEscapeHandling.EscapeNonAscii) + { + return i; + } + else + { + switch (c) + { + case '\u0085': + case '\u2028': + case '\u2029': + return i; + } + } + } + + return -1; + } + public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool bufferPool, ref char[] writeBuffer) { @@ -277,14 +308,7 @@ namespace Newtonsoft.Json.Utilities } else { - int length = s.Length - lastWritePosition; - - if (writeBuffer == null || writeBuffer.Length < length) - { - writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, length, writeBuffer); - } - - s.CopyTo(lastWritePosition, writeBuffer, 0, length); + int length = WriteEscapedStringThroughBuffer(s, lastWritePosition, bufferPool, ref writeBuffer); // write remaining text writer.Write(writeBuffer, 0, length); @@ -298,6 +322,20 @@ namespace Newtonsoft.Json.Utilities } } + private static int WriteEscapedStringThroughBuffer(string s, int lastWritePosition, IArrayPool bufferPool, ref char[] writeBuffer) + { + int length = s.Length - lastWritePosition; + + if (writeBuffer == null || writeBuffer.Length < length) + { + writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, length, writeBuffer); + } + + s.CopyTo(lastWritePosition, writeBuffer, 0, length); + + return length; + } + public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters, StringEscapeHandling stringEscapeHandling) { bool[] charEscapeFlags = GetCharEscapeFlags(stringEscapeHandling, delimiter); @@ -309,5 +347,182 @@ namespace Newtonsoft.Json.Utilities return w.ToString(); } } + +#if !(NET20 || NET35 || NET40 || PORTABLE40) + + public static Task WriteEscapedJavaScriptStringAsync(TextWriter writer, string s, char delimiter, bool appendDelimiters, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer, CancellationToken cancellationToken = default(CancellationToken)) + { + if (appendDelimiters) + { + return WriteEscapedJavaScriptStringWithDelimitersAsync(writer, s, delimiter, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken); + } + + if (string.IsNullOrEmpty(s)) + { + return cancellationToken.CancelIfRequestedAsync() ?? AsyncUtils.CompletedTask; + } + + return WriteEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken); + } + + private static async Task WriteEscapedJavaScriptStringWithDelimitersAsync(TextWriter writer, string s, char delimiter, + bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer, CancellationToken cancellationToken) + { + await writer.WriteAsync(delimiter).ConfigureAwait(false); + + if (!string.IsNullOrEmpty(s)) + { + await WriteEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken).ConfigureAwait(false); + } + + await writer.WriteAsync(delimiter).ConfigureAwait(false); + } + + private static Task WriteEscapedJavaScriptStringWithoutDelimitersAsync( + TextWriter writer, string s, bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, + JsonTextWriter client, char[] writeBuffer, CancellationToken cancellationToken) + { + int i = FirstCharToEscape(s, charEscapeFlags, stringEscapeHandling); + return i == -1 + ? writer.WriteAsync(s, cancellationToken) + : WriteDefinitelyEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, i, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken); + } + + private static async Task WriteDefinitelyEscapedJavaScriptStringWithoutDelimitersAsync( + TextWriter writer, string s, int lastWritePosition, bool[] charEscapeFlags, + StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer, + CancellationToken cancellationToken) + { + if (writeBuffer == null || writeBuffer.Length < lastWritePosition) + { + writeBuffer = client.EnsureWriteBuffer(lastWritePosition, UnicodeTextLength); + } + + if (lastWritePosition != 0) + { + s.CopyTo(0, writeBuffer, 0, lastWritePosition); + + // write unchanged chars at start of text. + await writer.WriteAsync(writeBuffer, 0, lastWritePosition, cancellationToken).ConfigureAwait(false); + } + + int length; + bool isEscapedUnicodeText = false; + string escapedValue = null; + + for (int i = lastWritePosition; i < s.Length; i++) + { + char c = s[i]; + + if (c < charEscapeFlags.Length && !charEscapeFlags[c]) + { + continue; + } + + switch (c) + { + case '\t': + escapedValue = @"\t"; + break; + case '\n': + escapedValue = @"\n"; + break; + case '\r': + escapedValue = @"\r"; + break; + case '\f': + escapedValue = @"\f"; + break; + case '\b': + escapedValue = @"\b"; + break; + case '\\': + escapedValue = @"\\"; + break; + case '\u0085': // Next Line + escapedValue = @"\u0085"; + break; + case '\u2028': // Line Separator + escapedValue = @"\u2028"; + break; + case '\u2029': // Paragraph Separator + escapedValue = @"\u2029"; + break; + default: + if (c < charEscapeFlags.Length || stringEscapeHandling == StringEscapeHandling.EscapeNonAscii) + { + if (c == '\'' && stringEscapeHandling != StringEscapeHandling.EscapeHtml) + { + escapedValue = @"\'"; + } + else if (c == '"' && stringEscapeHandling != StringEscapeHandling.EscapeHtml) + { + escapedValue = @"\"""; + } + else + { + if (writeBuffer.Length < UnicodeTextLength) + { + writeBuffer = client.EnsureWriteBuffer(UnicodeTextLength, 0); + } + + StringUtils.ToCharAsUnicode(c, writeBuffer); + + isEscapedUnicodeText = true; + } + } + else + { + continue; + } + break; + } + + if (i > lastWritePosition) + { + length = i - lastWritePosition + (isEscapedUnicodeText ? UnicodeTextLength : 0); + int start = isEscapedUnicodeText ? UnicodeTextLength : 0; + + if (writeBuffer.Length < length) + { + writeBuffer = client.EnsureWriteBuffer(length, UnicodeTextLength); + } + + s.CopyTo(lastWritePosition, writeBuffer, start, length - start); + + // write unchanged chars before writing escaped text + await writer.WriteAsync(writeBuffer, start, length - start, cancellationToken).ConfigureAwait(false); + } + + lastWritePosition = i + 1; + if (!isEscapedUnicodeText) + { + await writer.WriteAsync(escapedValue, cancellationToken).ConfigureAwait(false); + } + else + { + await writer.WriteAsync(writeBuffer, 0, UnicodeTextLength, cancellationToken).ConfigureAwait(false); + isEscapedUnicodeText = false; + } + } + + length = s.Length - lastWritePosition; + + if (length != 0) + { + if (writeBuffer.Length < length) + { + writeBuffer = client.EnsureWriteBuffer(length, 0); + } + + s.CopyTo(lastWritePosition, writeBuffer, 0, length); + + // write remaining text + await writer.WriteAsync(writeBuffer, 0, length, cancellationToken).ConfigureAwait(false); + } + } + +#endif + } } \ No newline at end of file