Esempio n. 1
0
        /// <summary>
        /// Asynchronously creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</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}"/> that represents the asynchronous creation. The
        /// <see cref="Task{TResult}.Result"/> property returns a <see cref="JToken"/> that contains
        /// the token and its descendant tokens
        /// that were read from the reader. The runtime type of the token is determined
        /// by the token type of the first token encountered in the reader.
        /// </returns>
        public static async Task <JToken> ReadFromAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default(CancellationToken))
        {
            ValidationUtils.ArgumentNotNull(reader, nameof(reader));

            if (reader.TokenType == JsonToken.None)
            {
                if (!await(settings != null && settings.CommentHandling == CommentHandling.Ignore ? reader.ReadAndMoveToContentAsync(cancellationToken) : reader.ReadAsync(cancellationToken)).ConfigureAwait(false))
                {
                    throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader.");
                }
            }

            IJsonLineInfo lineInfo = reader as IJsonLineInfo;

            switch (reader.TokenType)
            {
            case JsonToken.StartObject:
                return(await JObject.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false));

            case JsonToken.StartArray:
                return(await JArray.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false));

            case JsonToken.StartConstructor:
                return(await JConstructor.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false));

            case JsonToken.PropertyName:
                return(await JProperty.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false));

            case JsonToken.String:
            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.Date:
            case JsonToken.Boolean:
            case JsonToken.Bytes:
                JValue v = new JValue(reader.Value);
                v.SetLineInfo(lineInfo, settings);
                return(v);

            case JsonToken.Comment:
                v = JValue.CreateComment(reader.Value.ToString());
                v.SetLineInfo(lineInfo, settings);
                return(v);

            case JsonToken.Null:
                v = JValue.CreateNull();
                v.SetLineInfo(lineInfo, settings);
                return(v);

            case JsonToken.Undefined:
                v = JValue.CreateUndefined();
                v.SetLineInfo(lineInfo, settings);
                return(v);

            default:
                throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }
        }
Esempio n. 2
0
 public static new Task <JProperty> LoadAsync(JsonReader reader, CancellationToken cancellationToken = null)
 {
     return(JProperty.LoadAsync(reader, null, cancellationToken));
 }