From c445141fc6de2fe39c178018414a7bc66c7bc843 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 11 Jan 2018 16:18:04 +1300 Subject: [PATCH] -Fixed JsonTextReader sync read in async method --- Src/Newtonsoft.Json.Tests/Issues/Issue1569.cs | 125 ++++++++++++++++++ Src/Newtonsoft.Json/JsonTextReader.Async.cs | 2 +- 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 Src/Newtonsoft.Json.Tests/Issues/Issue1569.cs diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue1569.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue1569.cs new file mode 100644 index 00000000..d2552b83 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue1569.cs @@ -0,0 +1,125 @@ +#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 || PORTABLE || PORTABLE40) || NETSTANDARD1_3 || NETSTANDARD2_0 +using System; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +#if DNXCORE50 +using System.Reflection; +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif + +namespace Newtonsoft.Json.Tests.Issues +{ + [TestFixture] + public class Issue1569 : TestFixtureBase + { + [Test] + public async Task Test() + { + string json = "[1,2,3,456789999999999999999999999999999999999999999999999999999999999999456789999999999999999999999999999999999999999999999999999999999999456789999999999999999999999999999999999999999999999999999999999999]"; + + Stream s = new AsyncOnlyStream(new MemoryStream(Encoding.UTF8.GetBytes(json))); + StreamReader sr = new StreamReader(s, Encoding.UTF8, true, 2); + JsonTextReader reader = new JsonTextReader(sr); +#if DEBUG + reader.CharBuffer = new char[2]; +#endif + + while (await reader.ReadAsync()) + { + } + } + + public class AsyncOnlyStream : Stream + { + private readonly Stream _innerStream; + + public AsyncOnlyStream(Stream innerStream) + { + _innerStream = innerStream; + } + + public override void Flush() + { + throw new NotSupportedException(); + } + + public override Task FlushAsync(CancellationToken cancellationToken) + { + return _innerStream.FlushAsync(cancellationToken); + } + + public override long Seek(long offset, SeekOrigin origin) + { + return _innerStream.Seek(offset, origin); + } + + public override void SetLength(long value) + { + _innerStream.SetLength(value); + } + + public override int Read(byte[] buffer, int offset, int count) + { + throw new NotSupportedException(); + } + + public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + return _innerStream.ReadAsync(buffer, offset, count, cancellationToken); + } + + public override void Write(byte[] buffer, int offset, int count) + { + throw new NotSupportedException(); + } + + public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + return _innerStream.WriteAsync(buffer, offset, count, cancellationToken); + } + + public override bool CanRead => _innerStream.CanRead; + public override bool CanSeek => _innerStream.CanSeek; + public override bool CanWrite => _innerStream.CanWrite; + public override long Length => _innerStream.Length; + + public override long Position + { + get => _innerStream.Position; + set => _innerStream.Position = value; + } + } + } +} +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonTextReader.Async.cs b/Src/Newtonsoft.Json/JsonTextReader.Async.cs index ae183c33..14f8902b 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.Async.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.Async.cs @@ -362,7 +362,7 @@ namespace Newtonsoft.Json if (char.IsNumber(currentChar) || currentChar == '-' || currentChar == '.') { - ParseNumber(ReadType.Read); + await ParseNumberAsync(ReadType.Read, cancellationToken).ConfigureAwait(false); return true; }