コード例 #1
0
        /// <summary>
        /// Asynchronously writes the json array value.
        /// </summary>
        /// <param name="jsonWriter">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="arrayValue">Writes the json array value to the underlying json writer.</param>
        /// <returns>A task that represents the asynchronous write operation.</returns>
        private static async Task WriteJsonArrayValueAsync(this IJsonWriterAsync jsonWriter, IEnumerable arrayValue)
        {
            Debug.Assert(arrayValue != null, "arrayValue != null");

            await jsonWriter.StartArrayScopeAsync().ConfigureAwait(false);

            foreach (object element in arrayValue)
            {
                await jsonWriter.WriteJsonValueAsync(element).ConfigureAwait(false);
            }

            await jsonWriter.EndArrayScopeAsync().ConfigureAwait(false);
        }
コード例 #2
0
        /// <summary>
        /// Asynchronously writes the error details.
        /// </summary>
        /// <param name="jsonWriter">JSON writer.</param>
        /// <param name="details">The details of the error.</param>
        /// <param name="odataErrorDetailsName">The property name for the error details property.</param>
        /// <returns>A task that represents the asynchronous write operation.</returns>
        private static async Task WriteErrorDetailsAsync(IJsonWriterAsync jsonWriter, IEnumerable <ODataErrorDetail> details,
                                                         string odataErrorDetailsName)
        {
            Debug.Assert(jsonWriter != null, "jsonWriter != null");
            Debug.Assert(details != null, "details != null");
            Debug.Assert(odataErrorDetailsName != null, "odataErrorDetailsName != null");

            // "details": [
            await jsonWriter.WriteNameAsync(odataErrorDetailsName).ConfigureAwait(false);

            await jsonWriter.StartArrayScopeAsync().ConfigureAwait(false);

            foreach (var detail in details.Where(d => d != null))
            {
                // {
                await jsonWriter.StartObjectScopeAsync().ConfigureAwait(false);

                // "code": "301",
                await jsonWriter.WriteNameAsync(JsonConstants.ODataErrorCodeName).ConfigureAwait(false);

                await jsonWriter.WriteValueAsync(detail.ErrorCode ?? string.Empty).ConfigureAwait(false);

                if (detail.Target != null)
                {
                    // "target": "$search"
                    await jsonWriter.WriteNameAsync(JsonConstants.ODataErrorTargetName).ConfigureAwait(false);

                    await jsonWriter.WriteValueAsync(detail.Target).ConfigureAwait(false);
                }

                // "message": "$search query option not supported",
                await jsonWriter.WriteNameAsync(JsonConstants.ODataErrorMessageName).ConfigureAwait(false);

                await jsonWriter.WriteValueAsync(detail.Message ?? string.Empty).ConfigureAwait(false);

                // }
                await jsonWriter.EndObjectScopeAsync().ConfigureAwait(false);
            }

            // ]
            await jsonWriter.EndArrayScopeAsync().ConfigureAwait(false);
        }
コード例 #3
0
        /// <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);
        }
コード例 #4
0
        /// <summary>
        /// Asynchronously writes the ODataValue (primitive, collection or resource value) to the underlying json writer.
        /// </summary>
        /// <param name="jsonWriter">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="odataValue">value to write.</param>
        /// <returns>A task that represents the asynchronous write operation.</returns>
        internal static async Task WriteODataValueAsync(this IJsonWriterAsync jsonWriter, ODataValue odataValue)
        {
            if (odataValue == null || odataValue is ODataNullValue)
            {
                await jsonWriter.WriteValueAsync((string)null).ConfigureAwait(false);

                return;
            }

            object objectValue = odataValue.FromODataValue();

            if (EdmLibraryExtensions.IsPrimitiveType(objectValue.GetType()))
            {
                await jsonWriter.WritePrimitiveValueAsync(objectValue).ConfigureAwait(false);

                return;
            }

            ODataResourceValue resourceValue = odataValue as ODataResourceValue;

            if (resourceValue != null)
            {
                await jsonWriter.StartObjectScopeAsync().ConfigureAwait(false);

                foreach (ODataProperty property in resourceValue.Properties)
                {
                    await jsonWriter.WriteNameAsync(property.Name).ConfigureAwait(false);

                    await jsonWriter.WriteODataValueAsync(property.ODataValue).ConfigureAwait(false);
                }

                await jsonWriter.EndObjectScopeAsync().ConfigureAwait(false);

                return;
            }

            ODataCollectionValue collectionValue = odataValue as ODataCollectionValue;

            if (collectionValue != null)
            {
                await jsonWriter.StartArrayScopeAsync().ConfigureAwait(false);

                foreach (object item in collectionValue.Items)
                {
                    // Will not be able to accurately serialize complex objects unless they are ODataValues.
                    ODataValue collectionItem = item as ODataValue;
                    if (item != null)
                    {
                        await jsonWriter.WriteODataValueAsync(collectionItem).ConfigureAwait(false);
                    }
                    else
                    {
                        throw new ODataException(ODataErrorStrings.ODataJsonWriter_UnsupportedValueInCollection);
                    }
                }

                await jsonWriter.EndArrayScopeAsync().ConfigureAwait(false);

                return;
            }

            throw new ODataException(
                      ODataErrorStrings.ODataJsonWriter_UnsupportedValueType(odataValue.GetType().FullName));
        }