Faster path for Netcoreapp2.0
This commit is contained in:
@@ -78,7 +78,7 @@ namespace Newtonsoft.Json
|
||||
return ParseObjectAsync(cancellationToken);
|
||||
case State.PostValue:
|
||||
Task<bool> task = ParsePostValueAsync(false, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
if (task.Result)
|
||||
{
|
||||
@@ -531,7 +531,7 @@ namespace Newtonsoft.Json
|
||||
_charPos++;
|
||||
|
||||
Task<bool> task = EnsureCharsAsync(1, append, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
SetNewLine(task.Result);
|
||||
return AsyncUtils.CompletedTask;
|
||||
|
||||
@@ -186,7 +186,7 @@ namespace Newtonsoft.Json
|
||||
private Task WriteValueInternalAsync(JsonToken token, string value, CancellationToken cancellationToken)
|
||||
{
|
||||
Task task = InternalWriteValueAsync(token, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
return _writer.WriteAsync(value, cancellationToken);
|
||||
}
|
||||
@@ -266,7 +266,7 @@ namespace Newtonsoft.Json
|
||||
private Task WriteIntegerValueAsync(ulong uvalue, bool negative, CancellationToken cancellationToken)
|
||||
{
|
||||
Task task = InternalWriteValueAsync(JsonToken.Integer, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
return WriteDigitsAsync(uvalue, negative, cancellationToken);
|
||||
}
|
||||
@@ -317,13 +317,13 @@ namespace Newtonsoft.Json
|
||||
internal Task DoWritePropertyNameAsync(string name, CancellationToken cancellationToken)
|
||||
{
|
||||
Task task = InternalWritePropertyNameAsync(name, cancellationToken);
|
||||
if (task.Status != TaskStatus.RanToCompletion)
|
||||
if (!task.IsCompletedSucessfully())
|
||||
{
|
||||
return DoWritePropertyNameAsync(task, name, cancellationToken);
|
||||
}
|
||||
|
||||
task = WriteEscapedStringAsync(name, _quoteName, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
return _writer.WriteAsync(':', cancellationToken);
|
||||
}
|
||||
@@ -395,7 +395,7 @@ namespace Newtonsoft.Json
|
||||
internal Task DoWriteStartArrayAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Task task = InternalWriteStartAsync(JsonToken.StartArray, JsonContainerType.Array, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
return _writer.WriteAsync('[', cancellationToken);
|
||||
}
|
||||
@@ -425,7 +425,7 @@ namespace Newtonsoft.Json
|
||||
internal Task DoWriteStartObjectAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Task task = InternalWriteStartAsync(JsonToken.StartObject, JsonContainerType.Object, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
return _writer.WriteAsync('{', cancellationToken);
|
||||
}
|
||||
@@ -477,7 +477,7 @@ namespace Newtonsoft.Json
|
||||
internal Task DoWriteUndefinedAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Task task = InternalWriteValueAsync(JsonToken.Undefined, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
return _writer.WriteAsync(JsonConvert.Undefined, cancellationToken);
|
||||
}
|
||||
@@ -1054,7 +1054,7 @@ namespace Newtonsoft.Json
|
||||
internal Task DoWriteValueAsync(string value, CancellationToken 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);
|
||||
}
|
||||
@@ -1189,7 +1189,7 @@ namespace Newtonsoft.Json
|
||||
internal Task WriteValueNotNullAsync(Uri value, CancellationToken cancellationToken)
|
||||
{
|
||||
Task task = InternalWriteValueAsync(JsonToken.String, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
return WriteEscapedStringAsync(value.OriginalString, true, cancellationToken);
|
||||
}
|
||||
@@ -1310,7 +1310,7 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
UpdateScopeWithFinishedValue();
|
||||
Task task = AutoCompleteAsync(JsonToken.Undefined, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
return WriteRawAsync(json, cancellationToken);
|
||||
}
|
||||
|
||||
@@ -291,7 +291,7 @@ namespace Newtonsoft.Json
|
||||
if (_currentState == State.Property)
|
||||
{
|
||||
t = WriteNullAsync(cancellationToken);
|
||||
if (t.Status != TaskStatus.RanToCompletion)
|
||||
if (!t.IsCompletedSucessfully())
|
||||
{
|
||||
return AwaitProperty(t, levelsToComplete, token, type, cancellationToken);
|
||||
}
|
||||
@@ -302,7 +302,7 @@ namespace Newtonsoft.Json
|
||||
if (_currentState != State.ObjectStart && _currentState != State.ArrayStart)
|
||||
{
|
||||
t = WriteIndentAsync(cancellationToken);
|
||||
if (t.Status != TaskStatus.RanToCompletion)
|
||||
if (!t.IsCompletedSucessfully())
|
||||
{
|
||||
return AwaitIndent(t, levelsToComplete, token, type, cancellationToken);
|
||||
}
|
||||
@@ -310,7 +310,7 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
|
||||
t = WriteEndAsync(token, cancellationToken);
|
||||
if (t.Status != TaskStatus.RanToCompletion)
|
||||
if (!t.IsCompletedSucessfully())
|
||||
{
|
||||
return AwaitEnd(t, levelsToComplete, type, cancellationToken);
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace Newtonsoft.Json.Linq
|
||||
public override Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
|
||||
{
|
||||
var t = writer.WriteStartObjectAsync(cancellationToken);
|
||||
if (t.Status != TaskStatus.RanToCompletion)
|
||||
if (!t.IsCompletedSucessfully())
|
||||
{
|
||||
return AwaitStartObject(t, writer, cancellationToken, converters);
|
||||
}
|
||||
@@ -53,7 +53,7 @@ namespace Newtonsoft.Json.Linq
|
||||
for (int i = 0; i < _properties.Count; i++)
|
||||
{
|
||||
t = _properties[i].WriteToAsync(writer, cancellationToken, converters);
|
||||
if (t.Status != TaskStatus.RanToCompletion)
|
||||
if (!t.IsCompletedSucessfully())
|
||||
{
|
||||
return AwaitProperties(t, i + 1, writer, cancellationToken, converters);
|
||||
}
|
||||
@@ -61,7 +61,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
t = writer.WriteEndObjectAsync(cancellationToken);
|
||||
|
||||
return (t.Status == TaskStatus.RanToCompletion) ? AsyncUtils.CompletedTask : AwaitEndObject(t);
|
||||
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)
|
||||
|
||||
@@ -44,7 +44,7 @@ namespace Newtonsoft.Json.Linq
|
||||
public override Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
|
||||
{
|
||||
Task task = writer.WritePropertyNameAsync(_name, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
return WriteValueAsync(writer, cancellationToken, converters);
|
||||
}
|
||||
|
||||
@@ -92,6 +92,16 @@ namespace Newtonsoft.Json.Utilities
|
||||
Debug.Assert(reader != null);
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -381,7 +381,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
bool[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, JsonTextWriter client, char[] writeBuffer, CancellationToken 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);
|
||||
}
|
||||
@@ -389,7 +389,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
if (!string.IsNullOrEmpty(s))
|
||||
{
|
||||
task = WriteEscapedJavaScriptStringWithoutDelimitersAsync(writer, s, charEscapeFlags, stringEscapeHandling, client, writeBuffer, cancellationToken);
|
||||
if (task.Status == TaskStatus.RanToCompletion)
|
||||
if (task.IsCompletedSucessfully())
|
||||
{
|
||||
return writer.WriteAsync(delimiter, cancellationToken);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user