#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if HAVE_ASYNC
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using LC.Newtonsoft.Json.Utilities;
namespace LC.Newtonsoft.Json.Linq
{
public partial class JConstructor
{
///
/// Writes this token to a asynchronously.
///
/// A into which this method will write.
/// The token to monitor for cancellation requests.
/// A collection of which will be used when writing the token.
/// A that represents the asynchronous write operation.
public override async Task WriteToAsync(JsonWriter writer, CancellationToken cancellationToken, params JsonConverter[] converters)
{
await writer.WriteStartConstructorAsync(_name ?? string.Empty, cancellationToken).ConfigureAwait(false);
for (int i = 0; i < _values.Count; i++)
{
await _values[i].WriteToAsync(writer, cancellationToken, converters).ConfigureAwait(false);
}
await writer.WriteEndConstructorAsync(cancellationToken).ConfigureAwait(false);
}
///
/// Asynchronously loads a from a .
///
/// A that will be read for the content of the .
/// The token to monitor for cancellation requests. The default value is .
///
/// A that represents the asynchronous load. The
/// property returns a that contains the JSON that was read from the specified .
public new static Task LoadAsync(JsonReader reader, CancellationToken cancellationToken = default)
{
return LoadAsync(reader, null, cancellationToken);
}
///
/// Asynchronously loads a from a .
///
/// A that will be read for the content of the .
/// The used to load the JSON.
/// If this is null, default load settings will be used.
/// The token to monitor for cancellation requests. The default value is .
///
/// A that represents the asynchronous load. The
/// property returns a that contains the JSON that was read from the specified .
public new static async Task LoadAsync(JsonReader reader, JsonLoadSettings? settings, CancellationToken cancellationToken = default)
{
if (reader.TokenType == JsonToken.None)
{
if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
{
throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader.");
}
}
await reader.MoveToContentAsync(cancellationToken).ConfigureAwait(false);
if (reader.TokenType != JsonToken.StartConstructor)
{
throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
}
JConstructor c = new JConstructor((string)reader.Value!);
c.SetLineInfo(reader as IJsonLineInfo, settings);
await c.ReadTokenFromAsync(reader, settings, cancellationToken).ConfigureAwait(false);
return c;
}
}
}
#endif