From fe2ec84c18b74e242765193e1c4a9f1a6fc6bd31 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 3 Sep 2017 10:43:14 +1200 Subject: [PATCH] -Async cleanup --- Src/Newtonsoft.Json/JsonTextReader.Async.cs | 8 ++++---- Src/Newtonsoft.Json/JsonWriter.Async.cs | 6 +++--- Src/Newtonsoft.Json/Linq/JObject.Async.cs | 22 ++------------------- 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/Src/Newtonsoft.Json/JsonTextReader.Async.cs b/Src/Newtonsoft.Json/JsonTextReader.Async.cs index e2c67e9e..ae183c33 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.Async.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.Async.cs @@ -1138,7 +1138,7 @@ namespace Newtonsoft.Json switch (_currentState) { case State.PostValue: - if (await ParsePostValueAsync(true, cancellationToken)) + if (await ParsePostValueAsync(true, cancellationToken).ConfigureAwait(false)) { return null; } @@ -1273,7 +1273,7 @@ namespace Newtonsoft.Json switch (_currentState) { case State.PostValue: - if (await ParsePostValueAsync(true, cancellationToken)) + if (await ParsePostValueAsync(true, cancellationToken).ConfigureAwait(false)) { return null; } @@ -1399,7 +1399,7 @@ namespace Newtonsoft.Json switch (_currentState) { case State.PostValue: - if (await ParsePostValueAsync(true, cancellationToken)) + if (await ParsePostValueAsync(true, cancellationToken).ConfigureAwait(false)) { return null; } @@ -1535,7 +1535,7 @@ namespace Newtonsoft.Json switch (_currentState) { case State.PostValue: - if (await ParsePostValueAsync(true, cancellationToken)) + if (await ParsePostValueAsync(true, cancellationToken).ConfigureAwait(false)) { return null; } diff --git a/Src/Newtonsoft.Json/JsonWriter.Async.cs b/Src/Newtonsoft.Json/JsonWriter.Async.cs index 2f93e68a..586285b5 100644 --- a/Src/Newtonsoft.Json/JsonWriter.Async.cs +++ b/Src/Newtonsoft.Json/JsonWriter.Async.cs @@ -323,7 +323,7 @@ namespace Newtonsoft.Json // 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; + await task.ConfigureAwait(false); // Finish current loop if (_formatting == Formatting.Indented) @@ -343,7 +343,7 @@ namespace Newtonsoft.Json async Task AwaitIndent(Task task, int LevelsToComplete, JsonToken token, JsonContainerType Type, CancellationToken CancellationToken) { - await task; + await task.ConfigureAwait(false); // Finish current loop @@ -356,7 +356,7 @@ namespace Newtonsoft.Json async Task AwaitEnd(Task task, int LevelsToComplete, JsonContainerType Type, CancellationToken CancellationToken) { - await task; + await task.ConfigureAwait(false); // Finish current loop diff --git a/Src/Newtonsoft.Json/Linq/JObject.Async.cs b/Src/Newtonsoft.Json/Linq/JObject.Async.cs index 39f6493a..878b07ee 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.Async.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.Async.cs @@ -47,7 +47,7 @@ namespace Newtonsoft.Json.Linq var t = writer.WriteStartObjectAsync(cancellationToken); if (!t.IsCompletedSucessfully()) { - return AwaitStartObject(t, writer, cancellationToken, converters); + return AwaitProperties(t, 0, writer, cancellationToken, converters); } for (int i = 0; i < _properties.Count; i++) @@ -59,22 +59,9 @@ namespace Newtonsoft.Json.Linq } } - t = writer.WriteEndObjectAsync(cancellationToken); - - return (t.IsCompletedSucessfully()) ? AsyncUtils.CompletedTask : AwaitEndObject(t); + return writer.WriteEndObjectAsync(cancellationToken); // 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); @@ -85,11 +72,6 @@ namespace Newtonsoft.Json.Linq await Writer.WriteEndObjectAsync(CancellationToken).ConfigureAwait(false); } - - async Task AwaitEndObject(Task task) - { - await task.ConfigureAwait(false); - } } ///