public void ConvertFeed(Uri relativeODataUri, Uri relativeSodaUri, JsonPayload jsonPayload, DateTimeOffset feedUpdateTime) { var jsonObject = jsonPayload.JsonObject; var entries = jsonObject.PropertyValue<JArray>("entries"); var meta = jsonObject.PropertyValue<JObject>("meta"); var view = meta.PropertyValue<JObject>("view"); IList<string> fieldsToIgnore; var model = BuildModel(view, out fieldsToIgnore); var entitySet = model.EntityContainers.Single().EntitySets().Single(); var settings = new ODataMessageWriterSettings { Indent = true, }; using (var writer = new ODataMessageWriter(Message, settings, model)) { var feedWriter = writer.CreateODataFeedWriter(); var feed = new ODataFeed(); feed.SetAnnotation(new AtomFeedMetadata { Updated = feedUpdateTime, }); feed.Id = new Uri(ODataEndpointUri, relativeODataUri.OriginalString).OriginalString; feedWriter.WriteStart(feed); foreach (var entry in entries.Cast<JObject>()) { var entryMetadata = new ODataEntry(); entryMetadata.Id = (string) ((JValue) entry.Property("id").Value).Value; entryMetadata.TypeName = entitySet.ElementType.FullName(); entryMetadata.Properties = ConvertProperties(entry, fieldsToIgnore); entryMetadata.SetAnnotation(new AtomEntryMetadata { Updated = ConvertDateTimeOffset(entry.PrimitivePropertyValue<long>("updated_at")), Published = ConvertDateTimeOffset(entry.PrimitivePropertyValue<long>("created_at")), }); feedWriter.WriteStart(entryMetadata); feedWriter.WriteEnd(); } feedWriter.WriteEnd(); } }
public static MaterializerFeed CreateFeed(ODataFeed feed, IEnumerable<ODataEntry> entries) { if (entries == null) { entries = Enumerable.Empty<ODataEntry>(); } else { feed.SetAnnotation<IEnumerable<ODataEntry>>(entries); } return new MaterializerFeed(feed, entries); }
/// <summary> /// Create the <see cref="ODataFeed"/> to be written for the given feed instance. /// </summary> /// <param name="feedInstance">The instance representing the feed being written.</param> /// <param name="writeContext">The serializer context.</param> /// <returns>The created <see cref="ODataFeed"/> object.</returns> public virtual ODataFeed CreateODataFeed(IEnumerable feedInstance, ODataSerializerContext writeContext) { ODataFeed feed = new ODataFeed(); if (writeContext.EntitySet != null) { IEdmModel model = writeContext.Model; EntitySetLinkBuilderAnnotation linkBuilder = model.GetEntitySetLinkBuilder(writeContext.EntitySet); FeedContext feedContext = new FeedContext { Request = writeContext.Request, EntitySet = writeContext.EntitySet, Url = writeContext.Url, FeedInstance = feedInstance }; Uri feedSelfLink = linkBuilder.BuildFeedSelfLink(feedContext); if (feedSelfLink != null) { feed.SetAnnotation(new AtomFeedMetadata() { SelfLink = new AtomLinkMetadata() { Relation = "self", Href = feedSelfLink } }); } } // TODO: Bug 467590: remove the hardcoded feed id. Get support for it from the model builder ? feed.Id = "http://schemas.datacontract.org/2004/07/" + EntityCollectionType.FullName(); // If we have more OData format specific information apply it now, only if we are the root feed. if (!writeContext.IsNested) { PageResult odataFeedAnnotations = feedInstance as PageResult; if (odataFeedAnnotations != null) { feed.Count = odataFeedAnnotations.Count; feed.NextPageLink = odataFeedAnnotations.NextPageLink; } else if (writeContext.Request != null) { feed.NextPageLink = writeContext.Request.GetNextPageLink(); long? inlineCount = writeContext.Request.GetInlineCount(); if (inlineCount.HasValue) { feed.Count = inlineCount.Value; } } } return feed; }
private void WriteFeed(object graph, ODataWriter writer, ODataSerializerContext writeContext) { ODataSerializer entrySerializer = SerializerProvider.GetEdmTypeSerializer(_edmCollectionType.ElementType()); if (entrySerializer == null) { throw Error.NotSupported(SRResources.TypeCannotBeSerialized, _edmCollectionType.ElementType(), typeof(ODataMediaTypeFormatter).Name); } Contract.Assert(entrySerializer.ODataPayloadKind == ODataPayloadKind.Entry); IEnumerable enumerable = graph as IEnumerable; // Data to serialize if (enumerable != null) { ODataFeed feed = new ODataFeed(); if (writeContext.EntitySet != null) { IEntitySetLinkBuilder linkBuilder = SerializerProvider.EdmModel.GetEntitySetLinkBuilder(writeContext.EntitySet); Uri feedSelfLink = linkBuilder.BuildFeedSelfLink(new FeedContext(writeContext.EntitySet, writeContext.UrlHelper, graph)); if (feedSelfLink != null) { feed.SetAnnotation(new AtomFeedMetadata() { SelfLink = new AtomLinkMetadata() { Relation = SelfLinkRelation, Href = feedSelfLink } }); } } // TODO: Bug 467590: remove the hardcoded feed id. Get support for it from the model builder ? feed.Id = FeedNamespace + _edmCollectionType.FullName(); // If we have more OData format specific information apply it now. ODataResult odataFeedAnnotations = graph as ODataResult; if (odataFeedAnnotations != null) { feed.Count = odataFeedAnnotations.Count; feed.NextPageLink = odataFeedAnnotations.NextPageLink; } writer.WriteStart(feed); foreach (object entry in enumerable) { entrySerializer.WriteObjectInline(entry, writer, writeContext); } writer.WriteEnd(); } }
private void WriteFeed(object graph, ODataWriter writer, ODataSerializerContext writeContext) { IEnumerable enumerable = graph as IEnumerable; // Data to serialize if (enumerable != null) { ODataFeed feed = new ODataFeed(); if (writeContext.EntitySet != null) { IEdmModel model = writeContext.Model; EntitySetLinkBuilderAnnotation linkBuilder = model.GetEntitySetLinkBuilder(writeContext.EntitySet); FeedContext feedContext = new FeedContext { Request = writeContext.Request, EntitySet = writeContext.EntitySet, Url = writeContext.Url, FeedInstance = graph }; Uri feedSelfLink = linkBuilder.BuildFeedSelfLink(feedContext); if (feedSelfLink != null) { feed.SetAnnotation(new AtomFeedMetadata() { SelfLink = new AtomLinkMetadata() { Relation = "self", Href = feedSelfLink } }); } } // TODO: Bug 467590: remove the hardcoded feed id. Get support for it from the model builder ? feed.Id = "http://schemas.datacontract.org/2004/07/" + _edmCollectionType.FullName(); // Compute and save the NextPageLink for JSON Light streaming support. Uri nextPageLink = null; // If we have more OData format specific information apply it now. PageResult odataFeedAnnotations = graph as PageResult; if (odataFeedAnnotations != null) { feed.Count = odataFeedAnnotations.Count; nextPageLink = odataFeedAnnotations.NextPageLink; } else { nextPageLink = writeContext.NextPageLink; long? inlineCount = writeContext.InlineCount; if (inlineCount.HasValue) { feed.Count = inlineCount.Value; } } writer.WriteStart(feed); foreach (object entry in enumerable) { if (entry == null) { throw Error.NotSupported(SRResources.NullElementInCollection); } ODataSerializer entrySerializer = SerializerProvider.GetODataPayloadSerializer(writeContext.Model, entry.GetType()); if (entrySerializer == null) { throw Error.NotSupported(SRResources.TypeCannotBeSerialized, entry.GetType(), typeof(ODataMediaTypeFormatter).Name); } Contract.Assert(entrySerializer.ODataPayloadKind == ODataPayloadKind.Entry); entrySerializer.WriteObjectInline(entry, writer, writeContext); } // Subtle and suprising behavior: If the NextPageLink property is set before calling WriteStart(feed), // the next page link will be written early in a manner not compatible with streaming=true. Instead, if // the next page link is not set when calling WriteStart(feed) but is instead set later on that feed // object before calling WriteEnd(), the next page link will be written at the end, as required for // streaming=true support. if (nextPageLink != null) { feed.NextPageLink = nextPageLink; } writer.WriteEnd(); } }
private void WriteFeed(object graph, ODataWriter writer, ODataSerializerContext writeContext) { IEnumerable enumerable = graph as IEnumerable; // Data to serialize if (enumerable != null) { ODataFeed feed = new ODataFeed(); if (writeContext.EntitySet != null) { IEntitySetLinkBuilder linkBuilder = SerializerProvider.EdmModel.GetEntitySetLinkBuilder(writeContext.EntitySet); FeedContext feedContext = new FeedContext { EntitySet = writeContext.EntitySet, UrlHelper = writeContext.UrlHelper, PathHandler = writeContext.PathHandler, FeedInstance = graph }; Uri feedSelfLink = linkBuilder.BuildFeedSelfLink(feedContext); if (feedSelfLink != null) { feed.SetAnnotation(new AtomFeedMetadata() { SelfLink = new AtomLinkMetadata() { Relation = SelfLinkRelation, Href = feedSelfLink } }); } } // TODO: Bug 467590: remove the hardcoded feed id. Get support for it from the model builder ? feed.Id = FeedNamespace + _edmCollectionType.FullName(); // If we have more OData format specific information apply it now. ODataResult odataFeedAnnotations = graph as ODataResult; if (odataFeedAnnotations != null) { feed.Count = odataFeedAnnotations.Count; feed.NextPageLink = odataFeedAnnotations.NextPageLink; } else { object nextPageLinkPropertyValue; HttpRequestMessage request = writeContext.Request; if (request != null && request.Properties.TryGetValue(ODataQueryOptions.NextPageLinkPropertyKey, out nextPageLinkPropertyValue)) { Uri nextPageLink = nextPageLinkPropertyValue as Uri; if (nextPageLink != null) { feed.NextPageLink = nextPageLink; } } } writer.WriteStart(feed); foreach (object entry in enumerable) { if (entry == null) { throw Error.NotSupported(SRResources.NullElementInCollection); } ODataSerializer entrySerializer = SerializerProvider.GetODataPayloadSerializer(entry.GetType()); if (entrySerializer == null) { throw Error.NotSupported(SRResources.TypeCannotBeSerialized, entry.GetType(), typeof(ODataMediaTypeFormatter).Name); } Contract.Assert(entrySerializer.ODataPayloadKind == ODataPayloadKind.Entry); entrySerializer.WriteObjectInline(entry, writer, writeContext); } writer.WriteEnd(); } }
private void WriteFeedElements(IExpandedResult expanded, IEnumerator elements, ResourceType expectedType, string title, Uri relativeUri, Uri absoluteUri, bool hasMoved, bool topLevel) { ODataFeed feed = new ODataFeed { Id = absoluteUri.AbsoluteUri }; AtomFeedMetadata annotation = new AtomFeedMetadata(); feed.SetAnnotation<AtomFeedMetadata>(annotation); AtomTextConstruct construct = new AtomTextConstruct { Text = title }; annotation.Title = construct; AtomLinkMetadata metadata2 = new AtomLinkMetadata { Href = relativeUri, Title = title }; annotation.SelfLink = metadata2; bool flag = false; if (topLevel && (base.RequestDescription.CountOption == RequestQueryCountOption.Inline)) { flag = this.contentFormat == ODataFormat.VerboseJson; if (!flag) { feed.Count = new long?(base.RequestDescription.CountValue); } } this.odataWriter.WriteStart(feed); try { object lastObject = null; IExpandedResult skipTokenExpandedResult = null; while (hasMoved) { object current = elements.Current; IExpandedResult skipToken = base.GetSkipToken(expanded); if (current != null) { IExpandedResult result3 = current as IExpandedResult; if (result3 != null) { expanded = result3; current = Serializer.GetExpandedElement(expanded); skipToken = base.GetSkipToken(expanded); } this.WriteEntry(expanded, current, true, expectedType); } hasMoved = elements.MoveNext(); lastObject = current; skipTokenExpandedResult = skipToken; } if (flag) { feed.Count = new long?(base.RequestDescription.CountValue); } if (base.NeedNextPageLink(elements)) { feed.NextPageLink = base.GetNextLinkUri(lastObject, skipTokenExpandedResult, absoluteUri); } } finally { if (!topLevel) { WebUtil.Dispose(elements); } } this.odataWriter.WriteEnd(); }