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.

106 lines
5.4 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 JConstructor
  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.WriteStartConstructorAsync(_name, 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.WriteEndConstructorAsync(cancellationToken).ConfigureAwait(false);
  49. }
  50. /// <summary>
  51. /// Asynchronously loads a <see cref="JConstructor"/> 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="JConstructor"/>.</param>
  54. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  55. /// <returns>
  56. /// A <see cref="Task{TResult}"/> that represents the asynchronous load. The <see cref="Task{TResult}.Result"/>
  57. /// property returns a <see cref="JConstructor"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
  58. public new static Task<JConstructor> LoadAsync(JsonReader reader, CancellationToken cancellationToken = default(CancellationToken))
  59. {
  60. return LoadAsync(reader, null, cancellationToken);
  61. }
  62. /// <summary>
  63. /// Asynchronously loads a <see cref="JConstructor"/> from a <see cref="JsonReader"/>.
  64. /// </summary>
  65. /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JConstructor"/>.</param>
  66. /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
  67. /// If this is <c>null</c>, default load settings will be used.</param>
  68. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  69. /// <returns>
  70. /// A <see cref="Task{TResult}"/> that represents the asynchronous load. The <see cref="Task{TResult}.Result"/>
  71. /// property returns a <see cref="JConstructor"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
  72. public new static async Task<JConstructor> LoadAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default(CancellationToken))
  73. {
  74. if (reader.TokenType == JsonToken.None)
  75. {
  76. if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
  77. {
  78. throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader.");
  79. }
  80. }
  81. await reader.MoveToContentAsync(cancellationToken).ConfigureAwait(false);
  82. if (reader.TokenType != JsonToken.StartConstructor)
  83. {
  84. throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
  85. }
  86. JConstructor c = new JConstructor((string)reader.Value);
  87. c.SetLineInfo(reader as IJsonLineInfo, settings);
  88. await c.ReadTokenFromAsync(reader, settings, cancellationToken).ConfigureAwait(false);
  89. return c;
  90. }
  91. }
  92. }
  93. #endif