Merge branch 'less-async' of https://github.com/benaadams/Newtonsoft.Json into benaadams-less-async

This commit is contained in:
James Newton-King
2017-09-03 09:16:30 +12:00
7 changed files with 162 additions and 25 deletions
+2 -2
View File
@@ -78,7 +78,7 @@ namespace Newtonsoft.Json
return ParseObjectAsync(cancellationToken); return ParseObjectAsync(cancellationToken);
case State.PostValue: case State.PostValue:
Task<bool> task = ParsePostValueAsync(false, cancellationToken); Task<bool> task = ParsePostValueAsync(false, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
if (task.Result) if (task.Result)
{ {
@@ -531,7 +531,7 @@ namespace Newtonsoft.Json
_charPos++; _charPos++;
Task<bool> task = EnsureCharsAsync(1, append, cancellationToken); Task<bool> task = EnsureCharsAsync(1, append, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
SetNewLine(task.Result); SetNewLine(task.Result);
return AsyncUtils.CompletedTask; return AsyncUtils.CompletedTask;
+10 -10
View File
@@ -186,7 +186,7 @@ namespace Newtonsoft.Json
private Task WriteValueInternalAsync(JsonToken token, string value, CancellationToken cancellationToken) private Task WriteValueInternalAsync(JsonToken token, string value, CancellationToken cancellationToken)
{ {
Task task = InternalWriteValueAsync(token, cancellationToken); Task task = InternalWriteValueAsync(token, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
return _writer.WriteAsync(value, cancellationToken); return _writer.WriteAsync(value, cancellationToken);
} }
@@ -266,7 +266,7 @@ namespace Newtonsoft.Json
private Task WriteIntegerValueAsync(ulong uvalue, bool negative, CancellationToken cancellationToken) private Task WriteIntegerValueAsync(ulong uvalue, bool negative, CancellationToken cancellationToken)
{ {
Task task = InternalWriteValueAsync(JsonToken.Integer, cancellationToken); Task task = InternalWriteValueAsync(JsonToken.Integer, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
return WriteDigitsAsync(uvalue, negative, cancellationToken); return WriteDigitsAsync(uvalue, negative, cancellationToken);
} }
@@ -317,13 +317,13 @@ namespace Newtonsoft.Json
internal Task DoWritePropertyNameAsync(string name, CancellationToken cancellationToken) internal Task DoWritePropertyNameAsync(string name, CancellationToken cancellationToken)
{ {
Task task = InternalWritePropertyNameAsync(name, cancellationToken); Task task = InternalWritePropertyNameAsync(name, cancellationToken);
if (task.Status != TaskStatus.RanToCompletion) if (!task.IsCompletedSucessfully())
{ {
return DoWritePropertyNameAsync(task, name, cancellationToken); return DoWritePropertyNameAsync(task, name, cancellationToken);
} }
task = WriteEscapedStringAsync(name, _quoteName, cancellationToken); task = WriteEscapedStringAsync(name, _quoteName, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
return _writer.WriteAsync(':', cancellationToken); return _writer.WriteAsync(':', cancellationToken);
} }
@@ -395,7 +395,7 @@ namespace Newtonsoft.Json
internal Task DoWriteStartArrayAsync(CancellationToken cancellationToken) internal Task DoWriteStartArrayAsync(CancellationToken cancellationToken)
{ {
Task task = InternalWriteStartAsync(JsonToken.StartArray, JsonContainerType.Array, cancellationToken); Task task = InternalWriteStartAsync(JsonToken.StartArray, JsonContainerType.Array, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
return _writer.WriteAsync('[', cancellationToken); return _writer.WriteAsync('[', cancellationToken);
} }
@@ -425,7 +425,7 @@ namespace Newtonsoft.Json
internal Task DoWriteStartObjectAsync(CancellationToken cancellationToken) internal Task DoWriteStartObjectAsync(CancellationToken cancellationToken)
{ {
Task task = InternalWriteStartAsync(JsonToken.StartObject, JsonContainerType.Object, cancellationToken); Task task = InternalWriteStartAsync(JsonToken.StartObject, JsonContainerType.Object, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
return _writer.WriteAsync('{', cancellationToken); return _writer.WriteAsync('{', cancellationToken);
} }
@@ -477,7 +477,7 @@ namespace Newtonsoft.Json
internal Task DoWriteUndefinedAsync(CancellationToken cancellationToken) internal Task DoWriteUndefinedAsync(CancellationToken cancellationToken)
{ {
Task task = InternalWriteValueAsync(JsonToken.Undefined, cancellationToken); Task task = InternalWriteValueAsync(JsonToken.Undefined, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
return _writer.WriteAsync(JsonConvert.Undefined, cancellationToken); return _writer.WriteAsync(JsonConvert.Undefined, cancellationToken);
} }
@@ -1054,7 +1054,7 @@ namespace Newtonsoft.Json
internal Task DoWriteValueAsync(string value, CancellationToken cancellationToken) internal Task DoWriteValueAsync(string value, CancellationToken cancellationToken)
{ {
Task task = InternalWriteValueAsync(JsonToken.String, cancellationToken); Task task = InternalWriteValueAsync(JsonToken.String, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
return value == null ? _writer.WriteAsync(JsonConvert.Null, cancellationToken) : WriteEscapedStringAsync(value, true, cancellationToken); return value == null ? _writer.WriteAsync(JsonConvert.Null, cancellationToken) : WriteEscapedStringAsync(value, true, cancellationToken);
} }
@@ -1189,7 +1189,7 @@ namespace Newtonsoft.Json
internal Task WriteValueNotNullAsync(Uri value, CancellationToken cancellationToken) internal Task WriteValueNotNullAsync(Uri value, CancellationToken cancellationToken)
{ {
Task task = InternalWriteValueAsync(JsonToken.String, cancellationToken); Task task = InternalWriteValueAsync(JsonToken.String, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
return WriteEscapedStringAsync(value.OriginalString, true, cancellationToken); return WriteEscapedStringAsync(value.OriginalString, true, cancellationToken);
} }
@@ -1310,7 +1310,7 @@ namespace Newtonsoft.Json
{ {
UpdateScopeWithFinishedValue(); UpdateScopeWithFinishedValue();
Task task = AutoCompleteAsync(JsonToken.Undefined, cancellationToken); Task task = AutoCompleteAsync(JsonToken.Undefined, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
return WriteRawAsync(json, cancellationToken); return WriteRawAsync(json, cancellationToken);
} }
+94 -6
View File
@@ -275,32 +275,120 @@ namespace Newtonsoft.Json
} }
} }
internal async Task InternalWriteEndAsync(JsonContainerType type, CancellationToken cancellationToken) internal Task InternalWriteEndAsync(JsonContainerType type, CancellationToken cancellationToken)
{ {
cancellationToken.ThrowIfCancellationRequested(); if (cancellationToken.IsCancellationRequested)
int levelsToComplete = CalculateLevelsToComplete(type); {
return cancellationToken.FromCanceled();
}
int levelsToComplete = CalculateLevelsToComplete(type);
Task t;
while (levelsToComplete-- > 0) while (levelsToComplete-- > 0)
{ {
JsonToken token = GetCloseTokenForType(Pop()); JsonToken token = GetCloseTokenForType(Pop());
if (_currentState == State.Property) if (_currentState == State.Property)
{ {
await WriteNullAsync(cancellationToken).ConfigureAwait(false); t = WriteNullAsync(cancellationToken);
if (!t.IsCompletedSucessfully())
{
return AwaitProperty(t, levelsToComplete, token, type, cancellationToken);
}
} }
if (_formatting == Formatting.Indented) if (_formatting == Formatting.Indented)
{ {
if (_currentState != State.ObjectStart && _currentState != State.ArrayStart) if (_currentState != State.ObjectStart && _currentState != State.ArrayStart)
{ {
await WriteIndentAsync(cancellationToken).ConfigureAwait(false); t = WriteIndentAsync(cancellationToken);
if (!t.IsCompletedSucessfully())
{
return AwaitIndent(t, levelsToComplete, token, type, cancellationToken);
}
} }
} }
await WriteEndAsync(token, cancellationToken).ConfigureAwait(false); t = WriteEndAsync(token, cancellationToken);
if (!t.IsCompletedSucessfully())
{
return AwaitEnd(t, levelsToComplete, type, cancellationToken);
}
UpdateCurrentState(); UpdateCurrentState();
} }
return AsyncUtils.CompletedTask;
// Local functions, params renamed (capitalized) so as not to capture and allocate when calling async
async Task AwaitProperty(Task task, int LevelsToComplete, JsonToken token, JsonContainerType Type, CancellationToken CancellationToken)
{
await task;
// Finish current loop
if (_formatting == Formatting.Indented)
{
if (_currentState != State.ObjectStart && _currentState != State.ArrayStart)
{
await WriteIndentAsync(CancellationToken).ConfigureAwait(false);
}
}
await WriteEndAsync(token, CancellationToken).ConfigureAwait(false);
UpdateCurrentState();
await AwaitRemaining(LevelsToComplete, Type, CancellationToken).ConfigureAwait(false);
}
async Task AwaitIndent(Task task, int LevelsToComplete, JsonToken token, JsonContainerType Type, CancellationToken CancellationToken)
{
await task;
// Finish current loop
await WriteEndAsync(token, CancellationToken).ConfigureAwait(false);
UpdateCurrentState();
await AwaitRemaining(LevelsToComplete, Type, CancellationToken).ConfigureAwait(false);
}
async Task AwaitEnd(Task task, int LevelsToComplete, JsonContainerType Type, CancellationToken CancellationToken)
{
await task;
// Finish current loop
UpdateCurrentState();
await AwaitRemaining(LevelsToComplete, Type, CancellationToken).ConfigureAwait(false);
}
async Task AwaitRemaining(int LevelsToComplete, JsonContainerType Type, CancellationToken CancellationToken)
{
while (LevelsToComplete-- > 0)
{
JsonToken token = GetCloseTokenForType(Pop());
if (_currentState == State.Property)
{
await WriteNullAsync(CancellationToken).ConfigureAwait(false);
}
if (_formatting == Formatting.Indented)
{
if (_currentState != State.ObjectStart && _currentState != State.ArrayStart)
{
await WriteIndentAsync(CancellationToken).ConfigureAwait(false);
}
}
await WriteEndAsync(token, CancellationToken).ConfigureAwait(false);
UpdateCurrentState();
}
}
} }
/// <summary> /// <summary>
+43 -4
View File
@@ -25,6 +25,7 @@
#if HAVE_ASYNC #if HAVE_ASYNC
using System;
using System.Globalization; using System.Globalization;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -41,16 +42,54 @@ namespace Newtonsoft.Json.Linq
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param> /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
/// <returns>A <see cref="Task"/> that represents the asynchronous write operation.</returns> /// <returns>A <see cref="Task"/> that represents the asynchronous write operation.</returns>
public override async Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters) public override Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
{ {
await writer.WriteStartObjectAsync(cancellationToken).ConfigureAwait(false); var t = writer.WriteStartObjectAsync(cancellationToken);
if (!t.IsCompletedSucessfully())
{
return AwaitStartObject(t, writer, cancellationToken, converters);
}
for (int i = 0; i < _properties.Count; i++) for (int i = 0; i < _properties.Count; i++)
{ {
await _properties[i].WriteToAsync(writer, cancellationToken, converters).ConfigureAwait(false); t = _properties[i].WriteToAsync(writer, cancellationToken, converters);
if (!t.IsCompletedSucessfully())
{
return AwaitProperties(t, i + 1, writer, cancellationToken, converters);
}
} }
await writer.WriteEndObjectAsync(cancellationToken).ConfigureAwait(false); t = writer.WriteEndObjectAsync(cancellationToken);
return (t.IsCompletedSucessfully()) ? AsyncUtils.CompletedTask : AwaitEndObject(t);
// Local functions, params renamed (capitalized) so as not to capture and allocate when calling async
async Task AwaitStartObject(Task task, JsonWriter Writer, CancellationToken CancellationToken, JsonConverter[] Converters)
{
await task.ConfigureAwait(false);
for (int i = 0; i < _properties.Count; i++)
{
await _properties[i].WriteToAsync(Writer, CancellationToken, Converters).ConfigureAwait(false);
}
await Writer.WriteEndObjectAsync(CancellationToken).ConfigureAwait(false);
}
async Task AwaitProperties(Task task, int i, JsonWriter Writer, CancellationToken CancellationToken, JsonConverter[] Converters)
{
await task.ConfigureAwait(false);
for (; i < _properties.Count; i++)
{
await _properties[i].WriteToAsync(Writer, CancellationToken, Converters).ConfigureAwait(false);
}
await Writer.WriteEndObjectAsync(CancellationToken).ConfigureAwait(false);
}
async Task AwaitEndObject(Task task)
{
await task.ConfigureAwait(false);
}
} }
/// <summary> /// <summary>
+1 -1
View File
@@ -44,7 +44,7 @@ namespace Newtonsoft.Json.Linq
public override Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters) public override Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
{ {
Task task = writer.WritePropertyNameAsync(_name, cancellationToken); Task task = writer.WritePropertyNameAsync(_name, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
return WriteValueAsync(writer, cancellationToken, converters); return WriteValueAsync(writer, cancellationToken, converters);
} }
@@ -92,6 +92,16 @@ namespace Newtonsoft.Json.Utilities
Debug.Assert(reader != null); Debug.Assert(reader != null);
return cancellationToken.IsCancellationRequested ? FromCanceled<int>(cancellationToken) : reader.ReadAsync(buffer, index, count); return cancellationToken.IsCancellationRequested ? FromCanceled<int>(cancellationToken) : reader.ReadAsync(buffer, index, count);
} }
public static bool IsCompletedSucessfully(this Task task)
{
// IsCompletedSucessfully is the faster method, but only currently exposed on .NET Core 2.0
#if NETCOREAPP2_0
return task.IsCompletedSucessfully;
#else
return task.Status == TaskStatus.RanToCompletion;
#endif
}
} }
} }
@@ -383,7 +383,7 @@ namespace Newtonsoft.Json.Utilities
bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer, CancellationToken cancellationToken) bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer, CancellationToken cancellationToken)
{ {
Task task = writer.WriteAsync(delimiter, cancellationToken); Task task = writer.WriteAsync(delimiter, cancellationToken);
if (task.Status != TaskStatus.RanToCompletion) if (!task.IsCompletedSucessfully())
{ {
return WriteEscapedJavaScriptStringWithDelimitersAsync(task, writer, s, delimiter, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken); return WriteEscapedJavaScriptStringWithDelimitersAsync(task, writer, s, delimiter, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken);
} }
@@ -391,7 +391,7 @@ namespace Newtonsoft.Json.Utilities
if (!string.IsNullOrEmpty(s)) if (!string.IsNullOrEmpty(s))
{ {
task = WriteEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken); task = WriteEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken);
if (task.Status == TaskStatus.RanToCompletion) if (task.IsCompletedSucessfully())
{ {
return writer.WriteAsync(delimiter, cancellationToken); return writer.WriteAsync(delimiter, cancellationToken);
} }