/// <summary>
        /// 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>
        internal static void WriteODataValue(this IJsonWriter jsonWriter, ODataValue odataValue)
        {
            if (odataValue == null || odataValue is ODataNullValue)
            {
                jsonWriter.WriteValue((string)null);
                return;
            }

            object objectValue = odataValue.FromODataValue();

            if (EdmLibraryExtensions.IsPrimitiveType(objectValue.GetType()))
            {
                jsonWriter.WritePrimitiveValue(objectValue);
                return;
            }

            ODataResourceValue resourceValue = odataValue as ODataResourceValue;

            if (resourceValue != null)
            {
                jsonWriter.StartObjectScope();

                foreach (ODataProperty property in resourceValue.Properties)
                {
                    jsonWriter.WriteName(property.Name);
                    jsonWriter.WriteODataValue(property.ODataValue);
                }

                jsonWriter.EndObjectScope();
                return;
            }

            ODataCollectionValue collectionValue = odataValue as ODataCollectionValue;

            if (collectionValue != null)
            {
                jsonWriter.StartArrayScope();

                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)
                    {
                        jsonWriter.WriteODataValue(collectionItem);
                    }
                    else
                    {
                        throw new ODataException(ODataErrorStrings.ODataJsonWriter_UnsupportedValueInCollection);
                    }
                }

                jsonWriter.EndArrayScope();

                return;
            }

            throw new ODataException(
                      ODataErrorStrings.ODataJsonWriter_UnsupportedValueType(odataValue.GetType().FullName));
        }
        /// <summary>
        /// Ends a batch - implementation of the actual functionality.
        /// </summary>
        protected override void WriteEndBatchImplementation()
        {
            // write pending message data (headers, response line) for a previously unclosed message/request
            this.WritePendingMessageData(true);

            this.SetState(BatchWriterState.BatchCompleted);

            // Close the messages array
            jsonWriter.EndArrayScope();

            // Close the top level scope
            jsonWriter.EndObjectScope();
        }
        /// <summary>
        /// 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>
        private static void WriteJsonArrayValue(this IJsonWriter jsonWriter, IEnumerable arrayValue)
        {
            Debug.Assert(arrayValue != null, "arrayValue != null");

            jsonWriter.StartArrayScope();

            foreach (object element in arrayValue)
            {
                jsonWriter.WriteJsonValue(element);
            }

            jsonWriter.EndArrayScope();
        }
示例#4
0
        /// <summary>
        /// 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>
        /// <param name="odataVersion">The OData protocol version to be used for writing payloads.</param>
        private static void WriteJsonArrayValue(this IJsonWriter jsonWriter, IEnumerable arrayValue, ODataVersion odataVersion)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(arrayValue != null, "arrayValue != null");

            jsonWriter.StartArrayScope();

            foreach (object element in arrayValue)
            {
                jsonWriter.WriteJsonValue(element, odataVersion);
            }

            jsonWriter.EndArrayScope();
        }
示例#5
0
        private static void WriteErrorDetails(
            IJsonWriter 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": [
            jsonWriter.WriteName(odataErrorDetailsName);
            jsonWriter.StartArrayScope();

            foreach (var detail in details.Where(d => d != null))
            {
                // {
                jsonWriter.StartObjectScope();

                // "code": "301",
                jsonWriter.WriteName(JsonConstants.ODataErrorCodeName);
                jsonWriter.WriteValue(detail.ErrorCode ?? string.Empty);

                if (detail.Target != null)
                {
                    // "target": "$search"
                    jsonWriter.WriteName(JsonConstants.ODataErrorTargetName);
                    jsonWriter.WriteValue(detail.Target);
                }

                // "message": "$search query option not supported",
                jsonWriter.WriteName(JsonConstants.ODataErrorMessageName);
                jsonWriter.WriteValue(detail.Message ?? string.Empty);

                // }
                jsonWriter.EndObjectScope();
            }

            // ]
            jsonWriter.EndArrayScope();
        }
        private static void WriteErrorDetails(IJsonWriter 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": [
            jsonWriter.WriteName(odataErrorDetailsName);
            jsonWriter.StartArrayScope();

            foreach (var detail in details.Where(d => d != null))
            {
                // {
                jsonWriter.StartObjectScope();

                // "code": "301",
                jsonWriter.WriteName(JsonConstants.ODataErrorCodeName);
                jsonWriter.WriteValue(detail.ErrorCode ?? string.Empty);

                if (detail.Target != null)
                {
                    // "target": "$search" 
                    jsonWriter.WriteName(JsonConstants.ODataErrorTargetName);
                    jsonWriter.WriteValue(detail.Target);
                }

                // "message": "$search query option not supported",                
                jsonWriter.WriteName(JsonConstants.ODataErrorMessageName);
                jsonWriter.WriteValue(detail.Message ?? string.Empty);

                // }
                jsonWriter.EndObjectScope();
            }

            // ]
            jsonWriter.EndArrayScope();
        }
        /// <summary>
        /// Start writing a feed.
        /// </summary>
        /// <param name="feed">The feed to write.</param>
        protected override void StartFeed(ODataFeed feed)
        {
            Debug.Assert(feed != null, "feed != null");

            IJsonWriter jsonWriter = this.jsonLightOutputContext.JsonWriter;

            if (this.ParentNavigationLink == null)
            {
                // Top-level feed.
                // "{"
                jsonWriter.StartObjectScope();

                // @odata.context
                this.jsonLightEntryAndFeedSerializer.WriteFeedContextUri(this.CurrentFeedScope.GetOrCreateTypeContext(this.jsonLightOutputContext.Model, this.jsonLightOutputContext.WritingResponse));

                if (this.jsonLightOutputContext.WritingResponse)
                {
                    // Write the inline count if it's available.
                    this.WriteFeedCount(feed, /*propertyName*/ null);

                    // Write the next link if it's available.
                    this.WriteFeedNextLink(feed, /*propertyName*/ null);

                    // Write the delta link if it's available.
                    this.WriteFeedDeltaLink(feed);
                }

                // Write custom instance annotations
                this.jsonLightEntryAndFeedSerializer.InstanceAnnotationWriter.WriteInstanceAnnotations(feed.InstanceAnnotations, this.CurrentFeedScope.InstanceAnnotationWriteTracker);

                // "value":
                jsonWriter.WriteValuePropertyName();

                // Start array which will hold the entries in the feed.
                jsonWriter.StartArrayScope();
            }
            else
            {
                // Expanded feed.
                Debug.Assert(
                    this.ParentNavigationLink != null && this.ParentNavigationLink.IsCollection.HasValue && this.ParentNavigationLink.IsCollection.Value,
                    "We should have verified that feeds can only be written into IsCollection = true links in requests.");
                string propertyName = this.ParentNavigationLink.Name;

                this.ValidateNoDeltaLinkForExpandedFeed(feed);
                this.ValidateNoCustomInstanceAnnotationsForExpandedFeed(feed);

                if (this.jsonLightOutputContext.WritingResponse)
                {
                    // Write the inline count if it's available.
                    this.WriteFeedCount(feed, propertyName);

                    // Write the next link if it's available.
                    this.WriteFeedNextLink(feed, propertyName);

                    // And then write the property name to start the value.
                    jsonWriter.WriteName(propertyName);

                    // Start array which will hold the entries in the feed.
                    jsonWriter.StartArrayScope();
                }
                else
                {
                    JsonLightNavigationLinkScope navigationLinkScope = (JsonLightNavigationLinkScope)this.ParentNavigationLinkScope;
                    if (!navigationLinkScope.FeedWritten)
                    {
                        // Close the entity reference link array (if written)
                        if (navigationLinkScope.EntityReferenceLinkWritten)
                        {
                            jsonWriter.EndArrayScope();
                        }

                        // And then write the property name to start the value.
                        jsonWriter.WriteName(propertyName);

                        // Start array which will hold the entries in the feed.
                        jsonWriter.StartArrayScope();

                        navigationLinkScope.FeedWritten = true;
                    }
                }
            }
        }
示例#8
0
        /// <summary>
        /// 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>
        private static void WriteCurrentJsonObject(IJsonReader reader, IJsonWriter jsonWriter)
        {
            Stack <JsonNodeType> nodeTypes = new Stack <JsonNodeType>();

            do
            {
                switch (reader.NodeType)
                {
                case JsonNodeType.PrimitiveValue:
                {
                    if (reader.Value != null)
                    {
                        jsonWriter.WritePrimitiveValue(reader.Value);
                    }
                    else
                    {
                        jsonWriter.WriteValue((string)null);
                    }
                }

                break;

                case JsonNodeType.Property:
                {
                    jsonWriter.WriteName(reader.Value.ToString());
                }

                break;

                case JsonNodeType.StartObject:
                {
                    nodeTypes.Push(reader.NodeType);
                    jsonWriter.StartObjectScope();
                }

                break;

                case JsonNodeType.StartArray:
                {
                    nodeTypes.Push(reader.NodeType);
                    jsonWriter.StartArrayScope();
                }

                break;

                case JsonNodeType.EndObject:
                {
                    Debug.Assert(nodeTypes.Peek() == JsonNodeType.StartObject);
                    nodeTypes.Pop();
                    jsonWriter.EndObjectScope();
                }

                break;

                case JsonNodeType.EndArray:
                {
                    Debug.Assert(nodeTypes.Peek() == JsonNodeType.StartArray);
                    nodeTypes.Pop();
                    jsonWriter.EndArrayScope();
                }

                break;

                default:
                {
                    throw new ODataException(String.Format(
                                                 CultureInfo.InvariantCulture,
                                                 "Unexpected reader.NodeType: {0}.",
                                                 reader.NodeType));
                }
                }

                reader.ReadNext(); // This can be EndOfInput, where nodeTypes should be empty.
            }while (nodeTypes.Count != 0);

            jsonWriter.Flush();
        }