Ejemplo n.º 1
0
        /// <summary>
        /// Write an inner error property and message.
        /// </summary>
        /// <param name="jsonWriter">The JSON writer to write the error to.</param>
        /// <param name="innerError">Inner error details.</param>
        /// <param name="innerErrorPropertyName">The property name for the inner error property.</param>
        /// <param name="recursionDepth">The number of times this method has been called recursively.</param>
        /// <param name="maxInnerErrorDepth">The maximum number of nested inner errors to allow.</param>
        /// <returns>A task that represents the asynchronous write operation.</returns>
        private static async Task WriteInnerErrorAsync(IJsonWriterAsync jsonWriter, ODataInnerError innerError,
                                                       string innerErrorPropertyName, int recursionDepth, int maxInnerErrorDepth)
        {
            Debug.Assert(jsonWriter != null, "jsonWriter != null");
            Debug.Assert(innerErrorPropertyName != null, "innerErrorPropertyName != null");

            ValidationUtils.IncreaseAndValidateRecursionDepth(ref recursionDepth, maxInnerErrorDepth);

            // "innererror":
            await jsonWriter.WriteNameAsync(innerErrorPropertyName).ConfigureAwait(false);

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

            if (innerError.Properties != null)
            {
                foreach (KeyValuePair <string, ODataValue> pair in innerError.Properties)
                {
                    await jsonWriter.WriteNameAsync(pair.Key).ConfigureAwait(false);

                    if (pair.Value is ODataNullValue &&
                        (pair.Key == JsonConstants.ODataErrorInnerErrorMessageName ||
                         pair.Key == JsonConstants.ODataErrorInnerErrorStackTraceName ||
                         pair.Key == JsonConstants.ODataErrorInnerErrorTypeNameName))
                    {
                        // Write empty string for null values in stacktrace, type and message properties of inner error.
                        await jsonWriter.WriteODataValueAsync(new ODataPrimitiveValue(string.Empty)).ConfigureAwait(false);
                    }
                    else
                    {
                        await jsonWriter.WriteODataValueAsync(pair.Value).ConfigureAwait(false);
                    }
                }
            }

            if (innerError.InnerError != null)
            {
                // "internalexception": { <nested inner error> }
                await WriteInnerErrorAsync(jsonWriter, innerError.InnerError, JsonConstants.ODataErrorInnerErrorInnerErrorName,
                                           recursionDepth, maxInnerErrorDepth).ConfigureAwait(false);
            }

            // }
            await jsonWriter.EndObjectScopeAsync().ConfigureAwait(false);
        }
        /// <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));
        }