internal static async Task <bool> TryReadNullValueAsync( #endif IJsonReaderAsync jsonReader, ODataInputContext inputContext, IEdmTypeReference expectedTypeReference, bool validateNullValue, string propertyName, bool?isDynamicProperty = null) { Debug.Assert(jsonReader != null, "jsonReader != null"); Debug.Assert(inputContext != null, "inputContext != null"); if (jsonReader.NodeType == JsonNodeType.PrimitiveValue && await jsonReader.GetValueAsync().ConfigureAwait(false) == null) { await jsonReader.ReadNextAsync() .ConfigureAwait(false); // NOTE: when reading a null value we will never ask the type resolver (if present) to resolve the // type; we always fall back to the expected type. inputContext.MessageReaderSettings.Validator.ValidateNullValue( expectedTypeReference, validateNullValue, propertyName, isDynamicProperty); return(true); } return(false); }
/// <summary> /// Verifies that the current node is a property node and returns the property name. /// </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 property name of the current property node.</returns> internal static async Task <string> GetPropertyNameAsync(this IJsonReaderAsync jsonReader) { Debug.Assert(jsonReader != null, "jsonReader != null"); Debug.Assert(jsonReader.NodeType == JsonNodeType.Property, "jsonReader.NodeType == JsonNodeType.Property"); // NOTE: the JSON reader already verifies that property names are strings and not null/empty object value = await jsonReader.GetValueAsync() .ConfigureAwait(false); return((string)value); }
/// <summary> /// Asynchronously reads the next node from the <paramref name="jsonReader"/> and verifies that it is a PrimitiveValue 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 primitive value read from the reader.</returns> internal static async Task <object> ReadPrimitiveValueAsync(this IJsonReaderAsync jsonReader) { Debug.Assert(jsonReader != null, "jsonReader != null"); object value = await jsonReader.GetValueAsync() .ConfigureAwait(false); await ReadNextAsync(jsonReader, JsonNodeType.PrimitiveValue) .ConfigureAwait(false); return(value); }
/// <summary> /// Asynchronously writes the current Json object. /// </summary> /// <param name="reader">The Json reader providing the data.</param> /// <param name="jsonWriter">The Json writer writes data into memory stream.</param> /// <returns>A task that represents the asynchronous write operation.</returns> private static async Task WriteCurrentJsonObjectAsync(IJsonReaderAsync reader, IJsonWriterAsync jsonWriter) { Stack <JsonNodeType> nodeTypes = new Stack <JsonNodeType>(); do { switch (reader.NodeType) { case JsonNodeType.PrimitiveValue: object primitiveValue; if ((primitiveValue = await reader.GetValueAsync().ConfigureAwait(false)) != null) { await jsonWriter.WritePrimitiveValueAsync(primitiveValue) .ConfigureAwait(false); } else { await jsonWriter.WriteValueAsync((string)null) .ConfigureAwait(false); } break; case JsonNodeType.Property: object propertyName = await reader.GetValueAsync() .ConfigureAwait(false); await jsonWriter.WriteNameAsync(propertyName.ToString()) .ConfigureAwait(false); break; case JsonNodeType.StartObject: nodeTypes.Push(reader.NodeType); await jsonWriter.StartObjectScopeAsync() .ConfigureAwait(false); break; case JsonNodeType.StartArray: nodeTypes.Push(reader.NodeType); await jsonWriter.StartArrayScopeAsync() .ConfigureAwait(false); break; case JsonNodeType.EndObject: Debug.Assert(nodeTypes.Peek() == JsonNodeType.StartObject); nodeTypes.Pop(); await jsonWriter.EndObjectScopeAsync() .ConfigureAwait(false); break; case JsonNodeType.EndArray: Debug.Assert(nodeTypes.Peek() == JsonNodeType.StartArray); nodeTypes.Pop(); await jsonWriter.EndArrayScopeAsync() .ConfigureAwait(false); break; default: throw new ODataException(Strings.ODataJsonLightBatchBodyContentReaderStream_UnexpectedNodeType(reader.NodeType)); } await reader.ReadNextAsync() .ConfigureAwait(false); // This can be EndOfInput, where nodeTypes should be empty. }while (nodeTypes.Count != 0); await jsonWriter.FlushAsync() .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); } }