public override object ReadInline(ODataFeed feed, ODataDeserializerContext readContext) { if (readContext == null) { throw Error.ArgumentNull("readContext"); } if (feed == null) { return(null); } ODataEntryDeserializer deserializer = DeserializerProvider.GetODataDeserializer(_edmEntityType); IList feedValue = CreateNewCollection(EdmLibHelpers.GetClrType(_edmEntityType, EdmModel)); ODataFeedAnnotation feedAnnotation = feed.GetAnnotation <ODataFeedAnnotation>(); Contract.Assert(feedAnnotation != null, "Each feed we create should gave annotation on it."); foreach (ODataEntry entry in feedAnnotation) { ODataEntryAnnotation annotation = entry.GetAnnotation <ODataEntryAnnotation>(); Contract.Assert(annotation != null); feedValue.Add(deserializer.ReadInline(entry, readContext)); } return(feedValue); }
private void ApplyEntityProperties(ODataEntry entry, ODataEntryAnnotation entryAnnotation, ODataDeserializerContext readContext) { object entityResource = entryAnnotation.EntityResource; IEdmEntityTypeReference entityType = entryAnnotation.EntityType; ApplyValueProperties(entry, entityType, entityResource, readContext); ApplyNavigationProperties(entryAnnotation, entityType, entityResource, readContext); }
private void ApplyNavigationProperties(ODataEntryAnnotation entryAnnotation, IEdmEntityTypeReference entityType, object entityResource, ODataDeserializerContext readContext) { Contract.Assert(entityType.TypeKind() == EdmTypeKind.Entity, "Only entity types can be specified for entities."); foreach (ODataNavigationLink navigationLink in entryAnnotation) { IEdmNavigationProperty navigationProperty = entityType.FindProperty(navigationLink.Name) as IEdmNavigationProperty; Contract.Assert(navigationProperty != null, "ODataLib reader should have already validated that all navigation properties are declared and none is open."); ApplyNavigationProperty(navigationLink, navigationProperty, entityResource, readContext); } }
private object CreateNestedEntityAndApplyProperties(ODataEntry entry, IEdmEntityTypeReference elementType, ODataDeserializerReadContext readContext) { ODataEntryAnnotation annotation = entry.GetAnnotation <ODataEntryAnnotation>(); Contract.Assert(annotation != null); CreateEntityResource(annotation, elementType, readContext); ODataEntryDeserializer deserializer = DeserializerProvider.GetODataDeserializer(elementType); return(deserializer.ReadInline(entry, readContext)); }
private IEnumerable ReadItems(ODataFeed feed, ODataDeserializerContext readContext) { ODataEntryDeserializer deserializer = DeserializerProvider.GetODataDeserializer(_edmEntityType); ODataFeedAnnotation feedAnnotation = feed.GetAnnotation<ODataFeedAnnotation>(); Contract.Assert(feedAnnotation != null, "Each feed we create should gave annotation on it."); foreach (ODataEntry entry in feedAnnotation) { ODataEntryAnnotation annotation = entry.GetAnnotation<ODataEntryAnnotation>(); Contract.Assert(annotation != null); yield return deserializer.ReadInline(entry, readContext); } }
public override object ReadInline(ODataEntry entry, ODataDeserializerContext readContext) { if (entry == null) { throw Error.Argument("entry", SRResources.ItemMustBeOfType, typeof(ODataEntry).Name); } ODataEntryAnnotation entryAnnotation = entry.GetAnnotation <ODataEntryAnnotation>(); Contract.Assert(entryAnnotation != null); CreateEntityResource(entryAnnotation, EdmEntityType, readContext); RecurseEnter(readContext); ApplyEntityProperties(entry, entryAnnotation, readContext); RecurseLeave(readContext); return(entryAnnotation.EntityResource); }
public override object ReadInline(object item, ODataDeserializerReadContext readContext) { ODataEntry topLevelEntry = item as ODataEntry; if (item == null) { throw Error.Argument("item", SRResources.ItemMustBeOfType, typeof(ODataEntry).Name); } ODataEntryAnnotation topLevelEntryAnnotation = topLevelEntry.GetAnnotation <ODataEntryAnnotation>(); Contract.Assert(topLevelEntryAnnotation != null); RecurseEnter(readContext); ApplyEntityProperties(topLevelEntry, topLevelEntryAnnotation, readContext); RecurseLeave(readContext); return(topLevelEntryAnnotation.EntityResource); }
private void CreateEntityResource(ODataEntryAnnotation entryAnnotation, IEdmEntityTypeReference entityType, ODataDeserializerContext readContext) { Type clrType = EdmLibHelpers.GetClrType(entityType, EdmModel); if (clrType == null) { throw Error.Argument("entityType", SRResources.MappingDoesNotContainEntityType, entityType.FullName()); } object resource; if (!readContext.IsPatchMode) { resource = Activator.CreateInstance(clrType); } else { resource = Activator.CreateInstance(typeof(Delta <>).MakeGenericType(clrType)); } entryAnnotation.EntityResource = resource; entryAnnotation.EntityType = entityType; }
public override object ReadInline(ODataEntry entry, ODataDeserializerContext readContext) { if (entry == null) { throw Error.Argument("entry", SRResources.ItemMustBeOfType, typeof(ODataEntry).Name); } if (EdmEntityType.FullName() != entry.TypeName) { // received a derived type in a base type deserializer. // delegate it to the appropriate derived type deserializer. IEdmEntityType entityType = EdmModel.FindType(entry.TypeName) as IEdmEntityType; Contract.Assert(entityType != null, "edmlib should have already validated that it knows the edm type and is the same as or derives from EdmEntityType"); if (entityType.IsAbstract) { throw Error.InvalidOperation(SRResources.CannotInstantiateAbstractEntityType, entry.TypeName); } ODataEntityDeserializer deserializer = DeserializerProvider.GetODataDeserializer(new EdmEntityTypeReference(entityType, isNullable: false)) as ODataEntityDeserializer; return(deserializer.ReadInline(entry, readContext)); } else { ODataEntryAnnotation entryAnnotation = entry.GetAnnotation <ODataEntryAnnotation>(); Contract.Assert(entryAnnotation != null); CreateEntityResource(entryAnnotation, EdmEntityType, readContext); RecurseEnter(readContext); ApplyEntityProperties(entry, entryAnnotation, readContext); RecurseLeave(readContext); return(entryAnnotation.EntityResource); } }
internal static ODataItem ReadEntryOrFeed(ODataReader odataReader, ODataDeserializerContext readContext) { ODataItem topLevelItem = null; Stack <ODataItem> itemsStack = new Stack <ODataItem>(); while (odataReader.Read()) { switch (odataReader.State) { case ODataReaderState.EntryStart: ODataEntry entry = (ODataEntry)odataReader.Item; ODataEntryAnnotation entryAnnotation = null; if (entry != null) { entryAnnotation = new ODataEntryAnnotation(); entry.SetAnnotation(entryAnnotation); } if (itemsStack.Count == 0) { Contract.Assert(entry != null, "The top-level entry can never be null."); topLevelItem = entry; } else { ODataItem parentItem = itemsStack.Peek(); ODataFeed parentFeed = parentItem as ODataFeed; if (parentFeed != null) { ODataFeedAnnotation parentFeedAnnotation = parentFeed.GetAnnotation <ODataFeedAnnotation>(); Contract.Assert(parentFeedAnnotation != null, "Every feed we added to the stack should have the feed annotation on it."); parentFeedAnnotation.Add(entry); } else { ODataNavigationLink parentNavigationLink = (ODataNavigationLink)parentItem; ODataNavigationLinkAnnotation parentNavigationLinkAnnotation = parentNavigationLink.GetAnnotation <ODataNavigationLinkAnnotation>(); Contract.Assert(parentNavigationLinkAnnotation != null, "Every navigation link we added to the stack should have the navigation link annotation on it."); Contract.Assert(parentNavigationLink.IsCollection == false, "Only singleton navigation properties can contain entry as their child."); Contract.Assert(parentNavigationLinkAnnotation.Count == 0, "Each navigation property can contain only one entry as its direct child."); parentNavigationLinkAnnotation.Add(entry); } } itemsStack.Push(entry); RecurseEnter(readContext); break; case ODataReaderState.EntryEnd: Contract.Assert(itemsStack.Count > 0 && itemsStack.Peek() == odataReader.Item, "The entry which is ending should be on the top of the items stack."); itemsStack.Pop(); RecurseLeave(readContext); break; case ODataReaderState.NavigationLinkStart: ODataNavigationLink navigationLink = (ODataNavigationLink)odataReader.Item; Contract.Assert(navigationLink != null, "Navigation link should never be null."); navigationLink.SetAnnotation(new ODataNavigationLinkAnnotation()); Contract.Assert(itemsStack.Count > 0, "Navigation link can't appear as top-level item."); { ODataEntry parentEntry = (ODataEntry)itemsStack.Peek(); ODataEntryAnnotation parentEntryAnnotation = parentEntry.GetAnnotation <ODataEntryAnnotation>(); Contract.Assert(parentEntryAnnotation != null, "Every entry we added to the stack should have the entry annotation on it."); parentEntryAnnotation.Add(navigationLink); } itemsStack.Push(navigationLink); RecurseEnter(readContext); break; case ODataReaderState.NavigationLinkEnd: Contract.Assert(itemsStack.Count > 0 && itemsStack.Peek() == odataReader.Item, "The navigation link which is ending should be on the top of the items stack."); itemsStack.Pop(); RecurseLeave(readContext); break; case ODataReaderState.FeedStart: ODataFeed feed = (ODataFeed)odataReader.Item; Contract.Assert(feed != null, "Feed should never be null."); feed.SetAnnotation(new ODataFeedAnnotation()); if (itemsStack.Count > 0) { ODataNavigationLink parentNavigationLink = (ODataNavigationLink)itemsStack.Peek(); Contract.Assert(parentNavigationLink != null, "this has to be an inner feed. inner feeds always have a navigation link."); ODataNavigationLinkAnnotation parentNavigationLinkAnnotation = parentNavigationLink.GetAnnotation <ODataNavigationLinkAnnotation>(); Contract.Assert(parentNavigationLinkAnnotation != null, "Every navigation link we added to the stack should have the navigation link annotation on it."); Contract.Assert(parentNavigationLink.IsCollection == true, "Only collection navigation properties can contain feed as their child."); parentNavigationLinkAnnotation.Add(feed); } else { topLevelItem = feed; } itemsStack.Push(feed); RecurseEnter(readContext); break; case ODataReaderState.FeedEnd: Contract.Assert(itemsStack.Count > 0 && itemsStack.Peek() == odataReader.Item, "The feed which is ending should be on the top of the items stack."); itemsStack.Pop(); RecurseLeave(readContext); break; case ODataReaderState.EntityReferenceLink: ODataEntityReferenceLink entityReferenceLink = (ODataEntityReferenceLink)odataReader.Item; Contract.Assert(entityReferenceLink != null, "Entity reference link should never be null."); Contract.Assert(itemsStack.Count > 0, "Entity reference link should never be reported as top-level item."); { ODataNavigationLink parentNavigationLink = (ODataNavigationLink)itemsStack.Peek(); ODataNavigationLinkAnnotation parentNavigationLinkAnnotation = parentNavigationLink.GetAnnotation <ODataNavigationLinkAnnotation>(); Contract.Assert(parentNavigationLinkAnnotation != null, "Every navigation link we added to the stack should have the navigation link annotation on it."); parentNavigationLinkAnnotation.Add(entityReferenceLink); } break; default: Contract.Assert(false, "We should never get here, it means the ODataReader reported a wrong state."); break; } } Contract.Assert(odataReader.State == ODataReaderState.Completed, "We should have consumed all of the input by now."); Contract.Assert(topLevelItem != null, "A top level entry or feed should have been read by now."); return(topLevelItem); }