/// <summary> /// Asynchronously loads a <see cref="JArray"/> from a <see cref="JsonReader"/>. /// </summary> /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JArray"/>.</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 load. The <see cref="Task{TResult}.Result"/> property contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns> public new static async Task <JArray> 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 JArray from JsonReader."); } } await reader.MoveToContentAsync(cancellationToken).ConfigureAwait(false); if (reader.TokenType != JsonToken.StartArray) { throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } JArray a = new JArray(); a.SetLineInfo(reader as IJsonLineInfo, settings); await a.ReadTokenFromAsync(reader, settings, cancellationToken).ConfigureAwait(false); return(a); }
/// <summary> /// Load a <see cref="JArray"/> from a string that contains JSON. /// </summary> /// <param name="json">A <see cref="String"/> that contains JSON.</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> /// <returns>A <see cref="JArray"/> populated from the string that contains JSON.</returns> /// <example> /// <code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParseArray" title="Parsing a JSON Array from Text" /> /// </example> public new static JArray Parse(string json, JsonLoadSettings settings) { using (JsonReader reader = new JsonTextReader(new StringReader(json))) { JArray a = Load(reader, settings); while (reader.Read()) { // Any content encountered here other than a comment will throw in the reader. } return(a); } }
internal async Task ReadTokenFromAsync(JsonReader reader, JsonLoadSettings options, CancellationToken cancellationToken = default) { ValidationUtils.ArgumentNotNull(reader, nameof(reader)); int startDepth = reader.Depth; if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) { throw JsonReaderException.Create(reader, "Error reading {0} from JsonReader.".FormatWith(CultureInfo.InvariantCulture, GetType().Name)); } await ReadContentFromAsync(reader, options, cancellationToken).ConfigureAwait(false); if (reader.Depth > startDepth) { throw JsonReaderException.Create(reader, "Unexpected end of content while loading {0}.".FormatWith(CultureInfo.InvariantCulture, GetType().Name)); } }
private async Task ReadContentFromAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default) { IJsonLineInfo lineInfo = reader as IJsonLineInfo; JContainer parent = this; do { if (parent is JProperty p && p.Value != null) { if (parent == this) { return; } parent = parent.Parent; } switch (reader.TokenType) { case JsonToken.None: // new reader. move to actual content break; case JsonToken.StartArray: JArray a = new JArray(); a.SetLineInfo(lineInfo, settings); parent.Add(a); parent = a; break; case JsonToken.EndArray: if (parent == this) { return; } parent = parent.Parent; break; case JsonToken.StartObject: JObject o = new JObject(); o.SetLineInfo(lineInfo, settings); parent.Add(o); parent = o; break; case JsonToken.EndObject: if (parent == this) { return; } parent = parent.Parent; break; case JsonToken.StartConstructor: JConstructor constructor = new JConstructor(reader.Value.ToString()); constructor.SetLineInfo(lineInfo, settings); parent.Add(constructor); parent = constructor; break; case JsonToken.EndConstructor: if (parent == this) { return; } parent = parent.Parent; break; 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); parent.Add(v); break; case JsonToken.Comment: if (settings != null && settings.CommentHandling == CommentHandling.Load) { v = JValue.CreateComment(reader.Value.ToString()); v.SetLineInfo(lineInfo, settings); parent.Add(v); } break; case JsonToken.Null: v = JValue.CreateNull(); v.SetLineInfo(lineInfo, settings); parent.Add(v); break; case JsonToken.Undefined: v = JValue.CreateUndefined(); v.SetLineInfo(lineInfo, settings); parent.Add(v); break; case JsonToken.PropertyName: JProperty property = ReadProperty(reader, settings, lineInfo, parent); if (property != null) { parent = property; } else { await reader.SkipAsync().ConfigureAwait(false); } break; default: throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } } while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)); }