internal static void ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, object resource,
            ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext, AssembliesResolver assembliesResolver)
        {
            IEdmProperty edmProperty = resourceType.FindProperty(property.Name);

            bool isDynamicProperty = false;
            string propertyName = property.Name;
            if (edmProperty != null)
            {
                propertyName = EdmLibHelpers.GetClrPropertyName(edmProperty, readContext.Model);
            }
            else
            {
                IEdmStructuredType structuredType = resourceType.StructuredDefinition();
                isDynamicProperty = structuredType != null && structuredType.IsOpen;
            }

            // dynamic properties have null values
            IEdmTypeReference propertyType = edmProperty != null ? edmProperty.Type : null;

            EdmTypeKind propertyKind;
            object value = ConvertValue(property.Value, ref propertyType, deserializerProvider, readContext,
                out propertyKind);

            if (isDynamicProperty)
            {
                SetDynamicProperty(resource, resourceType, propertyKind, propertyName, value, propertyType,
                    readContext, assembliesResolver);
            }
            else
            {
                SetDeclaredProperty(resource, propertyKind, propertyName, value, edmProperty, readContext, assembliesResolver);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ODataEdmTypeDeserializer"/> class.
        /// </summary>
        /// <param name="payloadKind">The kind of OData payload this deserializer handles.</param>
        /// <param name="deserializerProvider">The <see cref="ODataDeserializerProvider"/>.</param>
        protected ODataEdmTypeDeserializer(ODataPayloadKind payloadKind, ODataDeserializerProvider deserializerProvider)
            : this(payloadKind)
        {
            if (deserializerProvider == null)
            {
                throw Error.ArgumentNull("deserializerProvider");
            }

            DeserializerProvider = deserializerProvider;
        }
        internal static void ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, object resource,
                                           ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext)
        {
            IEdmProperty edmProperty = resourceType.FindProperty(property.Name);

            bool   isDynamicProperty = false;
            string propertyName      = property.Name;

            if (edmProperty != null)
            {
                propertyName = readContext.Model.GetClrPropertyName(edmProperty);
            }
            else
            {
                IEdmStructuredType structuredType = resourceType.StructuredDefinition();
                isDynamicProperty = structuredType != null && structuredType.IsOpen;
            }

            if (!isDynamicProperty && edmProperty == null)
            {
                throw new ODataException(
                          Error.Format(SRResources.CannotDeserializeUnknownProperty, property.Name, resourceType.Definition));
            }

            // dynamic properties have null values
            IEdmTypeReference propertyType = edmProperty != null ? edmProperty.Type : null;

            EdmTypeKind propertyKind;
            object      value = ConvertValue(property.Value, ref propertyType, deserializerProvider, readContext,
                                             out propertyKind);

            if (isDynamicProperty)
            {
                SetDynamicProperty(resource, resourceType, propertyKind, propertyName, value, propertyType,
                                   readContext.Model);
            }
            else
            {
                SetDeclaredProperty(resource, propertyKind, propertyName, value, edmProperty, readContext);
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ODataCollectionDeserializer"/> class.
 /// </summary>
 /// <param name="deserializerProvider">The deserializer provider to use to read inner objects.</param>
 public ODataCollectionDeserializer(ODataDeserializerProvider deserializerProvider)
     : base(ODataPayloadKind.Collection, deserializerProvider)
 {
 }
        private static object ConvertEnumValue(ODataEnumValue enumValue, ref IEdmTypeReference propertyType,
            ODataDeserializerProvider 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();
            }

            ODataEdmTypeDeserializer deserializer = deserializerProvider.GetEdmTypeDeserializer(edmEnumType);
            return deserializer.ReadInline(enumValue, propertyType, readContext);
        }
        private static object ConvertCollectionValue(ODataCollectionValue collection,
            ref IEdmTypeReference propertyType, ODataDeserializerProvider 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.");
            }

            ODataEdmTypeDeserializer deserializer = deserializerProvider.GetEdmTypeDeserializer(collectionType);
            return deserializer.ReadInline(collection, collectionType, readContext);
        }
        internal static object ConvertValue(object oDataValue, ref IEdmTypeReference propertyType, ODataDeserializerProvider deserializerProvider,
            ODataDeserializerContext readContext, out EdmTypeKind typeKind)
        {
            if (oDataValue == null)
            {
                typeKind = EdmTypeKind.None;
                return null;
            }

            ODataComplexValue complexValue = oDataValue as ODataComplexValue;
            if (complexValue != null)
            {
                typeKind = EdmTypeKind.Complex;
                return ConvertComplexValue(complexValue, ref propertyType, deserializerProvider, readContext);
            }

            ODataEnumValue enumValue = oDataValue as ODataEnumValue;
            if (enumValue != null)
            {
                typeKind = EdmTypeKind.Enum;
                return ConvertEnumValue(enumValue, ref propertyType, deserializerProvider, readContext);
            }

            ODataCollectionValue collection = oDataValue as ODataCollectionValue;
            if (collection != null)
            {
                typeKind = EdmTypeKind.Collection;
                return ConvertCollectionValue(collection, ref propertyType, deserializerProvider, readContext);
            }
            
            typeKind = EdmTypeKind.Primitive;
            return oDataValue;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ODataResourceSetDeserializer"/> class.
 /// </summary>
 /// <param name="deserializerProvider">The deserializer provider to use to read inner objects.</param>
 public ODataResourceSetDeserializer(ODataDeserializerProvider deserializerProvider)
     : base(ODataPayloadKind.ResourceSet, deserializerProvider)
 {
 }
        private static object ConvertCollectionValue(ODataCollectionValue collection,
                                                     ref IEdmTypeReference propertyType, ODataDeserializerProvider 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.");
            }

            ODataEdmTypeDeserializer deserializer = deserializerProvider.GetEdmTypeDeserializer(collectionType);

            return(deserializer.ReadInline(collection, collectionType, readContext));
        }
        internal static object ConvertValue(object oDataValue, ref IEdmTypeReference propertyType, ODataDeserializerProvider deserializerProvider,
                                            ODataDeserializerContext readContext, out EdmTypeKind typeKind)
        {
            if (oDataValue == null)
            {
                typeKind = EdmTypeKind.None;
                return(null);
            }

            ODataEnumValue enumValue = oDataValue as ODataEnumValue;

            if (enumValue != null)
            {
                typeKind = EdmTypeKind.Enum;
                return(ConvertEnumValue(enumValue, ref propertyType, deserializerProvider, readContext));
            }

            ODataCollectionValue collection = oDataValue as ODataCollectionValue;

            if (collection != null)
            {
                typeKind = EdmTypeKind.Collection;
                return(ConvertCollectionValue(collection, ref propertyType, deserializerProvider, readContext));
            }

            ODataUntypedValue untypedValue = oDataValue as ODataUntypedValue;

            if (untypedValue != null)
            {
                Contract.Assert(!String.IsNullOrEmpty(untypedValue.RawValue));

                if (untypedValue.RawValue.StartsWith("[", StringComparison.Ordinal) ||
                    untypedValue.RawValue.StartsWith("{", StringComparison.Ordinal))
                {
                    throw new ODataException(Error.Format(SRResources.InvalidODataUntypedValue, untypedValue.RawValue));
                }

                oDataValue = ConvertPrimitiveValue(untypedValue.RawValue);
            }

            typeKind = EdmTypeKind.Primitive;
            return(oDataValue);
        }
Esempio n. 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ODataActionPayloadDeserializer"/> class.
 /// </summary>
 /// <param name="deserializerProvider">The deserializer provider to use to read inner objects.</param>
 public ODataActionPayloadDeserializer(ODataDeserializerProvider deserializerProvider)
     : base(ODataPayloadKind.Parameter)
 {
     DeserializerProvider = deserializerProvider ?? throw new ArgumentNullException(nameof(deserializerProvider));
 }
Esempio n. 12
0
        internal static object ConvertValue(object oDataValue, ref IEdmTypeReference propertyType, ODataDeserializerProvider deserializerProvider,
                                            ODataDeserializerContext readContext, out EdmTypeKind typeKind)
        {
            if (oDataValue == null)
            {
                typeKind = EdmTypeKind.None;
                return(null);
            }

            ODataComplexValue complexValue = oDataValue as ODataComplexValue;

            if (complexValue != null)
            {
                typeKind = EdmTypeKind.Complex;
                return(ConvertComplexValue(complexValue, ref propertyType, deserializerProvider, readContext));
            }

            ODataEnumValue enumValue = oDataValue as ODataEnumValue;

            if (enumValue != null)
            {
                typeKind = EdmTypeKind.Enum;
                return(ConvertEnumValue(enumValue, ref propertyType, deserializerProvider, readContext));
            }

            ODataCollectionValue collection = oDataValue as ODataCollectionValue;

            if (collection != null)
            {
                typeKind = EdmTypeKind.Collection;
                return(ConvertCollectionValue(collection, ref propertyType, deserializerProvider, readContext));
            }

            typeKind = EdmTypeKind.Primitive;
            return(oDataValue);
        }