Exemplo n.º 1
0
        /// <summary>
        /// Start writing a regular (non-expanded) link.
        /// </summary>
        /// <param name="link">The link to write.</param>
        protected override void WriteLink(ODataLink link)
        {
            Debug.Assert(link != null, "link != null");

            ValidationUtils.ValidateLink(link);

            // Link must specify the Url for non-expanded links
            // NOTE: we currently only require a non-null Url for ATOM payloads and non-expanded links in JSON.
            //       There is no place in JSON to write a Url if the link is expanded. We can't change that for v1 and v2; we
            //       might fix the protocol for v3.
            if (link.Url == null)
            {
                throw new ODataException(Strings.ODataWriter_LinkMustSpecifyUrl);
            }

            Debug.Assert(!string.IsNullOrEmpty(link.Name), "The link Name should have been validated by now.");
            this.jsonWriter.WriteName(link.Name);

            // A deferred link is represented as an object
            this.jsonWriter.StartObjectScope();

            // "__deferred": {
            this.jsonWriter.WriteName(JsonConstants.ODataDeferredName);
            this.jsonWriter.StartObjectScope();

            Debug.Assert(link.Url != null, "The link Url should have been validated by now.");
            this.jsonWriter.WriteName(JsonConstants.ODataLinkUriName);
            this.jsonWriter.WriteValue(ODataJsonWriterUtils.UriToAbsoluteUriString(link.Url, this.BaseUri));

            // End the __deferred object
            this.jsonWriter.EndObjectScope();

            // End the link value
            this.jsonWriter.EndObjectScope();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Finish writing a feed.
        /// </summary>
        /// <param name="feed">The feed to write.</param>
        protected override void EndFeed(ODataFeed feed)
        {
            Debug.Assert(feed != null, "feed != null");

            // End the array which holds the entries in the feed.
            this.jsonWriter.EndArrayScope();

            // We need to close the "results" wrapper for V2 and higher and only for responses
            Uri nextPageLink = feed.NextPageLink;

            if (this.Version >= ODataVersion.V2 && (this.WritingResponse || !this.IsTopLevel))
            {
                // "__next": "url"
                if (nextPageLink != null)
                {
                    this.jsonWriter.WriteName(JsonConstants.ODataNextLinkName);
                    this.jsonWriter.WriteValue(ODataJsonWriterUtils.UriToAbsoluteUriString(nextPageLink, this.BaseUri));
                }

                this.jsonWriter.EndObjectScope();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Writes the __metadata property and its content for an entry
        /// </summary>
        /// <param name="entry">The entry for which to write the metadata.</param>
        /// <returns>The resource type of the entry or null if no metadata is available.</returns>
        private ResourceType WriteEntryMetadata(ODataEntry entry)
        {
            Debug.Assert(entry != null, "entry != null");

            // Write the "__metadata" for the entry
            this.jsonWriter.WriteName(JsonConstants.ODataMetadataName);
            this.jsonWriter.StartObjectScope();

            // Write the "uri": "edit/read-link-uri"
            Uri uriValue = entry.EditLink;

            if (uriValue == null)
            {
                uriValue = entry.ReadLink;
            }

            if (uriValue != null)
            {
                this.jsonWriter.WriteName(JsonConstants.ODataMetadataUriName);
                this.jsonWriter.WriteValue(ODataJsonWriterUtils.UriToAbsoluteUriString(uriValue, this.BaseUri));
            }

            // Write the "etag": "ETag value"
            // TODO: if this is a top-level entry also put the ETag into the headers.
            string etag = entry.ETag;

            if (etag != null)
            {
                this.jsonWriter.WriteName(JsonConstants.ODataMetadataETagName);
                this.jsonWriter.WriteValue(etag);
            }

            // Write the "type": "typename"
            string       typeName  = entry.TypeName;
            ResourceType entryType = ValidationUtils.ValidateTypeName(this.MetadataProvider, typeName, ResourceTypeKind.EntityType, false);

            if (typeName != null)
            {
                this.jsonWriter.WriteName(JsonConstants.ODataMetadataTypeName);
                this.jsonWriter.WriteValue(typeName);
            }

            // Write MLE metadata
            ODataMediaResource mediaResource = entry.MediaResource;

            if (mediaResource != null)
            {
                // Write the "edit_media": "url"
                Uri mediaEditLink = mediaResource.EditLink;
                if (mediaEditLink != null)
                {
                    this.jsonWriter.WriteName(JsonConstants.ODataMetadataEditMediaName);
                    this.jsonWriter.WriteValue(ODataJsonWriterUtils.UriToAbsoluteUriString(mediaEditLink, this.BaseUri));
                }

                // Write the "media_src": "url"
                Debug.Assert(mediaResource.ReadLink != null, "The default stream read link should have been validated by now.");
                this.jsonWriter.WriteName(JsonConstants.ODataMetadataMediaUriName);
                this.jsonWriter.WriteValue(ODataJsonWriterUtils.UriToAbsoluteUriString(mediaResource.ReadLink, this.BaseUri));

                // Write the "content_type": "type"
                Debug.Assert(!string.IsNullOrEmpty(mediaResource.ContentType), "The default stream content type should have been validated by now.");
                this.jsonWriter.WriteName(JsonConstants.ODataMetadataContentTypeName);
                this.jsonWriter.WriteValue(mediaResource.ContentType);

                // Write the "media_etag": "etag"
                string mediaETag = mediaResource.ETag;
                if (mediaETag != null)
                {
                    Debug.Assert(mediaEditLink != null, "The default stream edit link and etag should have been validated by now.");
                    this.jsonWriter.WriteName(JsonConstants.ODataMetadataMediaETagName);
                    this.jsonWriter.WriteValue(mediaETag);
                }
            }

            // Write properties metadata
            // For now only association links are supported here
            IEnumerable <ODataAssociationLink> associationLinks = entry.AssociationLinks;

            if (associationLinks != null)
            {
                bool firstAssociationLink = true;

                foreach (ODataAssociationLink associationLink in associationLinks)
                {
                    ValidationUtils.ValidateAssociationLink(associationLink, this.Version);

                    if (firstAssociationLink)
                    {
                        // Write the "properties": {
                        this.jsonWriter.WriteName(JsonConstants.ODataMetadataPropertiesName);
                        this.jsonWriter.StartObjectScope();

                        firstAssociationLink = false;
                    }

                    // Write the "LinkName": {
                    this.jsonWriter.WriteName(associationLink.Name);
                    this.jsonWriter.StartObjectScope();

                    // Write the "__associationuri": "url"
                    this.jsonWriter.WriteName(JsonConstants.ODataMetadataPropertiesAssociationUriName);
                    this.jsonWriter.WriteValue(ODataJsonWriterUtils.UriToAbsoluteUriString(associationLink.Url, this.BaseUri));

                    // Close the "LinkName" object
                    this.jsonWriter.EndObjectScope();
                }

                if (!firstAssociationLink)
                {
                    // Close the "properties" object
                    this.jsonWriter.EndObjectScope();
                }
            }

            // Close the __metadata object scope
            this.jsonWriter.EndObjectScope();

            return(entryType);
        }