Пример #1
0
            internal static object ConvertTo(ODataParameterValue parameterValue, HttpActionContext actionContext, ModelBindingContext bindingContext)
            {
                Contract.Assert(parameterValue != null && parameterValue.EdmType != null);

                object oDataValue = parameterValue.Value;

                if (oDataValue == null || oDataValue is ODataNullValue)
                {
                    return(null);
                }

                IEdmTypeReference        edmTypeReference = parameterValue.EdmType;
                ODataDeserializerContext readContext      = BuildDeserializerContext(actionContext, bindingContext, edmTypeReference);

                // complex value
                ODataComplexValue complexValue = oDataValue as ODataComplexValue;

                if (complexValue != null)
                {
                    IEdmComplexTypeReference edmComplexType = edmTypeReference.AsComplex();
                    Contract.Assert(edmComplexType != null);

                    ODataComplexTypeDeserializer deserializer =
                        (ODataComplexTypeDeserializer)DeserializerProvider.GetEdmTypeDeserializer(edmComplexType);

                    return(deserializer.ReadInline(complexValue, edmComplexType, readContext));
                }

                // collection of primitive, enum, complex
                ODataCollectionValue collectionValue = oDataValue as ODataCollectionValue;

                if (collectionValue != null)
                {
                    return(ConvertCollection(collectionValue, edmTypeReference, bindingContext, readContext));
                }

                // enum value
                ODataEnumValue enumValue = oDataValue as ODataEnumValue;

                if (enumValue != null)
                {
                    IEdmEnumTypeReference edmEnumType = edmTypeReference.AsEnum();
                    Contract.Assert(edmEnumType != null);

                    ODataEnumDeserializer deserializer =
                        (ODataEnumDeserializer)DeserializerProvider.GetEdmTypeDeserializer(edmEnumType);

                    return(deserializer.ReadInline(enumValue, edmEnumType, readContext));
                }

                // primitive value
                if (edmTypeReference.IsPrimitive())
                {
                    return(EdmPrimitiveHelpers.ConvertPrimitiveValue(oDataValue, bindingContext.ModelType));
                }

                // Entity, Feed, Entity Reference or collection of entity reference
                return(ConvertFeedOrEntry(oDataValue, edmTypeReference, readContext));
            }
Пример #2
0
        /// <inheritdoc />
        public override object ReadInline(
            object item,
            IEdmTypeReference edmType,
            ODataDeserializerContext readContext)
        {
            var result = enumDeserializer.ReadInline(item, edmType, readContext);

            var edmEnumObject = result as EdmEnumObject;

            if (edmEnumObject != null)
            {
                return(edmEnumObject.Value);
            }

            return(result);
        }
Пример #3
0
        /// <summary>
        /// Convert an OData value into a CLR object.
        /// </summary>
        /// <param name="graph">The given object.</param>
        /// <param name="edmTypeReference">The EDM type of the given object.</param>
        /// <param name="clrType">The CLR type of the given object.</param>
        /// <param name="parameterName">The parameter name of the given object.</param>
        /// <param name="readContext">The <see cref="ODataDeserializerContext"/> use to convert.</param>
        /// <param name="requestContainer">The dependency injection container for the request.</param>
        /// <returns>The converted object.</returns>
        public static object Convert(object graph, IEdmTypeReference edmTypeReference,
                                     Type clrType, string parameterName, ODataDeserializerContext readContext,
                                     IServiceProvider requestContainer)
        {
            if (graph == null || graph is ODataNullValue)
            {
                return(null);
            }

            // collection of primitive, enum
            ODataCollectionValue collectionValue = graph as ODataCollectionValue;

            if (collectionValue != null)
            {
                return(ConvertCollection(collectionValue, edmTypeReference, clrType, parameterName, readContext,
                                         requestContainer));
            }

            // enum value
            ODataEnumValue enumValue = graph as ODataEnumValue;

            if (enumValue != null)
            {
                IEdmEnumTypeReference edmEnumType = edmTypeReference.AsEnum();
                Contract.Assert(edmEnumType != null);

                ODataDeserializerProvider deserializerProvider =
                    requestContainer.GetRequiredService <ODataDeserializerProvider>();

                ODataEnumDeserializer deserializer =
                    (ODataEnumDeserializer)deserializerProvider.GetEdmTypeDeserializer(edmEnumType);

                return(deserializer.ReadInline(enumValue, edmEnumType, readContext));
            }

            // primitive value
            if (edmTypeReference.IsPrimitive())
            {
                ConstantNode node = graph as ConstantNode;
                return(EdmPrimitiveHelper.ConvertPrimitiveValue(node != null ? node.Value : graph, clrType, readContext.TimeZone));
            }

            // Resource, ResourceSet, Entity Reference or collection of entity reference
            return(ConvertResourceOrResourceSet(graph, edmTypeReference, readContext));
        }