Esempio n. 1
0
        /// <summary>
        /// Asynchronously loads a <see cref="JProperty"/> from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JProperty"/>.</param>
        /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
        /// If this is <c>null</c>, default load settings will be used.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns>A <see cref="Task{TResult}"/> representing the asynchronous creation. The <see cref="Task{TResult}.Result"/>
        /// property returns a <see cref="JProperty"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
        public new static async Task <JProperty> LoadAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
                {
                    throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader.");
                }
            }

            await reader.MoveToContentAsync(cancellationToken).ConfigureAwait(false);

            if (reader.TokenType != JsonToken.PropertyName)
            {
                throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader. Current JsonReader item is not a property: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }

            JProperty p = new JProperty((string)reader.Value);

            p.SetLineInfo(reader as IJsonLineInfo, settings);

            await p.ReadTokenFromAsync(reader, settings, cancellationToken).ConfigureAwait(false);

            return(p);
        }
Esempio n. 2
0
        public static new async Task <JProperty> LoadAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = null)
        {
            ConfiguredTaskAwaitable <bool> configuredTaskAwaitable;

            if (reader.TokenType == JsonToken.None)
            {
                configuredTaskAwaitable = reader.ReadAsync(cancellationToken).ConfigureAwait(false);
                if (!await configuredTaskAwaitable)
                {
                    throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader.");
                }
            }
            configuredTaskAwaitable = reader.MoveToContentAsync(cancellationToken).ConfigureAwait(false);
            await configuredTaskAwaitable;

            if (reader.TokenType != JsonToken.PropertyName)
            {
                throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader. Current JsonReader item is not a property: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }
            JProperty jProperty = new JProperty((string)reader.Value);

            jProperty.SetLineInfo(reader as IJsonLineInfo, settings);
            ConfiguredTaskAwaitable configuredTaskAwaitable1 = jProperty.ReadTokenFromAsync(reader, settings, cancellationToken).ConfigureAwait(false);
            await configuredTaskAwaitable1;

            return(jProperty);
        }