private static object ConvertEnumValue(ODataEnumValue enumValue, ref IEdmTypeReference propertyType, IODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { IEdmEnumTypeReference edmEnumType; if (propertyType == null) { // dynamic enum property Contract.Assert(!String.IsNullOrEmpty(enumValue.TypeName), "ODataLib should have verified that dynamic enum value has a type name since we provided metadata."); IEdmModel model = readContext.Model; IEdmType edmType = model.FindType(enumValue.TypeName); Contract.Assert(edmType.TypeKind == EdmTypeKind.Enum, "ODataLib should have verified that enum value has a enum resource type."); edmEnumType = new EdmEnumTypeReference(edmType as IEdmEnumType, isNullable: true); propertyType = edmEnumType; } else { edmEnumType = propertyType.AsEnum(); } IODataEdmTypeDeserializer deserializer = deserializerProvider.GetEdmTypeDeserializer(edmEnumType); return(deserializer.ReadInline(enumValue, propertyType, readContext)); }
/// <summary> /// Deserializes the given <paramref name="collectionValue"/> under the given <paramref name="readContext"/>. /// </summary> /// <param name="collectionValue">The <see cref="ODataCollectionValue"/> to deserialize.</param> /// <param name="elementType">The element type of the collection to read.</param> /// <param name="readContext">The deserializer context.</param> /// <returns>The deserialized collection.</returns> public virtual IEnumerable ReadCollectionValue(ODataCollectionValue collectionValue, IEdmTypeReference elementType, ODataDeserializerContext readContext) { if (collectionValue == null) { throw Error.ArgumentNull("collectionValue"); } if (elementType == null) { throw Error.ArgumentNull("elementType"); } IODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(elementType); if (deserializer == null) { throw new SerializationException( Error.Format(SRResources.TypeCannotBeDeserialized, elementType.FullName())); } foreach (object item in collectionValue.Items) { if (elementType.IsPrimitive()) { yield return(item); } else { yield return(deserializer.ReadInline(item, elementType, readContext)); } } }
private static object ConvertCollectionValue(ODataCollectionValue collection, ref IEdmTypeReference propertyType, IODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext) { IEdmCollectionTypeReference collectionType; if (propertyType == null) { // dynamic collection property Contract.Assert(!String.IsNullOrEmpty(collection.TypeName), "ODataLib should have verified that dynamic collection value has a type name " + "since we provided metadata."); string elementTypeName = GetCollectionElementTypeName(collection.TypeName, isNested: false); IEdmModel model = readContext.Model; IEdmSchemaType elementType = model.FindType(elementTypeName); Contract.Assert(elementType != null); collectionType = new EdmCollectionTypeReference( new EdmCollectionType(elementType.ToEdmTypeReference(isNullable: false))); propertyType = collectionType; } else { collectionType = propertyType as IEdmCollectionTypeReference; Contract.Assert(collectionType != null, "The type for collection must be a IEdmCollectionType."); } IODataEdmTypeDeserializer deserializer = deserializerProvider.GetEdmTypeDeserializer(collectionType); return(deserializer.ReadInline(collection, collectionType, readContext)); }
/// <summary> /// Deserializes the given <paramref name="resource"/> under the given <paramref name="readContext"/>. /// The given resource could be "Added Resource", "Updated Resource" or "Deleted Resource". /// </summary> /// <param name="resource">The Added Updated or Deleted Resource.</param> /// <param name="elementType">The element type.</param> /// <param name="readContext">The deserializer context.</param> /// <returns>The created object.</returns> public virtual object ReadDeltaResource(ODataResourceWrapper resource, IEdmStructuredTypeReference elementType, ODataDeserializerContext readContext) { if (resource == null) { throw Error.ArgumentNull(nameof(resource)); } if (readContext == null) { throw Error.ArgumentNull(nameof(readContext)); } IODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(elementType); if (deserializer == null) { throw new SerializationException( Error.Format(SRResources.TypeCannotBeDeserialized, elementType.FullName())); } ODataDeserializerContext resourceReadContext = readContext.CloneWithoutType(); if (readContext.IsNoClrType) // check the clr type status from parent context. { if (resource.IsDeletedResource) { resourceReadContext.ResourceType = typeof(EdmDeltaDeletedResourceObject); } else { resourceReadContext.ResourceType = typeof(EdmEntityObject); } } else { Type clrType = readContext.Model.GetClrType(elementType); if (clrType == null) { throw new ODataException( Error.Format(SRResources.MappingDoesNotContainResourceType, elementType.FullName())); } if (resource.IsDeletedResource) { resourceReadContext.ResourceType = typeof(DeltaDeletedResource <>).MakeGenericType(clrType); } else { resourceReadContext.ResourceType = typeof(Delta <>).MakeGenericType(clrType); } } return(deserializer.ReadInline(resource, elementType, resourceReadContext)); }
private object ReadNestedResourceInline(ODataResourceWrapper resourceWrapper, IEdmTypeReference edmType, ODataDeserializerContext readContext) { Contract.Assert(edmType != null); Contract.Assert(readContext != null); if (resourceWrapper == null) { return(null); } IODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(edmType); if (deserializer == null) { throw new SerializationException(Error.Format(SRResources.TypeCannotBeDeserialized, edmType.FullName())); } IEdmStructuredTypeReference structuredType = edmType.AsStructured(); var nestedReadContext = new ODataDeserializerContext { Path = readContext.Path, Model = readContext.Model, Request = readContext.Request, TimeZone = readContext.TimeZone }; Type clrType; if (readContext.IsNoClrType) { clrType = structuredType.IsEntity() ? typeof(EdmEntityObject) : typeof(EdmComplexObject); } else { clrType = readContext.Model.GetClrType(structuredType); if (clrType == null) { throw new ODataException( Error.Format(SRResources.MappingDoesNotContainResourceType, structuredType.FullName())); } } nestedReadContext.ResourceType = readContext.IsDeltaOfT ? typeof(Delta <>).MakeGenericType(clrType) : clrType; return(deserializer.ReadInline(resourceWrapper, edmType, nestedReadContext)); }
private object ReadNestedResourceSetInline(ODataResourceSetWrapper resourceSetWrapper, IEdmTypeReference edmType, ODataDeserializerContext readContext) { Contract.Assert(resourceSetWrapper != null); Contract.Assert(edmType != null); Contract.Assert(readContext != null); IODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(edmType); if (deserializer == null) { throw new SerializationException(Error.Format(SRResources.TypeCannotBeDeserialized, edmType.FullName())); } IEdmStructuredTypeReference structuredType = edmType.AsCollection().ElementType().AsStructured(); ODataDeserializerContext nestedReadContext = readContext.CloneWithoutType(); if (readContext.IsNoClrType) { if (structuredType.IsEntity()) { nestedReadContext.ResourceType = typeof(EdmEntityObjectCollection); } else { nestedReadContext.ResourceType = typeof(EdmComplexObjectCollection); } } else { Type clrType = readContext.Model.GetClrType(structuredType); if (clrType == null) { throw new ODataException( Error.Format(SRResources.MappingDoesNotContainResourceType, structuredType.FullName())); } nestedReadContext.ResourceType = typeof(List <>).MakeGenericType(clrType); } return(deserializer.ReadInline(resourceSetWrapper, edmType, nestedReadContext)); }
/// <summary> /// Deserializes the given <paramref name="resourceSet"/> under the given <paramref name="readContext"/>. /// </summary> /// <param name="resourceSet">The resource set to deserialize.</param> /// <param name="readContext">The deserializer context.</param> /// <param name="elementType">The element type of the resource set being read.</param> /// <returns>The deserialized resource set object.</returns> public virtual IEnumerable ReadResourceSet(ODataResourceSetWrapper resourceSet, IEdmStructuredTypeReference elementType, ODataDeserializerContext readContext) { if (resourceSet == null) { throw Error.ArgumentNull(nameof(resourceSet)); } IODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(elementType); if (deserializer == null) { throw new SerializationException( Error.Format(SRResources.TypeCannotBeDeserialized, elementType.FullName())); } foreach (ODataResourceWrapper resourceWrapper in resourceSet.Resources) { yield return(deserializer.ReadInline(resourceWrapper, elementType, readContext)); } }
private void ApplyNestedDeltaResourceSet(IEdmProperty nestedProperty, object resource, ODataDeltaResourceSetWrapper deltaResourceSetWrapper, ODataDeserializerContext readContext) { Contract.Assert(nestedProperty != null); Contract.Assert(resource != null); Contract.Assert(readContext != null); IEdmTypeReference edmType = nestedProperty.Type; IODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(edmType, true); if (deserializer == null) { throw new SerializationException(Error.Format(SRResources.TypeCannotBeDeserialized, edmType.FullName())); } IEdmStructuredTypeReference structuredType = edmType.AsCollection().ElementType().AsStructured(); ODataDeserializerContext nestedReadContext = readContext.CloneWithoutType(); if (readContext.IsNoClrType) { nestedReadContext.ResourceType = typeof(EdmChangedObjectCollection); } else { Type clrType = readContext.Model.GetClrType(structuredType); if (clrType == null) { throw new ODataException( Error.Format(SRResources.MappingDoesNotContainResourceType, structuredType.FullName())); } nestedReadContext.ResourceType = typeof(DeltaSet <>).MakeGenericType(clrType); } object value = deserializer.ReadInline(deltaResourceSetWrapper, edmType, nestedReadContext); string propertyName = readContext.Model.GetClrPropertyName(nestedProperty); DeserializationHelpers.SetCollectionProperty(resource, nestedProperty, value, propertyName); }
/// <summary> /// Deserializes the given <paramref name="resourceWrapper"/> under the given <paramref name="readContext"/>. /// </summary> /// <param name="resourceWrapper">The OData resource to deserialize.</param> /// <param name="structuredType">The type of the resource to deserialize.</param> /// <param name="readContext">The deserializer context.</param> /// <returns>The deserialized resource.</returns> public virtual object ReadResource(ODataResourceWrapper resourceWrapper, IEdmStructuredTypeReference structuredType, ODataDeserializerContext readContext) { if (resourceWrapper == null) { throw new ArgumentNullException(nameof(resourceWrapper)); } if (readContext == null) { throw new ArgumentNullException(nameof(readContext)); } if (!String.IsNullOrEmpty(resourceWrapper.Resource.TypeName) && structuredType.FullName() != resourceWrapper.Resource.TypeName && resourceWrapper.Resource.TypeName != "Edm.Untyped") { // received a derived type in a base type deserializer. delegate it to the appropriate derived type deserializer. IEdmModel model = readContext.Model; if (model == null) { throw Error.Argument("readContext", SRResources.ModelMissingFromReadContext); } IEdmStructuredType actualType = model.FindType(resourceWrapper.Resource.TypeName) as IEdmStructuredType; if (actualType == null) { throw new ODataException(Error.Format(SRResources.ResourceTypeNotInModel, resourceWrapper.Resource.TypeName)); } if (actualType.IsAbstract) { string message = Error.Format(SRResources.CannotInstantiateAbstractResourceType, resourceWrapper.Resource.TypeName); throw new ODataException(message); } IEdmTypeReference actualStructuredType; IEdmEntityType actualEntityType = actualType as IEdmEntityType; if (actualEntityType != null) { actualStructuredType = new EdmEntityTypeReference(actualEntityType, isNullable: false); } else { actualStructuredType = new EdmComplexTypeReference(actualType as IEdmComplexType, isNullable: false); } IODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(actualStructuredType); if (deserializer == null) { throw new SerializationException( Error.Format(SRResources.TypeCannotBeDeserialized, actualEntityType.FullName())); } object resource = deserializer.ReadInline(resourceWrapper, actualStructuredType, readContext); EdmStructuredObject structuredObject = resource as EdmStructuredObject; if (structuredObject != null) { structuredObject.ExpectedEdmType = structuredType.StructuredDefinition(); } return(resource); } else { object resource = CreateResourceInstance(structuredType, readContext); ApplyResourceProperties(resource, resourceWrapper, structuredType, readContext); ApplyDeletedResource(resource, resourceWrapper, readContext); return(resource); } }
public override async Task <object> ReadAsync(ODataMessageReader messageReader, Type type, ODataDeserializerContext readContext) { if (messageReader == null) { throw Error.ArgumentNull("messageReader"); } IEdmAction action = GetAction(readContext); Contract.Assert(action != null); // Create the correct resource type; Dictionary <string, object> payload; if (type == typeof(ODataActionParameters)) { payload = new ODataActionParameters(); } else { payload = new ODataUntypedActionParameters(action); } ODataParameterReader reader = await messageReader.CreateODataParameterReaderAsync(action).ConfigureAwait(false); while (await reader.ReadAsync().ConfigureAwait(false)) { string parameterName = null; IEdmOperationParameter parameter = null; switch (reader.State) { case ODataParameterReaderState.Value: parameterName = reader.Name; parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName); // ODataLib protects against this but asserting just in case. Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName)); if (parameter.Type.IsPrimitive()) { payload[parameterName] = reader.Value; } else { IODataEdmTypeDeserializer deserializer = DeserializerProvider.GetEdmTypeDeserializer(parameter.Type); payload[parameterName] = deserializer.ReadInline(reader.Value, parameter.Type, readContext); } break; case ODataParameterReaderState.Collection: parameterName = reader.Name; parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName); // ODataLib protects against this but asserting just in case. Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName)); IEdmCollectionTypeReference collectionType = parameter.Type as IEdmCollectionTypeReference; Contract.Assert(collectionType != null); ODataCollectionValue value = await ODataCollectionDeserializer .ReadCollectionAsync(reader.CreateCollectionReader()).ConfigureAwait(false); ODataCollectionDeserializer collectionDeserializer = (ODataCollectionDeserializer)DeserializerProvider.GetEdmTypeDeserializer(collectionType); payload[parameterName] = collectionDeserializer.ReadInline(value, collectionType, readContext); break; case ODataParameterReaderState.Resource: parameterName = reader.Name; parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName); Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName)); Contract.Assert(parameter.Type.IsStructured()); ODataReader resourceReader = reader.CreateResourceReader(); object item = await resourceReader.ReadResourceOrResourceSetAsync().ConfigureAwait(false); ODataResourceDeserializer resourceDeserializer = (ODataResourceDeserializer)DeserializerProvider.GetEdmTypeDeserializer(parameter.Type); payload[parameterName] = resourceDeserializer.ReadInline(item, parameter.Type, readContext); break; case ODataParameterReaderState.ResourceSet: parameterName = reader.Name; parameter = action.Parameters.SingleOrDefault(p => p.Name == parameterName); Contract.Assert(parameter != null, String.Format(CultureInfo.InvariantCulture, "Parameter '{0}' not found.", parameterName)); IEdmCollectionTypeReference resourceSetType = parameter.Type as IEdmCollectionTypeReference; Contract.Assert(resourceSetType != null); ODataReader resourceSetReader = reader.CreateResourceSetReader(); object feed = await resourceSetReader.ReadResourceOrResourceSetAsync().ConfigureAwait(false); ODataResourceSetDeserializer resourceSetDeserializer = (ODataResourceSetDeserializer)DeserializerProvider.GetEdmTypeDeserializer(resourceSetType); object result = resourceSetDeserializer.ReadInline(feed, resourceSetType, readContext); IEdmTypeReference elementTypeReference = resourceSetType.ElementType(); Contract.Assert(elementTypeReference.IsStructured()); IEnumerable enumerable = result as IEnumerable; if (enumerable != null) { if (readContext.IsNoClrType) { payload[parameterName] = enumerable.ConvertToEdmObject(resourceSetType); } else { Type elementClrType = readContext.Model.GetClrType(elementTypeReference); IEnumerable castedResult = _castMethodInfo.MakeGenericMethod(elementClrType) .Invoke(null, new[] { result }) as IEnumerable; payload[parameterName] = castedResult; } } break; } } return(payload); }