You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
6.2 KiB

  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. //
  4. // Permission is hereby granted, free of charge, to any person
  5. // obtaining a copy of this software and associated documentation
  6. // files (the "Software"), to deal in the Software without
  7. // restriction, including without limitation the rights to use,
  8. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following
  11. // conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  18. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  20. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. // OTHER DEALINGS IN THE SOFTWARE.
  24. #endregion
  25. #if HAVE_ASYNC
  26. using System;
  27. using System.Globalization;
  28. using System.Threading;
  29. using System.Threading.Tasks;
  30. using Newtonsoft.Json.Utilities;
  31. namespace Newtonsoft.Json.Linq
  32. {
  33. internal partial class JObject
  34. {
  35. /// <summary>
  36. /// Writes this token to a <see cref="JsonWriter"/> asynchronously.
  37. /// </summary>
  38. /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
  39. /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
  40. /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
  41. /// <returns>A <see cref="Task"/> that represents the asynchronous write operation.</returns>
  42. public override Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
  43. {
  44. Task t = writer.WriteStartObjectAsync(cancellationToken);
  45. if (!t.IsCompletedSucessfully())
  46. {
  47. return AwaitProperties(t, 0, writer, cancellationToken, converters);
  48. }
  49. for (int i = 0; i < _properties.Count; i++)
  50. {
  51. t = _properties[i].WriteToAsync(writer, cancellationToken, converters);
  52. if (!t.IsCompletedSucessfully())
  53. {
  54. return AwaitProperties(t, i + 1, writer, cancellationToken, converters);
  55. }
  56. }
  57. return writer.WriteEndObjectAsync(cancellationToken);
  58. // Local functions, params renamed (capitalized) so as not to capture and allocate when calling async
  59. async Task AwaitProperties(Task task, int i, JsonWriter Writer, CancellationToken CancellationToken, JsonConverter[] Converters)
  60. {
  61. await task.ConfigureAwait(false);
  62. for (; i < _properties.Count; i++)
  63. {
  64. await _properties[i].WriteToAsync(Writer, CancellationToken, Converters).ConfigureAwait(false);
  65. }
  66. await Writer.WriteEndObjectAsync(CancellationToken).ConfigureAwait(false);
  67. }
  68. }
  69. /// <summary>
  70. /// Asynchronously loads a <see cref="JObject"/> from a <see cref="JsonReader"/>.
  71. /// </summary>
  72. /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JObject"/>.</param>
  73. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  74. /// <returns>
  75. /// A <see cref="Task{TResult}"/> that represents the asynchronous load. The <see cref="Task{TResult}.Result"/>
  76. /// property returns a <see cref="JObject"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
  77. public new static Task<JObject> LoadAsync(JsonReader reader, CancellationToken cancellationToken = default(CancellationToken))
  78. {
  79. return LoadAsync(reader, null, cancellationToken);
  80. }
  81. /// <summary>
  82. /// Asynchronously loads a <see cref="JObject"/> from a <see cref="JsonReader"/>.
  83. /// </summary>
  84. /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JObject"/>.</param>
  85. /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
  86. /// If this is <c>null</c>, default load settings will be used.</param>
  87. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  88. /// <returns>
  89. /// A <see cref="Task{TResult}"/> that represents the asynchronous load. The <see cref="Task{TResult}.Result"/>
  90. /// property returns a <see cref="JObject"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
  91. public new static async Task<JObject> LoadAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default(CancellationToken))
  92. {
  93. ValidationUtils.ArgumentNotNull(reader, nameof(reader));
  94. if (reader.TokenType == JsonToken.None)
  95. {
  96. if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
  97. {
  98. throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader.");
  99. }
  100. }
  101. await reader.MoveToContentAsync(cancellationToken).ConfigureAwait(false);
  102. if (reader.TokenType != JsonToken.StartObject)
  103. {
  104. throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
  105. }
  106. JObject o = new JObject();
  107. o.SetLineInfo(reader as IJsonLineInfo, settings);
  108. await o.ReadTokenFromAsync(reader, settings, cancellationToken).ConfigureAwait(false);
  109. return o;
  110. }
  111. }
  112. }
  113. #endif