예제 #1
0
        /// <summary>
        /// Asynchronously reads the json object value from the jsonReader
        /// </summary>
        /// <param name="jsonReader">Json reader to read payload from the wire.</param>
        /// <param name="insideJsonObjectValue">true if the reader is positioned on the first property of the value which is a JSON Object
        /// (or the second property if the first one was odata.type).</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 an instance of IDictionary containing the spatial value.</returns>
        private static async Task <Dictionary <string, object> > ReadObjectValueAsync(
            IJsonReaderAsync jsonReader,
            bool insideJsonObjectValue,
            ODataInputContext inputContext,
            int recursionDepth)
        {
            Debug.Assert(jsonReader != null, "jsonReader != null");
            Debug.Assert(insideJsonObjectValue || jsonReader.NodeType == JsonNodeType.StartObject,
                         "insideJsonObjectValue || jsonReader.NodeType == JsonNodeType.StartObject");
            Debug.Assert(
                !insideJsonObjectValue || jsonReader.NodeType == JsonNodeType.Property || jsonReader.NodeType == JsonNodeType.EndObject,
                "!insideJsonObjectValue || jsonReader.NodeType == JsonNodeType.Property || jsonReader.NodeType == JsonNodeType.EndObject");
            Debug.Assert(inputContext != null, "inputContext != null");

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

            Dictionary <string, object> jsonValue = new Dictionary <string, object>(StringComparer.Ordinal);

            if (!insideJsonObjectValue)
            {
                // Note that if the insideJsonObjectValue is true we will ignore the odata.type instance annotation
                // which might have been there. This is OK since for spatial we only need the normal properties anyway.
                await jsonReader.ReadNextAsync()
                .ConfigureAwait(false);
            }

            while (jsonReader.NodeType != JsonNodeType.EndObject)
            {
                // read the property name
                string propertyName = await jsonReader.ReadPropertyNameAsync()
                                      .ConfigureAwait(false);

                // read the property value
                object propertyValue;
                switch (jsonReader.NodeType)
                {
                case JsonNodeType.PrimitiveValue:
                    propertyValue = await jsonReader.ReadPrimitiveValueAsync()
                                    .ConfigureAwait(false);

                    break;

                case JsonNodeType.StartArray:
                    propertyValue = await ReadArrayValueAsync(jsonReader, inputContext, recursionDepth)
                                    .ConfigureAwait(false);

                    break;

                case JsonNodeType.StartObject:
                    propertyValue = await ReadObjectValueAsync(jsonReader, /*insideJsonObjectValue*/ false, inputContext, recursionDepth)
                                    .ConfigureAwait(false);

                    break;

                default:
                    Debug.Assert(false, "We should never reach here - There should be matching end element");
                    return(null);
                }

                jsonValue.Add(ODataAnnotationNames.RemoveAnnotationPrefix(propertyName), propertyValue);
            }

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

            return(jsonValue);
        }
예제 #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));
            }
        }