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.

102 lines
5.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.Globalization;
  27. using System.Threading;
  28. using System.Threading.Tasks;
  29. using Newtonsoft.Json.Utilities;
  30. namespace Newtonsoft.Json.Linq
  31. {
  32. internal partial class JArray
  33. {
  34. /// <summary>
  35. /// Writes this token to a <see cref="JsonWriter"/> asynchronously.
  36. /// </summary>
  37. /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
  38. /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
  39. /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
  40. /// <returns>A <see cref="Task"/> that represents the asynchronous write operation.</returns>
  41. public override async Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
  42. {
  43. await writer.WriteStartArrayAsync(cancellationToken).ConfigureAwait(false);
  44. for (int i = 0; i < _values.Count; i++)
  45. {
  46. await _values[i].WriteToAsync(writer, cancellationToken, converters).ConfigureAwait(false);
  47. }
  48. await writer.WriteEndArrayAsync(cancellationToken).ConfigureAwait(false);
  49. }
  50. /// <summary>
  51. /// Asynchronously loads a <see cref="JArray"/> from a <see cref="JsonReader"/>.
  52. /// </summary>
  53. /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JArray"/>.
  54. /// If this is <c>null</c>, default load settings will be used.</param>
  55. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  56. /// <returns>A <see cref="Task{TResult}"/> representing the asynchronous load. The <see cref="Task{TResult}.Result"/> property contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
  57. public new static Task<JArray> LoadAsync(JsonReader reader, CancellationToken cancellationToken = default(CancellationToken))
  58. {
  59. return LoadAsync(reader, null, cancellationToken);
  60. }
  61. /// <summary>
  62. /// Asynchronously loads a <see cref="JArray"/> from a <see cref="JsonReader"/>.
  63. /// </summary>
  64. /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JArray"/>.</param>
  65. /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
  66. /// If this is <c>null</c>, default load settings will be used.</param>
  67. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  68. /// <returns>A <see cref="Task{TResult}"/> representing the asynchronous load. The <see cref="Task{TResult}.Result"/> property contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
  69. public new static async Task<JArray> LoadAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default(CancellationToken))
  70. {
  71. if (reader.TokenType == JsonToken.None)
  72. {
  73. if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
  74. {
  75. throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader.");
  76. }
  77. }
  78. await reader.MoveToContentAsync(cancellationToken).ConfigureAwait(false);
  79. if (reader.TokenType != JsonToken.StartArray)
  80. {
  81. throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
  82. }
  83. JArray a = new JArray();
  84. a.SetLineInfo(reader as IJsonLineInfo, settings);
  85. await a.ReadTokenFromAsync(reader, settings, cancellationToken).ConfigureAwait(false);
  86. return a;
  87. }
  88. }
  89. }
  90. #endif