Asynchronous methods in readers and writers. (#1122)

Contributes to #650
This commit is contained in:
Jon Hanna
2017-01-06 15:33:39 +13:00
committed by James Newton-King
parent 211b3b0db2
commit f663060005
27 changed files with 14440 additions and 402 deletions
+15 -1
View File
@@ -53,7 +53,7 @@ namespace Newtonsoft.Json.TestConsole
//t.DeserializeLargeJson(); //t.DeserializeLargeJson();
//PerformanceTests t = new PerformanceTests(); //PerformanceTests t = new PerformanceTests();
LargeArrayJTokenPathPerformance(); SerializeJsonAsync();
//t.Iterations = 50000; //t.Iterations = 50000;
//t.BenchmarkDeserializeMethod<TestClass>(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText); //t.BenchmarkDeserializeMethod<TestClass>(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() public static void DeserializeJson()
{ {
PerformanceTests t = new PerformanceTests(); PerformanceTests t = new PerformanceTests();
File diff suppressed because it is too large Load Diff
@@ -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<JsonWriterException>(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<JsonWriterException>(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<JsonWriterException>(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<JsonWriterException>(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<JsonWriterException>(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
File diff suppressed because it is too large Load Diff
@@ -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<JsonReaderException>(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<JsonReaderException>(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
@@ -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<img src=rdb:\/\/13264>\r\n\r\n#L \""Hacker Tool\"" \""87814\""\r\n<img src=rdb:\/\/99282>\r\n\r\n#L \""Clanalizer\"" \""208313\""\r\n<img src=rdb:\/\/156479>\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<img src=rdb:\/\/13264>\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<img src=rdb:\/\/13264>\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<img src=rdb://13264>\r\n\r\n#L \"Hacker Tool\" \"87814\"\r\n<img src=rdb://99282>\r\n\r\n#L \"Clanalizer\" \"208313\"\r\n<img src=rdb://156479>\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<img src=rdb://13264>\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<img src=rdb://13264>\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<string> 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
@@ -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<JsonReaderException>(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<JsonReaderException>(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<JsonReaderException>(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<JsonReaderException>(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<JsonReaderException>(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<JsonReaderException>(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<JsonReaderException>(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<JsonReaderException>(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<JsonReaderException>(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<JsonReaderException>(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<JsonReaderException>(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<JsonReaderException>(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<JsonReaderException>(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
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -1649,7 +1649,7 @@ null//comment
public class CustomJsonTextWriter : JsonTextWriter public class CustomJsonTextWriter : JsonTextWriter
{ {
private readonly TextWriter _writer; protected readonly TextWriter _writer;
public CustomJsonTextWriter(TextWriter textWriter) : base(textWriter) public CustomJsonTextWriter(TextWriter textWriter) : base(textWriter)
{ {
@@ -107,8 +107,10 @@
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Bson\BsonWriterTests.cs" /> <Compile Include="Bson\BsonReaderAsyncTests.cs" />
<Compile Include="Bson\BsonReaderTests.cs" /> <Compile Include="Bson\BsonReaderTests.cs" />
<Compile Include="Bson\BsonWriterAsyncTests.cs" />
<Compile Include="Bson\BsonWriterTests.cs" />
<Compile Include="Converters\BinaryConverterTests.cs" /> <Compile Include="Converters\BinaryConverterTests.cs" />
<Compile Include="Converters\DiscriminatedUnionConverterTests.cs" /> <Compile Include="Converters\DiscriminatedUnionConverterTests.cs" />
<Compile Include="Converters\ExpandoObjectConverterTests.cs" /> <Compile Include="Converters\ExpandoObjectConverterTests.cs" />
@@ -122,10 +124,15 @@
<Compile Include="Converters\VersionConverterTests.cs" /> <Compile Include="Converters\VersionConverterTests.cs" />
<Compile Include="JsonArrayAttributeTests.cs" /> <Compile Include="JsonArrayAttributeTests.cs" />
<Compile Include="ExceptionTests.cs" /> <Compile Include="ExceptionTests.cs" />
<Compile Include="JsonTextReaderTests\ExceptionHandlingAsyncTests.cs" />
<Compile Include="JsonTextReaderTests\ExceptionHandlingTests.cs" /> <Compile Include="JsonTextReaderTests\ExceptionHandlingTests.cs" />
<Compile Include="JsonTextReaderTests\FloatAsyncTests.cs" />
<Compile Include="JsonTextReaderTests\FloatTests.cs" /> <Compile Include="JsonTextReaderTests\FloatTests.cs" />
<Compile Include="JsonTextReaderTests\MiscAsyncTests.cs" />
<Compile Include="JsonTextReaderTests\MiscTests.cs" /> <Compile Include="JsonTextReaderTests\MiscTests.cs" />
<Compile Include="JsonTextReaderTests\ParseAsyncTests.cs" />
<Compile Include="JsonTextReaderTests\ParseTests.cs" /> <Compile Include="JsonTextReaderTests\ParseTests.cs" />
<Compile Include="JsonTextReaderTests\ReadAsyncTests.cs" />
<Compile Include="JsonTextReaderTests\ReadTests.cs" /> <Compile Include="JsonTextReaderTests\ReadTests.cs" />
<Compile Include="JsonValidatingReaderTests.cs" /> <Compile Include="JsonValidatingReaderTests.cs" />
<Compile Include="Linq\ComponentModel\BindingTests.cs" /> <Compile Include="Linq\ComponentModel\BindingTests.cs" />
@@ -341,6 +348,7 @@
<Compile Include="JsonConvertTest.cs" /> <Compile Include="JsonConvertTest.cs" />
<Compile Include="Converters\JavaScriptDateTimeConverterTests.cs" /> <Compile Include="Converters\JavaScriptDateTimeConverterTests.cs" />
<Compile Include="Serialization\JsonSerializerTest.cs" /> <Compile Include="Serialization\JsonSerializerTest.cs" />
<Compile Include="JsonTextWriterAsyncTests.cs" />
<Compile Include="JsonTextWriterTest.cs" /> <Compile Include="JsonTextWriterTest.cs" />
<Compile Include="Linq\JTokenReaderTest.cs" /> <Compile Include="Linq\JTokenReaderTest.cs" />
<Compile Include="Linq\JTokenWriterTest.cs" /> <Compile Include="Linq\JTokenWriterTest.cs" />
@@ -49,7 +49,9 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Bson\BsonReaderAsyncTests.cs" />
<Compile Include="Bson\BsonReaderTests.cs" /> <Compile Include="Bson\BsonReaderTests.cs" />
<Compile Include="Bson\BsonWriterAsyncTests.cs" />
<Compile Include="Bson\BsonWriterTests.cs" /> <Compile Include="Bson\BsonWriterTests.cs" />
<Compile Include="Converters\BinaryConverterTests.cs" /> <Compile Include="Converters\BinaryConverterTests.cs" />
<Compile Include="Converters\CustomCreationConverterTests.cs" /> <Compile Include="Converters\CustomCreationConverterTests.cs" />
@@ -222,8 +224,14 @@
<Compile Include="ExceptionTests.cs" /> <Compile Include="ExceptionTests.cs" />
<Compile Include="JsonArrayAttributeTests.cs" /> <Compile Include="JsonArrayAttributeTests.cs" />
<Compile Include="JsonConvertTest.cs" /> <Compile Include="JsonConvertTest.cs" />
<Compile Include="JsonTextReaderTests\ExceptionHandlingAsyncTests.cs" />
<Compile Include="JsonTextReaderTests\FloatAsyncTests.cs" />
<Compile Include="JsonTextReaderTests\MiscAsyncTests.cs" />
<Compile Include="JsonTextReaderTests\MiscTests.cs" /> <Compile Include="JsonTextReaderTests\MiscTests.cs" />
<Compile Include="JsonTextReaderTests\ExceptionHandlingTests.cs" /> <Compile Include="JsonTextReaderTests\ExceptionHandlingTests.cs" />
<Compile Include="JsonTextReaderTests\ParseAsyncTests.cs" />
<Compile Include="JsonTextReaderTests\ReadAsyncTests.cs" />
<Compile Include="JsonTextWriterAsyncTests.cs" />
<Compile Include="TestObjects\JsonReaderStubWithIsClosed.cs" /> <Compile Include="TestObjects\JsonReaderStubWithIsClosed.cs" />
<Compile Include="TestObjects\JsonTextReaderTests\FakeArrayPool.cs" /> <Compile Include="TestObjects\JsonTextReaderTests\FakeArrayPool.cs" />
<Compile Include="JsonTextReaderTests\FloatTests.cs" /> <Compile Include="JsonTextReaderTests\FloatTests.cs" />
+221 -2
View File
@@ -43,6 +43,7 @@ using Newtonsoft.Json.Bson;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters; using Newtonsoft.Json.Converters;
using System.Threading.Tasks;
namespace Newtonsoft.Json.Tests namespace Newtonsoft.Json.Tests
{ {
@@ -69,8 +70,8 @@ namespace Newtonsoft.Json.Tests
#if DEBUG #if DEBUG
public int Iterations = 1; public int Iterations = 1;
#else #else
public int Iterations = 100; //public int Iterations = 100;
//public int Iterations = 10000; public int Iterations = 10000;
#endif #endif
#region Data #region Data
@@ -99,6 +100,7 @@ namespace Newtonsoft.Json.Tests
JsonNetBinary, JsonNetBinary,
JsonNetLinq, JsonNetLinq,
JsonNetManual, JsonNetManual,
JsonNetManualAsync,
BinaryFormatter, BinaryFormatter,
JavaScriptSerializer, JavaScriptSerializer,
DataContractSerializer, DataContractSerializer,
@@ -136,6 +138,14 @@ namespace Newtonsoft.Json.Tests
SerializeTests(test); SerializeTests(test);
} }
[Test]
public async Task SerializeAsync()
{
TestClass test = CreateSerializationObject();
await BenchmarkSerializeMethodAsync(SerializeMethod.JsonNetManualAsync, test);
}
[Test] [Test]
public void ReadLargeJson() public void ReadLargeJson()
{ {
@@ -238,6 +248,12 @@ namespace Newtonsoft.Json.Tests
BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetBinary, HexToBytes(BsonHex)); BenchmarkDeserializeMethod<TestClass>(SerializeMethod.JsonNetBinary, HexToBytes(BsonHex));
} }
[Test]
public async Task DeserializeAsync()
{
await BenchmarkDeserializeMethodAsync<TestClass>(SerializeMethod.JsonNetManualAsync, JsonText);
}
public void DeserializeTests<T>(string json) public void DeserializeTests<T>(string json)
{ {
BenchmarkDeserializeMethod<T>(SerializeMethod.JavaScriptSerializer, json); BenchmarkDeserializeMethod<T>(SerializeMethod.JavaScriptSerializer, json);
@@ -641,6 +657,27 @@ If attributes are not mentioned, default values are used in each case.
#region Serialize #region Serialize
private static readonly byte[] Buffer = new byte[4096]; 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) public void BenchmarkSerializeMethod(SerializeMethod method, object value)
{ {
Serialize(method, value); Serialize(method, value);
@@ -738,6 +775,83 @@ If attributes are not mentioned, default values are used in each case.
} }
} }
private async Task<string> 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<string, int> 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) private string Serialize(SerializeMethod method, object value)
{ {
string json; string json;
@@ -918,6 +1032,35 @@ If attributes are not mentioned, default values are used in each case.
Console.WriteLine(); Console.WriteLine();
} }
public async Task BenchmarkDeserializeMethodAsync<T>(SerializeMethod method, object json, int? iterations = null, bool warmUp = true)
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (warmUp)
{
await DeserializeAsync<T>(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<T>(method, json);
}
timed.Stop();
Console.WriteLine("Deserialize method: {0}", method);
Console.WriteLine("{0} ms", timed.ElapsedMilliseconds);
Console.WriteLine(value);
Console.WriteLine();
}
public T DeserializeJsonNet<T>(string json, bool isoDateTimeConverter) public T DeserializeJsonNet<T>(string json, bool isoDateTimeConverter)
{ {
Type type = typeof(T); Type type = typeof(T);
@@ -940,6 +1083,66 @@ If attributes are not mentioned, default values are used in each case.
} }
} }
public async Task<TestClass> 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) public TestClass DeserializeJsonNetManual(string json)
{ {
TestClass c = new TestClass(); 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); return (T)dataContractSerializer.ReadObject(ms);
} }
private async Task<T> DeserializeAsync<T>(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<T>(SerializeMethod method, object json) private T Deserialize<T>(SerializeMethod method, object json)
{ {
switch (method) switch (method)
@@ -45,6 +45,9 @@ using NUnit.Framework;
#endif #endif
using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Utilities;
using System.Collections; using System.Collections;
#if !(NET20 || NET35 || NET40 || PORTABLE40)
using System.Threading.Tasks;
#endif
#if NET20 #if NET20
using Newtonsoft.Json.Utilities.LinqBridge; using Newtonsoft.Json.Utilities.LinqBridge;
#else #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); 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<TException> ThrowsAsync<TException>(Func<Task> 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
} }
} }
+210
View File
@@ -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
{
/// <summary>
/// Asynchronously reads the next JSON token from the source.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous read. The <see cref="Task{TResult}.Result"/>
/// property returns <c>true</c> if the next token was read successfully; <c>false</c> if there are no more tokens to read.</returns>
/// <remarks>The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asychronousity.</remarks>
public virtual Task<bool> ReadAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return cancellationToken.CancelIfRequestedAsync<bool>() ?? Read().ToAsync();
}
/// <summary>
/// Asynchronously skips the children of the current token.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns>
/// <remarks>The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asychronousity.</remarks>
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();
}
}
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}"/> of <see cref="bool"/>.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous read. The <see cref="Task{TResult}.Result"/>
/// property returns the <see cref="Nullable{T}"/> of <see cref="bool"/>. This result will be <c>null</c> at the end of an array.</returns>
/// <remarks>The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asychronousity.</remarks>
public virtual Task<bool?> ReadAsBooleanAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return cancellationToken.CancelIfRequestedAsync<bool?>() ?? Task.FromResult(ReadAsBoolean());
}
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="byte"/>[].
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous read. The <see cref="Task{TResult}.Result"/>
/// property returns the <see cref="byte"/>[]. This result will be <c>null</c> at the end of an array.</returns>
/// <remarks>The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asychronousity.</remarks>
public virtual Task<byte[]> ReadAsBytesAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return cancellationToken.CancelIfRequestedAsync<byte[]>() ?? Task.FromResult(ReadAsBytes());
}
internal async Task<byte[]> ReadArrayIntoByteArrayAsync(CancellationToken cancellationToken)
{
List<byte> buffer = new List<byte>();
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;
}
}
}
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}"/> of <see cref="DateTime"/>.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous read. The <see cref="Task{TResult}.Result"/>
/// property returns the <see cref="Nullable{T}"/> of <see cref="DateTime"/>. This result will be <c>null</c> at the end of an array.</returns>
/// <remarks>The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asychronousity.</remarks>
public virtual Task<DateTime?> ReadAsDateTimeAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return cancellationToken.CancelIfRequestedAsync<DateTime?>() ?? Task.FromResult(ReadAsDateTime());
}
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}"/> of <see cref="DateTimeOffset"/>.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous read. The <see cref="Task{TResult}.Result"/>
/// property returns the <see cref="Nullable{T}"/> of <see cref="DateTimeOffset"/>. This result will be <c>null</c> at the end of an array.</returns>
/// <remarks>The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asychronousity.</remarks>
public virtual Task<DateTimeOffset?> ReadAsDateTimeOffsetAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return cancellationToken.CancelIfRequestedAsync<DateTimeOffset?>() ?? Task.FromResult(ReadAsDateTimeOffset());
}
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}"/> of <see cref="decimal"/>.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous read. The <see cref="Task{TResult}.Result"/>
/// property returns the <see cref="Nullable{T}"/> of <see cref="decimal"/>. This result will be <c>null</c> at the end of an array.</returns>
/// <remarks>The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asychronousity.</remarks>
public virtual Task<decimal?> ReadAsDecimalAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return cancellationToken.CancelIfRequestedAsync<decimal?>() ?? Task.FromResult(ReadAsDecimal());
}
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}"/> of <see cref="double"/>.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous read. The <see cref="Task{TResult}.Result"/>
/// property returns the <see cref="Nullable{T}"/> of <see cref="double"/>. This result will be <c>null</c> at the end of an array.</returns>
/// <remarks>The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asychronousity.</remarks>
public virtual Task<double?> ReadAsDoubleAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return Task.FromResult(ReadAsDouble());
}
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="Nullable{T}"/> of <see cref="int"/>.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous read. The <see cref="Task{TResult}.Result"/>
/// property returns the <see cref="Nullable{T}"/> of <see cref="int"/>. This result will be <c>null</c> at the end of an array.</returns>
/// <remarks>The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asychronousity.</remarks>
public virtual Task<int?> ReadAsInt32Async(CancellationToken cancellationToken = default(CancellationToken))
{
return cancellationToken.CancelIfRequestedAsync<int?>() ?? Task.FromResult(ReadAsInt32());
}
/// <summary>
/// Asynchronously reads the next JSON token from the source as a <see cref="string"/>.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A <see cref="Task{TResult}"/> that represents the asynchronous read. The <see cref="Task{TResult}.Result"/>
/// property returns the <see cref="string"/>. This result will be <c>null</c> at the end of an array.</returns>
/// <remarks>The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asychronousity.</remarks>
public virtual Task<string> ReadAsStringAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return cancellationToken.CancelIfRequestedAsync<string>() ?? Task.FromResult(ReadAsString());
}
}
}
#endif
+23 -9
View File
@@ -43,7 +43,7 @@ namespace Newtonsoft.Json
/// <summary> /// <summary>
/// Represents a reader that provides fast, non-cached, forward-only access to serialized JSON data. /// Represents a reader that provides fast, non-cached, forward-only access to serialized JSON data.
/// </summary> /// </summary>
public abstract class JsonReader : IDisposable public abstract partial class JsonReader : IDisposable
{ {
/// <summary> /// <summary>
/// Specifies the state of the reader. /// Specifies the state of the reader.
@@ -583,21 +583,35 @@ namespace Newtonsoft.Json
while (true) while (true)
{ {
JsonToken t = GetContentToken(); if (!Read())
switch (t) {
SetToken(JsonToken.None);
}
if (ReadArrayElementIntoByteArrayReportDone(buffer))
{
byte[] d = buffer.ToArray();
SetToken(JsonToken.Bytes, d, false);
return d;
}
}
}
private bool ReadArrayElementIntoByteArrayReportDone(List<byte> buffer)
{
switch (TokenType)
{ {
case JsonToken.None: case JsonToken.None:
throw JsonReaderException.Create(this, "Unexpected end when reading bytes."); throw JsonReaderException.Create(this, "Unexpected end when reading bytes.");
case JsonToken.Integer: case JsonToken.Integer:
buffer.Add(Convert.ToByte(Value, CultureInfo.InvariantCulture)); buffer.Add(Convert.ToByte(Value, CultureInfo.InvariantCulture));
break; return false;
case JsonToken.EndArray: case JsonToken.EndArray:
byte[] d = buffer.ToArray(); return true;
SetToken(JsonToken.Bytes, d, false); case JsonToken.Comment:
return d; return false;
default: default:
throw JsonReaderException.Create(this, "Unexpected token when reading bytes: {0}.".FormatWith(CultureInfo.InvariantCulture, t)); throw JsonReaderException.Create(this, "Unexpected token when reading bytes: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
}
} }
} }
File diff suppressed because it is too large Load Diff
+149 -80
View File
@@ -24,10 +24,7 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text;
using System.IO; using System.IO;
using System.Globalization; using System.Globalization;
#if !(PORTABLE || PORTABLE40 || NET35 || NET20) || NETSTANDARD1_1 #if !(PORTABLE || PORTABLE40 || NET35 || NET20) || NETSTANDARD1_1
@@ -55,7 +52,7 @@ namespace Newtonsoft.Json
/// <summary> /// <summary>
/// Represents a reader that provides fast, non-cached, forward-only access to JSON text data. /// Represents a reader that provides fast, non-cached, forward-only access to JSON text data.
/// </summary> /// </summary>
public class JsonTextReader : JsonReader, IJsonLineInfo public partial class JsonTextReader : JsonReader, IJsonLineInfo
{ {
private const char UnicodeReplacementChar = '\uFFFD'; private const char UnicodeReplacementChar = '\uFFFD';
private const int MaximumJavascriptIntegerCharacterLength = 380; private const int MaximumJavascriptIntegerCharacterLength = 380;
@@ -85,6 +82,10 @@ namespace Newtonsoft.Json
_reader = reader; _reader = reader;
_lineNumber = 1; _lineNumber = 1;
#if !(NET20 || NET35 || NET40 || PORTABLE40)
_safeAsync = GetType() == typeof(JsonTextReader);
#endif
} }
#if DEBUG #if DEBUG
@@ -131,6 +132,11 @@ namespace Newtonsoft.Json
ShiftBufferIfNeeded(); ShiftBufferIfNeeded();
ReadStringIntoBuffer(quote); ReadStringIntoBuffer(quote);
ParseReadString(quote, readType);
}
private void ParseReadString(char quote, ReadType readType)
{
SetPostValueState(true); SetPostValueState(true);
switch (readType) switch (readType)
@@ -243,13 +249,8 @@ namespace Newtonsoft.Json
return ReadData(append, 0); 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 // char buffer is full
if (_charsUsed + charsRequired >= _chars.Length - 1) if (_charsUsed + charsRequired >= _chars.Length - 1)
{ {
@@ -299,6 +300,16 @@ namespace Newtonsoft.Json
_charsUsed = remainingCharCount; _charsUsed = remainingCharCount;
} }
} }
}
private int ReadData(bool append, int charsRequired)
{
if (_isEndOfFile)
{
return 0;
}
PrepareBufferForReadData(append, charsRequired);
int attemptCharReadCount = _chars.Length - _charsUsed - 1; int attemptCharReadCount = _chars.Length - _charsUsed - 1;
@@ -574,29 +585,7 @@ namespace Newtonsoft.Json
case '"': case '"':
case '\'': case '\'':
ParseString(currentChar, readType); ParseString(currentChar, readType);
switch (readType) return FinishReadQuotedStringValue(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));
}
case '-': case '-':
if (EnsureChars(1, true) && _chars[_charPos + 1] == 'I') 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) private JsonReaderException CreateUnexpectedCharacterException(char c)
{ {
return JsonReaderException.Create(this, "Unexpected character encountered while parsing value: {0}.".FormatWith(CultureInfo.InvariantCulture, 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 '"':
case '\'': case '\'':
ParseString(currentChar, readType); ParseString(currentChar, readType);
switch (readType) return FinishReadQuotedNumber(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));
}
case 'n': case 'n':
HandleNull(); HandleNull();
return null; 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 #if !NET20
/// <summary> /// <summary>
/// Reads the next JSON token from the underlying <see cref="TextReader"/> as a <see cref="Nullable{T}"/> of <see cref="DateTimeOffset"/>. /// Reads the next JSON token from the underlying <see cref="TextReader"/> as a <see cref="Nullable{T}"/> of <see cref="DateTimeOffset"/>.
@@ -1186,8 +1208,16 @@ namespace Newtonsoft.Json
case '\'': case '\'':
if (_chars[charPos - 1] == quote) if (_chars[charPos - 1] == quote)
{ {
charPos--; FinishReadStringIntoBuffer(charPos - 1, initialPosition, lastWritePosition);
return;
}
break;
}
}
}
private void FinishReadStringIntoBuffer(int charPos, int initialPosition, int lastWritePosition)
{
if (initialPosition == lastWritePosition) if (initialPosition == lastWritePosition)
{ {
_stringReference = new StringReference(_chars, initialPosition, charPos - initialPosition); _stringReference = new StringReference(_chars, initialPosition, charPos - initialPosition);
@@ -1204,13 +1234,7 @@ namespace Newtonsoft.Json
_stringReference = new StringReference(_stringBuffer.InternalBuffer, 0, _stringBuffer.Position); _stringReference = new StringReference(_stringBuffer.InternalBuffer, 0, _stringBuffer.Position);
} }
charPos++; _charPos = charPos + 1;
_charPos = charPos;
return;
}
break;
}
}
} }
private void WriteCharToBuffer(char writeChar, int lastWritePosition, int writeToPosition) private void WriteCharToBuffer(char writeChar, int lastWritePosition, int writeToPosition)
@@ -1223,21 +1247,23 @@ namespace Newtonsoft.Json
_stringBuffer.Append(_arrayPool, writeChar); _stringBuffer.Append(_arrayPool, writeChar);
} }
private char ParseUnicode() private char ConvertUnicode(bool enoughChars)
{ {
char writeChar; if (enoughChars)
if (EnsureChars(4, true))
{ {
char hexChar = Convert.ToChar(ConvertUtils.HexTextToInt(_chars, _charPos, _charPos + 4)); char hexChar = Convert.ToChar(ConvertUtils.HexTextToInt(_chars, _charPos, _charPos + 4));
writeChar = hexChar;
_charPos += 4; _charPos += 4;
return hexChar;
} }
else else
{ {
throw JsonReaderException.Create(this, "Unexpected end while parsing Unicode character."); throw JsonReaderException.Create(this, "Unexpected end while parsing Unicode character.");
} }
return writeChar; }
private char ParseUnicode()
{
return ConvertUnicode(EnsureChars(4, true));
} }
private void ReadNumberIntoBuffer() private void ReadNumberIntoBuffer()
@@ -1246,9 +1272,9 @@ namespace Newtonsoft.Json
while (true) while (true)
{ {
switch (_chars[charPos]) char currentChar = _chars[charPos];
if (currentChar == '\0')
{ {
case '\0':
_charPos = charPos; _charPos = charPos;
if (_charsUsed == charPos) if (_charsUsed == charPos)
@@ -1262,7 +1288,22 @@ namespace Newtonsoft.Json
{ {
return; return;
} }
break; }
else if (ReadNumberCharIntoBuffer(currentChar, charPos))
{
return;
}
else
{
charPos++;
}
}
}
private bool ReadNumberCharIntoBuffer(char currentChar, int charPos)
{
switch (currentChar)
{
case '-': case '-':
case '+': case '+':
case 'a': case 'a':
@@ -1290,21 +1331,18 @@ namespace Newtonsoft.Json
case '7': case '7':
case '8': case '8':
case '9': case '9':
charPos++; return false;
break;
default: default:
_charPos = charPos; _charPos = charPos;
char currentChar = _chars[_charPos];
if (char.IsWhiteSpace(currentChar) || currentChar == ',' || currentChar == '}' || currentChar == ']' || currentChar == ')' || currentChar == '/') if (char.IsWhiteSpace(currentChar) || currentChar == ',' || currentChar == '}' || currentChar == ']' || currentChar == ')' || currentChar == '/')
{ {
return; return true;
} }
throw JsonReaderException.Create(this, "Unexpected character encountered while parsing number: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar)); throw JsonReaderException.Create(this, "Unexpected character encountered while parsing number: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar));
} }
} }
}
private void ClearRecentString() private void ClearRecentString()
{ {
@@ -1503,9 +1541,9 @@ namespace Newtonsoft.Json
// parse unquoted property name until whitespace or colon // parse unquoted property name until whitespace or colon
while (true) 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)
@@ -1513,29 +1551,36 @@ namespace Newtonsoft.Json
throw JsonReaderException.Create(this, "Unexpected end while parsing unquoted property name."); throw JsonReaderException.Create(this, "Unexpected end while parsing unquoted property name.");
} }
break; continue;
} }
_stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition);
return; return;
default: }
char currentChar = _chars[_charPos];
if (ReadUnquotedPropertyReportIfDone(currentChar, initialPosition))
{
return;
}
}
}
private bool ReadUnquotedPropertyReportIfDone(char currentChar, int initialPosition)
{
if (ValidIdentifierChar(currentChar)) if (ValidIdentifierChar(currentChar))
{ {
_charPos++; _charPos++;
break; return false;
} }
else if (char.IsWhiteSpace(currentChar) || currentChar == ':')
if (char.IsWhiteSpace(currentChar) || currentChar == ':')
{ {
_stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition); _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition);
return; return true;
} }
throw JsonReaderException.Create(this, "Invalid JavaScript property identifier character: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar)); throw JsonReaderException.Create(this, "Invalid JavaScript property identifier character: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar));
} }
}
}
private bool ParseValue() private bool ParseValue()
{ {
@@ -1820,6 +1865,11 @@ namespace Newtonsoft.Json
ReadNumberIntoBuffer(); 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 // set state to PostValue now so that if there is an error parsing the number then the reader can continue
SetPostValueState(true); SetPostValueState(true);
@@ -2196,7 +2246,12 @@ namespace Newtonsoft.Json
private bool MatchValue(string value) 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; _charPos = _charsUsed;
throw CreateUnexpectedEndException(); throw CreateUnexpectedEndException();
@@ -2327,7 +2382,12 @@ namespace Newtonsoft.Json
private object ParseNumberNegativeInfinity(ReadType readType) 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) switch (readType)
{ {
@@ -2352,7 +2412,11 @@ namespace Newtonsoft.Json
private object ParseNumberPositiveInfinity(ReadType readType) 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) switch (readType)
{ {
@@ -2377,7 +2441,12 @@ namespace Newtonsoft.Json
private object ParseNumberNaN(ReadType readType) 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) switch (readType)
{ {
File diff suppressed because it is too large Load Diff
+100 -53
View File
@@ -39,8 +39,9 @@ namespace Newtonsoft.Json
/// <summary> /// <summary>
/// Represents a writer that provides a fast, non-cached, forward-only way of generating JSON data. /// Represents a writer that provides a fast, non-cached, forward-only way of generating JSON data.
/// </summary> /// </summary>
public class JsonTextWriter : JsonWriter public partial class JsonTextWriter : JsonWriter
{ {
private const int IndentCharBufferSize = 12;
private readonly TextWriter _writer; private readonly TextWriter _writer;
private Base64Encoder _base64Encoder; private Base64Encoder _base64Encoder;
private char _indentChar; private char _indentChar;
@@ -160,6 +161,10 @@ namespace Newtonsoft.Json
_indentation = 2; _indentation = 2;
UpdateCharEscapeFlags(); UpdateCharEscapeFlags();
#if !(NET20 || NET35 || NET40 || PORTABLE40)
_safeAsync = GetType() == typeof(JsonTextWriter);
#endif
} }
/// <summary> /// <summary>
@@ -178,6 +183,11 @@ namespace Newtonsoft.Json
{ {
base.Close(); base.Close();
CloseBufferAndWriter();
}
private void CloseBufferAndWriter()
{
if (_writeBuffer != null) if (_writeBuffer != null)
{ {
BufferUtils.ReturnBuffer(_arrayPool, _writeBuffer); BufferUtils.ReturnBuffer(_arrayPool, _writeBuffer);
@@ -308,27 +318,45 @@ namespace Newtonsoft.Json
/// </summary> /// </summary>
protected override void WriteIndent() protected override void WriteIndent()
{ {
_writer.WriteLine();
// levels of indentation multiplied by the indent count // levels of indentation multiplied by the indent count
int currentIndentCount = Top * _indentation; int currentIndentCount = Top * _indentation;
if (currentIndentCount > 0) int newLineLen = SetIndentChars();
{
if (_indentChars == null)
{
_indentChars = new string(_indentChar, 10).ToCharArray();
}
while (currentIndentCount > 0) _writer.Write(_indentChars, 0, newLineLen + Math.Min(currentIndentCount, IndentCharBufferSize));
while ((currentIndentCount -= IndentCharBufferSize) > 0)
{ {
int writeCount = Math.Min(currentIndentCount, 10); _writer.Write(_indentChars, newLineLen, Math.Min(currentIndentCount, IndentCharBufferSize));
_writer.Write(_indentChars, 0, writeCount);
currentIndentCount -= writeCount;
} }
} }
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)
{
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;
} }
/// <summary> /// <summary>
@@ -465,7 +493,7 @@ namespace Newtonsoft.Json
public override void WriteValue(ulong value) public override void WriteValue(ulong value)
{ {
InternalWriteValue(JsonToken.Integer); InternalWriteValue(JsonToken.Integer);
WriteIntegerValue(value); WriteIntegerValue(value, false);
} }
/// <summary> /// <summary>
@@ -605,14 +633,9 @@ namespace Newtonsoft.Json
if (string.IsNullOrEmpty(DateFormatString)) if (string.IsNullOrEmpty(DateFormatString))
{ {
EnsureWriteBuffer(); int length = WriteValueToBuffer(value);
int pos = 0; _writer.Write(_writeBuffer, 0, length);
_writeBuffer[pos++] = _quoteChar;
pos = DateTimeUtils.WriteDateTimeString(_writeBuffer, pos, value, null, value.Kind, DateFormatHandling);
_writeBuffer[pos++] = _quoteChar;
_writer.Write(_writeBuffer, 0, pos);
} }
else 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;
}
/// <summary> /// <summary>
/// Writes a <see cref="Byte"/>[] value. /// Writes a <see cref="Byte"/>[] value.
/// </summary> /// </summary>
@@ -653,14 +687,9 @@ namespace Newtonsoft.Json
if (string.IsNullOrEmpty(DateFormatString)) if (string.IsNullOrEmpty(DateFormatString))
{ {
EnsureWriteBuffer(); int length = WriteValueToBuffer(value);
int pos = 0; _writer.Write(_writeBuffer, 0, length);
_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);
} }
else else
{ {
@@ -669,6 +698,17 @@ namespace Newtonsoft.Json
_writer.Write(_quoteChar); _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 #endif
/// <summary> /// <summary>
@@ -771,38 +811,45 @@ namespace Newtonsoft.Json
} }
else else
{ {
ulong uvalue = (value < 0) ? (ulong)-value : (ulong)value; bool negative = value < 0;
WriteIntegerValue(negative ? (ulong)-value : (ulong)value, negative);
}
}
if (value < 0) private void WriteIntegerValue(ulong uvalue, bool negative)
{ {
_writer.Write('-'); if (!negative & uvalue <= 9)
}
WriteIntegerValue(uvalue);
}
}
private void WriteIntegerValue(ulong uvalue)
{
if (uvalue <= 9)
{ {
_writer.Write((char)('0' + uvalue)); _writer.Write((char)('0' + uvalue));
} }
else else
{ {
EnsureWriteBuffer(); int length = WriteNumberToBuffer(uvalue, negative);
int totalLength = MathUtils.IntLength(uvalue);
int length = 0;
do
{
_writeBuffer[totalLength - ++length] = (char)('0' + (uvalue % 10));
uvalue /= 10;
} while (uvalue != 0);
_writer.Write(_writeBuffer, 0, length); _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;
}
} }
} }
File diff suppressed because it is too large Load Diff
+42 -33
View File
@@ -43,7 +43,7 @@ namespace Newtonsoft.Json
/// <summary> /// <summary>
/// Represents a writer that provides a fast, non-cached, forward-only way of generating JSON data. /// Represents a writer that provides a fast, non-cached, forward-only way of generating JSON data.
/// </summary> /// </summary>
public abstract class JsonWriter : IDisposable public abstract partial class JsonWriter : IDisposable
{ {
internal enum State internal enum State
{ {
@@ -628,20 +628,7 @@ namespace Newtonsoft.Json
internal virtual void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments) internal virtual void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments)
{ {
int initialDepth; int initialDepth = CalculateWriteTokenDepth(reader);
if (reader.TokenType == JsonToken.None)
{
initialDepth = -1;
}
else if (!JsonTokenUtils.IsStartToken(reader.TokenType))
{
initialDepth = reader.Depth + 1;
}
else
{
initialDepth = reader.Depth;
}
do do
{ {
@@ -664,6 +651,17 @@ namespace Newtonsoft.Json
&& reader.Read()); && 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) private void WriteConstructorDate(JsonReader reader)
{ {
if (!reader.Read()) if (!reader.Read())
@@ -733,7 +731,33 @@ namespace Newtonsoft.Json
private void AutoCompleteClose(JsonContainerType type) 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; int levelsToComplete = 0;
if (_currentPosition.Type == type) if (_currentPosition.Type == type)
@@ -760,25 +784,11 @@ namespace Newtonsoft.Json
throw JsonWriterException.Create(this, "No token to close.", null); throw JsonWriterException.Create(this, "No token to close.", null);
} }
for (int i = 0; i < levelsToComplete; i++) return levelsToComplete;
{
JsonToken token = GetCloseTokenForType(Pop());
if (_currentState == State.Property)
{
WriteNull();
} }
if (_formatting == Formatting.Indented) private void UpdateCurrentState()
{ {
if (_currentState != State.ObjectStart && _currentState != State.ArrayStart)
{
WriteIndent();
}
}
WriteEnd(token);
JsonContainerType currentLevelType = Peek(); JsonContainerType currentLevelType = Peek();
switch (currentLevelType) switch (currentLevelType)
@@ -799,7 +809,6 @@ namespace Newtonsoft.Json
throw JsonWriterException.Create(this, "Unknown JsonType: " + currentLevelType, null); throw JsonWriterException.Create(this, "Unknown JsonType: " + currentLevelType, null);
} }
} }
}
/// <summary> /// <summary>
/// Writes the specified end token. /// Writes the specified end token.
@@ -93,13 +93,17 @@
<Compile Include="JsonObjectAttribute.cs" /> <Compile Include="JsonObjectAttribute.cs" />
<Compile Include="JsonPosition.cs" /> <Compile Include="JsonPosition.cs" />
<Compile Include="JsonPropertyAttribute.cs" /> <Compile Include="JsonPropertyAttribute.cs" />
<Compile Include="JsonReader.Async.cs" />
<Compile Include="JsonReader.cs" /> <Compile Include="JsonReader.cs" />
<Compile Include="JsonReaderException.cs" /> <Compile Include="JsonReaderException.cs" />
<Compile Include="JsonRequiredAttribute.cs" /> <Compile Include="JsonRequiredAttribute.cs" />
<Compile Include="JsonSerializationException.cs" /> <Compile Include="JsonSerializationException.cs" />
<Compile Include="JsonSerializer.cs" /> <Compile Include="JsonSerializer.cs" />
<Compile Include="JsonSerializerSettings.cs" /> <Compile Include="JsonSerializerSettings.cs" />
<Compile Include="JsonTextReader.Async.cs" />
<Compile Include="JsonTextReader.cs" /> <Compile Include="JsonTextReader.cs" />
<Compile Include="JsonTextWriter.Async.cs" />
<Compile Include="JsonWriter.Async.cs" />
<Compile Include="JsonTextWriter.cs" /> <Compile Include="JsonTextWriter.cs" />
<Compile Include="JsonToken.cs" /> <Compile Include="JsonToken.cs" />
<Compile Include="JsonValidatingReader.cs" /> <Compile Include="JsonValidatingReader.cs" />
@@ -215,6 +219,7 @@
<Compile Include="TraceLevel.cs" /> <Compile Include="TraceLevel.cs" />
<Compile Include="TypeNameAssemblyFormatHandling.cs" /> <Compile Include="TypeNameAssemblyFormatHandling.cs" />
<Compile Include="TypeNameHandling.cs" /> <Compile Include="TypeNameHandling.cs" />
<Compile Include="Utilities\AsyncUtils.cs" />
<Compile Include="Utilities\Base64Encoder.cs" /> <Compile Include="Utilities\Base64Encoder.cs" />
<Compile Include="Utilities\BidirectionalDictionary.cs" /> <Compile Include="Utilities\BidirectionalDictionary.cs" />
<Compile Include="Utilities\CollectionUtils.cs" /> <Compile Include="Utilities\CollectionUtils.cs" />
@@ -96,16 +96,20 @@
<Compile Include="JsonObjectAttribute.cs" /> <Compile Include="JsonObjectAttribute.cs" />
<Compile Include="JsonPosition.cs" /> <Compile Include="JsonPosition.cs" />
<Compile Include="JsonPropertyAttribute.cs" /> <Compile Include="JsonPropertyAttribute.cs" />
<Compile Include="JsonReader.Async.cs" />
<Compile Include="JsonReader.cs" /> <Compile Include="JsonReader.cs" />
<Compile Include="JsonReaderException.cs" /> <Compile Include="JsonReaderException.cs" />
<Compile Include="JsonRequiredAttribute.cs" /> <Compile Include="JsonRequiredAttribute.cs" />
<Compile Include="JsonSerializationException.cs" /> <Compile Include="JsonSerializationException.cs" />
<Compile Include="JsonSerializer.cs" /> <Compile Include="JsonSerializer.cs" />
<Compile Include="JsonSerializerSettings.cs" /> <Compile Include="JsonSerializerSettings.cs" />
<Compile Include="JsonTextReader.Async.cs" />
<Compile Include="JsonTextReader.cs" /> <Compile Include="JsonTextReader.cs" />
<Compile Include="JsonTextWriter.Async.cs" />
<Compile Include="JsonTextWriter.cs" /> <Compile Include="JsonTextWriter.cs" />
<Compile Include="JsonToken.cs" /> <Compile Include="JsonToken.cs" />
<Compile Include="JsonValidatingReader.cs" /> <Compile Include="JsonValidatingReader.cs" />
<Compile Include="JsonWriter.Async.cs" />
<Compile Include="JsonWriter.cs" /> <Compile Include="JsonWriter.cs" />
<Compile Include="JsonWriterException.cs" /> <Compile Include="JsonWriterException.cs" />
<Compile Include="Linq\CommentHandling.cs" /> <Compile Include="Linq\CommentHandling.cs" />
@@ -218,6 +222,7 @@
<Compile Include="StringEscapeHandling.cs" /> <Compile Include="StringEscapeHandling.cs" />
<Compile Include="TraceLevel.cs" /> <Compile Include="TraceLevel.cs" />
<Compile Include="TypeNameHandling.cs" /> <Compile Include="TypeNameHandling.cs" />
<Compile Include="Utilities\AsyncUtils.cs" />
<Compile Include="Utilities\Base64Encoder.cs" /> <Compile Include="Utilities\Base64Encoder.cs" />
<Compile Include="Utilities\BidirectionalDictionary.cs" /> <Compile Include="Utilities\BidirectionalDictionary.cs" />
<Compile Include="Utilities\CollectionUtils.cs" /> <Compile Include="Utilities\CollectionUtils.cs" />
+104
View File
@@ -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<bool> False = Task.FromResult(false);
public static readonly Task<bool> True = Task.FromResult(true);
internal static Task<bool> ToAsync(this bool value) => value ? True : False;
public static Task CancelIfRequestedAsync(this CancellationToken cancellationToken)
{
return cancellationToken.IsCancellationRequested ? FromCanceled(cancellationToken) : null;
}
public static Task<T> CancelIfRequestedAsync<T>(this CancellationToken cancellationToken)
{
return cancellationToken.IsCancellationRequested ? FromCanceled<T>(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<T> FromCanceled<T>(this CancellationToken cancellationToken)
{
Debug.Assert(cancellationToken.IsCancellationRequested);
return new Task<T>(() => 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<int> ReadAsync(this TextReader reader, char[] buffer, int index, int count, CancellationToken cancellationToken)
{
Debug.Assert(reader != null);
return cancellationToken.IsCancellationRequested ? FromCanceled<int>(cancellationToken) : reader.ReadAsync(buffer, index, count);
}
}
}
#endif
+111 -22
View File
@@ -25,6 +25,10 @@
using System; using System;
using System.IO; using System.IO;
#if !(NET20 || NET35 || NET40 || PORTABLE40)
using System.Threading;
using System.Threading.Tasks;
#endif
namespace Newtonsoft.Json.Utilities namespace Newtonsoft.Json.Utilities
{ {
@@ -45,7 +49,7 @@ namespace Newtonsoft.Json.Utilities
_writer = writer; _writer = writer;
} }
public void Encode(byte[] buffer, int index, int count) private void ValidateEncode(byte[] buffer, int index, int count)
{ {
if (buffer == null) if (buffer == null)
{ {
@@ -66,36 +70,25 @@ namespace Newtonsoft.Json.Utilities
{ {
throw new ArgumentOutOfRangeException(nameof(count)); throw new ArgumentOutOfRangeException(nameof(count));
} }
}
public void Encode(byte[] buffer, int index, int count)
{
ValidateEncode(buffer, index, count);
if (_leftOverBytesCount > 0) if (_leftOverBytesCount > 0)
{ {
int leftOverBytesCount = _leftOverBytesCount; if(FulfillFromLeftover(buffer, index, ref count))
while (leftOverBytesCount < 3 && count > 0)
{ {
_leftOverBytes[leftOverBytesCount++] = buffer[index++];
count--;
}
if (count == 0 && leftOverBytesCount < 3)
{
_leftOverBytesCount = leftOverBytesCount;
return; return;
} }
int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0); int num2 = Convert.ToBase64CharArray(_leftOverBytes, 0, 3, _charsLine, 0);
WriteChars(_charsLine, 0, num2); WriteChars(_charsLine, 0, num2);
} }
_leftOverBytesCount = count % 3;
if (_leftOverBytesCount > 0) StoreLeftOverBytes(buffer, index, ref count);
{
count -= _leftOverBytesCount;
if (_leftOverBytes == null)
{
_leftOverBytes = new byte[3];
}
for (int i = 0; i < _leftOverBytesCount; i++)
{
_leftOverBytes[i] = buffer[(index + count) + i];
}
}
int num4 = index + count; int num4 = index + count;
int length = LineSizeInBytes; int length = LineSizeInBytes;
while (index < num4) 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() public void Flush()
{ {
if (_leftOverBytesCount > 0) if (_leftOverBytesCount > 0)
@@ -124,5 +155,63 @@ namespace Newtonsoft.Json.Utilities
{ {
_writer.Write(chars, index, count); _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
} }
} }
+228 -13
View File
@@ -24,17 +24,16 @@
#endregion #endregion
using System; using System;
using System.Collections;
using System.Globalization;
using System.IO; using System.IO;
using System.Text; #if !(NET20 || NET35 || NET40 || PORTABLE40)
using System.Text.RegularExpressions; using System.Threading;
using System.Threading.Tasks;
#endif
using System.Collections.Generic; using System.Collections.Generic;
#if NET20 #if NET20
using Newtonsoft.Json.Utilities.LinqBridge; using Newtonsoft.Json.Utilities.LinqBridge;
#else #else
using System.Linq; using System.Linq;
#endif #endif
namespace Newtonsoft.Json.Utilities namespace Newtonsoft.Json.Utilities
@@ -141,6 +140,38 @@ namespace Newtonsoft.Json.Utilities
return false; 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, public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters,
bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool<char> bufferPool, ref char[] writeBuffer) bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool<char> bufferPool, ref char[] writeBuffer)
{ {
@@ -277,14 +308,7 @@ namespace Newtonsoft.Json.Utilities
} }
else else
{ {
int length = s.Length - lastWritePosition; int length = WriteEscapedStringThroughBuffer(s, lastWritePosition, bufferPool, ref writeBuffer);
if (writeBuffer == null || writeBuffer.Length < length)
{
writeBuffer = BufferUtils.EnsureBufferSize(bufferPool, length, writeBuffer);
}
s.CopyTo(lastWritePosition, writeBuffer, 0, length);
// write remaining text // write remaining text
writer.Write(writeBuffer, 0, length); writer.Write(writeBuffer, 0, length);
@@ -298,6 +322,20 @@ namespace Newtonsoft.Json.Utilities
} }
} }
private static int WriteEscapedStringThroughBuffer(string s, int lastWritePosition, IArrayPool<char> 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) public static string ToEscapedJavaScriptString(string value, char delimiter, bool appendDelimiters, StringEscapeHandling stringEscapeHandling)
{ {
bool[] charEscapeFlags = GetCharEscapeFlags(stringEscapeHandling, delimiter); bool[] charEscapeFlags = GetCharEscapeFlags(stringEscapeHandling, delimiter);
@@ -309,5 +347,182 @@ namespace Newtonsoft.Json.Utilities
return w.ToString(); 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
} }
} }