#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;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using LC.Newtonsoft.Json.Utilities;
namespace LC.Newtonsoft.Json
{
public abstract partial class JsonReader
{
///
/// Asynchronously reads the next JSON token from the source.
///
/// The token to monitor for cancellation requests. The default value is .
/// A that represents the asynchronous read. The
/// property returns true if the next token was read successfully; false if there are no more tokens to read.
/// The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asynchronicity.
public virtual Task ReadAsync(CancellationToken cancellationToken = default)
{
return cancellationToken.CancelIfRequestedAsync() ?? Read().ToAsync();
}
///
/// Asynchronously skips the children of the current token.
///
/// The token to monitor for cancellation requests. The default value is .
/// A that represents the asynchronous operation.
/// The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asynchronicity.
public async Task SkipAsync(CancellationToken cancellationToken = default)
{
if (TokenType == JsonToken.PropertyName)
{
await ReadAsync(cancellationToken).ConfigureAwait(false);
}
if (JsonTokenUtils.IsStartToken(TokenType))
{
int depth = Depth;
while (await ReadAsync(cancellationToken).ConfigureAwait(false) && depth < Depth)
{
}
}
}
internal async Task ReaderReadAndAssertAsync(CancellationToken cancellationToken)
{
if (!await ReadAsync(cancellationToken).ConfigureAwait(false))
{
throw CreateUnexpectedEndException();
}
}
///
/// Asynchronously reads the next JSON token from the source as a of .
///
/// The token to monitor for cancellation requests. The default value is .
/// A that represents the asynchronous read. The
/// property returns the of . This result will be null at the end of an array.
/// The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asynchronicity.
public virtual Task ReadAsBooleanAsync(CancellationToken cancellationToken = default)
{
return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsBoolean());
}
///
/// Asynchronously reads the next JSON token from the source as a [].
///
/// The token to monitor for cancellation requests. The default value is .
/// A that represents the asynchronous read. The
/// property returns the []. This result will be null at the end of an array.
/// The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asynchronicity.
public virtual Task ReadAsBytesAsync(CancellationToken cancellationToken = default)
{
return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsBytes());
}
internal async Task ReadArrayIntoByteArrayAsync(CancellationToken cancellationToken)
{
List buffer = new List();
while (true)
{
if (!await ReadAsync(cancellationToken).ConfigureAwait(false))
{
SetToken(JsonToken.None);
}
if (ReadArrayElementIntoByteArrayReportDone(buffer))
{
byte[] d = buffer.ToArray();
SetToken(JsonToken.Bytes, d, false);
return d;
}
}
}
///
/// Asynchronously reads the next JSON token from the source as a of .
///
/// The token to monitor for cancellation requests. The default value is .
/// A that represents the asynchronous read. The
/// property returns the of . This result will be null at the end of an array.
/// The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asynchronicity.
public virtual Task ReadAsDateTimeAsync(CancellationToken cancellationToken = default)
{
return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsDateTime());
}
///
/// Asynchronously reads the next JSON token from the source as a of .
///
/// The token to monitor for cancellation requests. The default value is .
/// A that represents the asynchronous read. The
/// property returns the of . This result will be null at the end of an array.
/// The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asynchronicity.
public virtual Task ReadAsDateTimeOffsetAsync(CancellationToken cancellationToken = default)
{
return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsDateTimeOffset());
}
///
/// Asynchronously reads the next JSON token from the source as a of .
///
/// The token to monitor for cancellation requests. The default value is .
/// A that represents the asynchronous read. The
/// property returns the of . This result will be null at the end of an array.
/// The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asynchronicity.
public virtual Task ReadAsDecimalAsync(CancellationToken cancellationToken = default)
{
return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsDecimal());
}
///
/// Asynchronously reads the next JSON token from the source as a of .
///
/// The token to monitor for cancellation requests. The default value is .
/// A that represents the asynchronous read. The
/// property returns the of . This result will be null at the end of an array.
/// The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asynchronicity.
public virtual Task ReadAsDoubleAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult(ReadAsDouble());
}
///
/// Asynchronously reads the next JSON token from the source as a of .
///
/// The token to monitor for cancellation requests. The default value is .
/// A that represents the asynchronous read. The
/// property returns the of . This result will be null at the end of an array.
/// The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asynchronicity.
public virtual Task ReadAsInt32Async(CancellationToken cancellationToken = default)
{
return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsInt32());
}
///
/// Asynchronously reads the next JSON token from the source as a .
///
/// The token to monitor for cancellation requests. The default value is .
/// A that represents the asynchronous read. The
/// property returns the . This result will be null at the end of an array.
/// The default behaviour is to execute synchronously, returning an already-completed task. Derived
/// classes can override this behaviour for true asynchronicity.
public virtual Task ReadAsStringAsync(CancellationToken cancellationToken = default)
{
return cancellationToken.CancelIfRequestedAsync() ?? Task.FromResult(ReadAsString());
}
internal async Task ReadAndMoveToContentAsync(CancellationToken cancellationToken)
{
return await ReadAsync(cancellationToken).ConfigureAwait(false) && await MoveToContentAsync(cancellationToken).ConfigureAwait(false);
}
internal Task MoveToContentAsync(CancellationToken cancellationToken)
{
switch (TokenType)
{
case JsonToken.None:
case JsonToken.Comment:
return MoveToContentFromNonContentAsync(cancellationToken);
default:
return AsyncUtils.True;
}
}
private async Task MoveToContentFromNonContentAsync(CancellationToken cancellationToken)
{
while (true)
{
if (!await ReadAsync(cancellationToken).ConfigureAwait(false))
{
return false;
}
switch (TokenType)
{
case JsonToken.None:
case JsonToken.Comment:
break;
default:
return true;
}
}
}
}
}
#endif