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.

177 lines
9.7 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 abstract partial class JToken
  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 virtual Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
  43. {
  44. throw new NotImplementedException();
  45. }
  46. /// <summary>
  47. /// Writes this token to a <see cref="JsonWriter"/> asynchronously.
  48. /// </summary>
  49. /// <param name="writer">A <see cref="JsonWriter"/> into which this method will write.</param>
  50. /// <param name="converters">A collection of <see cref="JsonConverter"/> which will be used when writing the token.</param>
  51. /// <returns>A <see cref="Task"/> that represents the asynchronous write operation.</returns>
  52. public Task WriteToAsync(JsonWriter writer, params JsonConverter[] converters)
  53. {
  54. return WriteToAsync(writer, default(CancellationToken), converters);
  55. }
  56. /// <summary>
  57. /// Asynchronously creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
  58. /// </summary>
  59. /// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
  60. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  61. /// <returns>
  62. /// A <see cref="Task{TResult}"/> that represents the asynchronous creation. The
  63. /// <see cref="Task{TResult}.Result"/> property returns a <see cref="JToken"/> that contains
  64. /// the token and its descendant tokens
  65. /// that were read from the reader. The runtime type of the token is determined
  66. /// by the token type of the first token encountered in the reader.
  67. /// </returns>
  68. public static Task<JToken> ReadFromAsync(JsonReader reader, CancellationToken cancellationToken = default(CancellationToken))
  69. {
  70. return ReadFromAsync(reader, null, cancellationToken);
  71. }
  72. /// <summary>
  73. /// Asynchronously creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
  74. /// </summary>
  75. /// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
  76. /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
  77. /// If this is <c>null</c>, default load settings will be used.</param>
  78. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  79. /// <returns>
  80. /// A <see cref="Task{TResult}"/> that represents the asynchronous creation. The
  81. /// <see cref="Task{TResult}.Result"/> property returns a <see cref="JToken"/> that contains
  82. /// the token and its descendant tokens
  83. /// that were read from the reader. The runtime type of the token is determined
  84. /// by the token type of the first token encountered in the reader.
  85. /// </returns>
  86. public static async Task<JToken> ReadFromAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default(CancellationToken))
  87. {
  88. ValidationUtils.ArgumentNotNull(reader, nameof(reader));
  89. if (reader.TokenType == JsonToken.None)
  90. {
  91. if (!await (settings != null && settings.CommentHandling == CommentHandling.Ignore ? reader.ReadAndMoveToContentAsync(cancellationToken) : reader.ReadAsync(cancellationToken)).ConfigureAwait(false))
  92. {
  93. throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader.");
  94. }
  95. }
  96. IJsonLineInfo lineInfo = reader as IJsonLineInfo;
  97. switch (reader.TokenType)
  98. {
  99. case JsonToken.StartObject:
  100. return await JObject.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false);
  101. case JsonToken.StartArray:
  102. return await JArray.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false);
  103. case JsonToken.StartConstructor:
  104. return await JConstructor.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false);
  105. case JsonToken.PropertyName:
  106. return await JProperty.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false);
  107. case JsonToken.String:
  108. case JsonToken.Integer:
  109. case JsonToken.Float:
  110. case JsonToken.Date:
  111. case JsonToken.Boolean:
  112. case JsonToken.Bytes:
  113. JValue v = new JValue(reader.Value);
  114. v.SetLineInfo(lineInfo, settings);
  115. return v;
  116. case JsonToken.Comment:
  117. v = JValue.CreateComment(reader.Value.ToString());
  118. v.SetLineInfo(lineInfo, settings);
  119. return v;
  120. case JsonToken.Null:
  121. v = JValue.CreateNull();
  122. v.SetLineInfo(lineInfo, settings);
  123. return v;
  124. case JsonToken.Undefined:
  125. v = JValue.CreateUndefined();
  126. v.SetLineInfo(lineInfo, settings);
  127. return v;
  128. default:
  129. throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
  130. }
  131. }
  132. /// <summary>
  133. /// Asynchronously creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
  134. /// </summary>
  135. /// <param name="reader">A <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
  136. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  137. /// <returns>
  138. /// A <see cref="Task{TResult}"/> that represents the asynchronous creation. The <see cref="Task{TResult}.Result"/>
  139. /// property returns a <see cref="JToken"/> that contains the token and its descendant tokens
  140. /// that were read from the reader. The runtime type of the token is determined
  141. /// by the token type of the first token encountered in the reader.
  142. /// </returns>
  143. public static Task<JToken> LoadAsync(JsonReader reader, CancellationToken cancellationToken = default(CancellationToken))
  144. {
  145. return LoadAsync(reader, null, cancellationToken);
  146. }
  147. /// <summary>
  148. /// Asynchronously creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
  149. /// </summary>
  150. /// <param name="reader">A <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
  151. /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
  152. /// If this is <c>null</c>, default load settings will be used.</param>
  153. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  154. /// <returns>
  155. /// A <see cref="Task{TResult}"/> that represents the asynchronous creation. The <see cref="Task{TResult}.Result"/>
  156. /// property returns a <see cref="JToken"/> that contains the token and its descendant tokens
  157. /// that were read from the reader. The runtime type of the token is determined
  158. /// by the token type of the first token encountered in the reader.
  159. /// </returns>
  160. public static Task<JToken> LoadAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default(CancellationToken))
  161. {
  162. return ReadFromAsync(reader, settings, cancellationToken);
  163. }
  164. }
  165. }
  166. #endif