/// <summary> /// Asynchronously reads the next node. Use this instead of the direct call to Read since this asserts that there actually is a next node. /// </summary> /// <param name="jsonReader">The <see cref="JsonReader"/> to read from.</param> /// <returns>A task that represents the asynchronous read operation. /// The value of the TResult parameter contains the node type of the node that reader is positioned on after reading.</returns> internal static async Task <JsonNodeType> ReadNextAsync(this IJsonReaderAsync jsonReader) { Debug.Assert(jsonReader != null, "jsonReader != null"); #if DEBUG bool result = await jsonReader.ReadAsync() .ConfigureAwait(false); Debug.Assert(result, "JsonReader.Read returned false in an unexpected place."); #else await jsonReader.ReadAsync(); #endif return(jsonReader.NodeType); }
/// <summary> /// Asynchronously skips over a JSON value (primitive, object or array). /// </summary> /// <param name="jsonReader">The <see cref="JsonReader"/> to read from.</param> /// <remarks> /// Pre-Condition: JsonNodeType.PrimitiveValue, JsonNodeType.StartArray or JsonNodeType.StartObject /// Post-Condition: JsonNodeType.PrimitiveValue, JsonNodeType.EndArray or JsonNodeType.EndObject /// </remarks> /// <returns>A task that represents the asynchronous read operation.</returns> internal static async Task SkipValueAsync(this IJsonReaderAsync jsonReader) { Debug.Assert(jsonReader != null, "jsonReader != null"); int depth = 0; do { switch (jsonReader.NodeType) { case JsonNodeType.StartArray: case JsonNodeType.StartObject: depth++; break; case JsonNodeType.EndArray: case JsonNodeType.EndObject: Debug.Assert(depth > 0, "Seen too many scope ends."); depth--; break; default: Debug.Assert( jsonReader.NodeType != JsonNodeType.EndOfInput, "We should not have reached end of input, since the scopes should be well formed. Otherwise JsonReader should have failed by now."); break; } }while (await jsonReader.ReadAsync().ConfigureAwait(false) && depth > 0); if (depth > 0) { // Not all open scopes were closed: // "Invalid JSON. Unexpected end of input was found in JSON content. Not all object and array scopes were closed." throw CreateException(Strings.JsonReader_EndOfInputWithOpenScope); } }
/// <summary> /// Asynchronously reads the next node from the <paramref name="jsonReader"/> and verifies that it is of the expected node type. /// </summary> /// <param name="jsonReader">The <see cref="JsonReader"/> to read from.</param> /// <param name="expectedNodeType">The expected <see cref="JsonNodeType"/> of the read node.</param> private static async Task ReadNextAsync(this IJsonReaderAsync jsonReader, JsonNodeType expectedNodeType) { Debug.Assert(jsonReader != null, "jsonReader != null"); Debug.Assert(expectedNodeType != JsonNodeType.None, "expectedNodeType != JsonNodeType.None"); jsonReader.ValidateNodeType(expectedNodeType); await jsonReader.ReadAsync().ConfigureAwait(false); }
/// <summary> /// Asynchronously skips over a JSON value (primitive, object or array), and append raw string to StringBuilder. /// </summary> /// <param name="jsonReader">The <see cref="JsonReader"/> to read from.</param> /// <param name="jsonRawValueStringBuilder">The StringBuilder to receive JSON raw string.</param> /// <returns>A task that represents the asynchronous read operation.</returns> internal static async Task SkipValueAsync(this IJsonReaderAsync jsonReader, StringBuilder jsonRawValueStringBuilder) { Debug.Assert(jsonReader != null, "jsonReader != null"); using (StringWriter stringWriter = new StringWriter(jsonRawValueStringBuilder, CultureInfo.InvariantCulture)) { JsonWriter jsonWriter = new JsonWriter(stringWriter, jsonReader.IsIeee754Compatible); int depth = 0; do { switch (jsonReader.NodeType) { case JsonNodeType.PrimitiveValue: if (await jsonReader.GetValueAsync().ConfigureAwait(false) == null) { await jsonWriter.WriteValueAsync((string)null) .ConfigureAwait(false); } else { object primitiveValue = await jsonReader.GetValueAsync() .ConfigureAwait(false); await jsonWriter.WritePrimitiveValueAsync(primitiveValue) .ConfigureAwait(false); } break; case JsonNodeType.StartArray: await jsonWriter.StartArrayScopeAsync() .ConfigureAwait(false); depth++; break; case JsonNodeType.StartObject: await jsonWriter.StartObjectScopeAsync() .ConfigureAwait(false); depth++; break; case JsonNodeType.EndArray: await jsonWriter.EndArrayScopeAsync() .ConfigureAwait(false); Debug.Assert(depth > 0, "Seen too many scope ends."); depth--; break; case JsonNodeType.EndObject: await jsonWriter.EndObjectScopeAsync() .ConfigureAwait(false); Debug.Assert(depth > 0, "Seen too many scope ends."); depth--; break; case JsonNodeType.Property: string propertyName = await jsonReader.GetPropertyNameAsync() .ConfigureAwait(false); await jsonWriter.WriteNameAsync(propertyName) .ConfigureAwait(false); break; default: Debug.Assert( jsonReader.NodeType != JsonNodeType.EndOfInput, "We should not have reached end of input, since the scopes should be well formed. Otherwise JsonReader should have failed by now."); break; } }while (await jsonReader.ReadAsync().ConfigureAwait(false) && depth > 0); if (depth > 0) { // Not all open scopes were closed: // "Invalid JSON. Unexpected end of input was found in JSON content. Not all object and array scopes were closed." throw CreateException(Strings.JsonReader_EndOfInputWithOpenScope); } await jsonWriter.FlushAsync() .ConfigureAwait(false); } }