コード例 #1
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);
        }