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.

118 lines
5.9 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 JProperty
  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 Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
  42. {
  43. Task task = writer.WritePropertyNameAsync(_name, cancellationToken);
  44. if (task.IsCompletedSucessfully())
  45. {
  46. return WriteValueAsync(writer, cancellationToken, converters);
  47. }
  48. return WriteToAsync(task, writer, cancellationToken, converters);
  49. }
  50. private async Task WriteToAsync(Task task, JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
  51. {
  52. await task.ConfigureAwait(false);
  53. await WriteValueAsync(writer, cancellationToken, converters).ConfigureAwait(false);
  54. }
  55. private Task WriteValueAsync(JsonWriter writer, CancellationToken cancellationToken, JsonConverter[] converters)
  56. {
  57. JToken value = Value;
  58. return value != null
  59. ? value.WriteToAsync(writer, cancellationToken, converters)
  60. : writer.WriteNullAsync(cancellationToken);
  61. }
  62. /// <summary>
  63. /// Asynchronously loads a <see cref="JProperty"/> 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="JProperty"/>.</param>
  66. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  67. /// <returns>A <see cref="Task{TResult}"/> representing the asynchronous creation. The <see cref="Task{TResult}.Result"/>
  68. /// property returns a <see cref="JProperty"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
  69. public new static Task<JProperty> LoadAsync(JsonReader reader, CancellationToken cancellationToken = default(CancellationToken))
  70. {
  71. return LoadAsync(reader, null, cancellationToken);
  72. }
  73. /// <summary>
  74. /// Asynchronously loads a <see cref="JProperty"/> from a <see cref="JsonReader"/>.
  75. /// </summary>
  76. /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JProperty"/>.</param>
  77. /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
  78. /// If this is <c>null</c>, default load settings will be used.</param>
  79. /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
  80. /// <returns>A <see cref="Task{TResult}"/> representing the asynchronous creation. The <see cref="Task{TResult}.Result"/>
  81. /// property returns a <see cref="JProperty"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
  82. public new static async Task<JProperty> LoadAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default(CancellationToken))
  83. {
  84. if (reader.TokenType == JsonToken.None)
  85. {
  86. if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
  87. {
  88. throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader.");
  89. }
  90. }
  91. await reader.MoveToContentAsync(cancellationToken).ConfigureAwait(false);
  92. if (reader.TokenType != JsonToken.PropertyName)
  93. {
  94. throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader. Current JsonReader item is not a property: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
  95. }
  96. JProperty p = new JProperty((string)reader.Value);
  97. p.SetLineInfo(reader as IJsonLineInfo, settings);
  98. await p.ReadTokenFromAsync(reader, settings, cancellationToken).ConfigureAwait(false);
  99. return p;
  100. }
  101. }
  102. }
  103. #endif