Ejemplo n.º 1
0
        /// <summary>
        /// Asynchronously reads the json array from the reader.
        /// </summary>
        /// <param name="jsonReader">JsonReader instance.</param>
        /// <param name="inputContext">The input context with all the settings.</param>
        /// <param name="recursionDepth">The recursion depth to start with.</param>
        /// <returns>A task that represents the asynchronous read operation.
        /// The value of the TResult parameter contains a lit of JSON objects.</returns>
        /// <returns>a list of json objects.</returns>
        private static async Task <List <object> > ReadArrayValueAsync(IJsonReaderAsync jsonReader, ODataInputContext inputContext, int recursionDepth)
        {
            Debug.Assert(jsonReader != null, "jsonReader != null");
            Debug.Assert(jsonReader.NodeType == JsonNodeType.StartArray, "jsonReader.NodeType == JsonNodeType.StartArray");
            Debug.Assert(inputContext != null, "inputContext != null");

            ValidationUtils.IncreaseAndValidateRecursionDepth(ref recursionDepth, inputContext.MessageReaderSettings.MessageQuotas.MaxNestingDepth);

            List <object> items = new List <object>();
            await jsonReader.ReadNextAsync()
            .ConfigureAwait(false);

            while (jsonReader.NodeType != JsonNodeType.EndArray)
            {
                switch (jsonReader.NodeType)
                {
                case JsonNodeType.PrimitiveValue:
                    items.Add(await jsonReader.ReadPrimitiveValueAsync()
                              .ConfigureAwait(false));
                    break;

                case JsonNodeType.StartObject:
                    items.Add(await ReadObjectValueAsync(jsonReader, /*insideJsonObjectValue*/ false, inputContext, recursionDepth)
                              .ConfigureAwait(false));
                    break;

                case JsonNodeType.StartArray:
                    items.Add(await ReadArrayValueAsync(jsonReader, inputContext, recursionDepth)
                              .ConfigureAwait(false));
                    break;

                default:
                    Debug.Assert(false, "We should never have got here - the valid states in array are primitive value or object");
                    return(null);
                }
            }

            await jsonReader.ReadEndArrayAsync()
            .ConfigureAwait(false);

            return(items);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Asynchronously reads the next node from the <paramref name="jsonReader"/> as an <see cref="ODataValue"/>.
        /// </summary>
        /// <param name="jsonReader">The reader to inspect.</param>
        /// <returns>A task that represents the asynchronous read operation.
        /// The value of the TResult parameter contains the <see cref="ODataValue"/> value read from the reader.</returns>
        internal static async Task <ODataValue> ReadODataValueAsync(this IJsonReaderAsync jsonReader)
        {
            if (jsonReader.NodeType == JsonNodeType.PrimitiveValue)
            {
                object primitiveValue = await jsonReader.ReadPrimitiveValueAsync()
                                        .ConfigureAwait(false);

                return(primitiveValue.ToODataValue());
            }
            else if (jsonReader.NodeType == JsonNodeType.StartObject)
            {
                await jsonReader.ReadStartObjectAsync()
                .ConfigureAwait(false);

                ODataResourceValue   resourceValue = new ODataResourceValue();
                List <ODataProperty> properties    = new List <ODataProperty>();

                while (jsonReader.NodeType != JsonNodeType.EndObject)
                {
                    ODataProperty property = new ODataProperty();
                    property.Name = await jsonReader.ReadPropertyNameAsync()
                                    .ConfigureAwait(false);

                    property.Value = await jsonReader.ReadODataValueAsync()
                                     .ConfigureAwait(false);

                    properties.Add(property);
                }

                resourceValue.Properties = properties;

                await jsonReader.ReadEndObjectAsync()
                .ConfigureAwait(false);

                return(resourceValue);
            }
            else if (jsonReader.NodeType == JsonNodeType.StartArray)
            {
                await jsonReader.ReadStartArrayAsync()
                .ConfigureAwait(false);

                ODataCollectionValue collectionValue = new ODataCollectionValue();
                List <object>        properties      = new List <object>();

                while (jsonReader.NodeType != JsonNodeType.EndArray)
                {
                    ODataValue odataValue = await jsonReader.ReadODataValueAsync()
                                            .ConfigureAwait(false);

                    properties.Add(odataValue);
                }

                collectionValue.Items = properties;
                await jsonReader.ReadEndArrayAsync()
                .ConfigureAwait(false);

                return(collectionValue);
            }
            else
            {
                return(await jsonReader.ReadAsUntypedOrNullValueAsync()
                       .ConfigureAwait(false));
            }
        }