private static void AddToCollectionCore(this IEnumerable items, IEnumerable collection, Type elementType, IList list, MethodInfo addMethod, AssembliesResolver assembliesResolver)
        {
            bool isNonstandardEdmPrimitiveCollection;

            EdmLibHelpers.IsNonstandardEdmPrimitive(elementType, assembliesResolver, out isNonstandardEdmPrimitiveCollection);

            foreach (object item in items)
            {
                object element = item;

                if (isNonstandardEdmPrimitiveCollection && element != null)
                {
                    // convert non-standard edm primitives if required.
                    element = EdmPrimitiveHelpers.ConvertPrimitiveValue(element, elementType);
                }

                if (list != null)
                {
                    list.Add(element);
                }
                else
                {
                    Contract.Assert(addMethod != null);
                    addMethod.Invoke(collection, new object[] { element });
                }
            }
        }
        internal static void SetDeclaredProperty(object resource, EdmTypeKind propertyKind, string propertyName,
                                                 object propertyValue, IEdmProperty edmProperty, ODataDeserializerContext readContext)
        {
            if (propertyKind == EdmTypeKind.Collection)
            {
                SetCollectionProperty(resource, edmProperty, propertyValue, propertyName);
            }
            else
            {
                var  isUntypedProp = readContext.GetType().GetProperty("IsUntyped", BindingFlags.NonPublic | BindingFlags.Instance);
                bool isUntyped     = (bool)isUntypedProp.GetValue(readContext, null);

                //if (!readContext.IsUntyped)
                if (!isUntyped)
                {
                    if (propertyKind == EdmTypeKind.Primitive)
                    {
                        propertyValue = EdmPrimitiveHelpers.ConvertPrimitiveValue(propertyValue,
                                                                                  GetPropertyType(resource, propertyName));
                    }
                }

                SetProperty(resource, propertyName, propertyValue);
            }
        }
Ejemplo n.º 3
0
        internal static void ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, object resource, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext)
        {
            IEdmProperty edmProperty = resourceType.FindProperty(property.Name);

            string            propertyName = property.Name;
            IEdmTypeReference propertyType = edmProperty != null ? edmProperty.Type : null; // open properties have null values

            // If we are in patch mode and we are deserializing an entity object then we are updating Delta<T> and not T.
            bool isDelta = readContext.IsPatchMode && resourceType.IsEntity();

            if (isDelta && resourceType.AsEntity().Key().Select(key => key.Name).Contains(propertyName))
            {
                return;
            }

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

            if (propertyKind == EdmTypeKind.Collection)
            {
                SetCollectionProperty(resource, propertyName, isDelta, value);
            }
            else
            {
                if (propertyKind == EdmTypeKind.Primitive)
                {
                    value = EdmPrimitiveHelpers.ConvertPrimitiveValue(value, GetPropertyType(resource, propertyName, isDelta));
                }

                SetProperty(resource, propertyName, isDelta, value);
            }
        }
Ejemplo n.º 4
0
        internal static void SetDeclaredProperty(object resource, EdmTypeKind propertyKind, string propertyName,
                                                 object propertyValue, IEdmProperty edmProperty, ODataDeserializerContext readContext)
        {
            if (propertyKind == EdmTypeKind.Collection)
            {
                SetCollectionProperty(resource, edmProperty, propertyValue, propertyName, readContext.TimeZoneInfo);
            }
            else
            {
                if (!readContext.IsUntyped)
                {
                    if (propertyKind == EdmTypeKind.Primitive)
                    {
                        propertyValue = EdmPrimitiveHelpers.ConvertPrimitiveValue(propertyValue,
                                                                                  GetPropertyType(resource, propertyName), readContext.TimeZoneInfo);
                    }
                    else if (propertyKind == EdmTypeKind.Enum)
                    {
                        propertyValue = EnumDeserializationHelpers.ConvertEnumValue(propertyValue,
                                                                                    GetPropertyType(resource, propertyName));
                    }
                }

                SetProperty(resource, propertyName, propertyValue);
            }
        }
Ejemplo n.º 5
0
        internal static void ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, object resource,
                                           ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext)
        {
            IEdmProperty edmProperty = resourceType.FindProperty(property.Name);

            string            propertyName = property.Name;
            IEdmTypeReference propertyType = edmProperty != null ? edmProperty.Type : null; // open properties have null values

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

            if (propertyKind == EdmTypeKind.Collection)
            {
                SetCollectionProperty(resource, edmProperty, value);
            }
            else
            {
                if (propertyKind == EdmTypeKind.Primitive && !readContext.IsUntyped)
                {
                    value = EdmPrimitiveHelpers.ConvertPrimitiveValue(value, GetPropertyType(resource, propertyName));
                }

                SetProperty(resource, propertyName, value);
            }
        }
Ejemplo n.º 6
0
        public void ConvertDateTimeValue_NonStandardPrimitives_DefaultTimeZoneInfo(DateTimeOffset valueToConvert)
        {
            // Arrange & Act
            TimeZoneInfoHelper.TimeZone = null;
            object actual = EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, typeof(DateTime));

            // Assert
            DateTime dt = Assert.IsType <DateTime>(actual);

            Assert.Equal(valueToConvert.LocalDateTime, dt);
        }
        public static void AddToCollection(this IEnumerable items, IEnumerable collection, Type elementType,
                                           Type resourceType, string propertyName, Type propertyType)
        {
            Contract.Assert(items != null);
            Contract.Assert(collection != null);
            Contract.Assert(elementType != null);
            Contract.Assert(resourceType != null);
            Contract.Assert(propertyName != null);
            Contract.Assert(propertyType != null);

            MethodInfo addMethod = null;
            IList      list      = collection as IList;

            if (list == null)
            {
                addMethod = collection.GetType().GetMethod("Add", new Type[] { elementType });
                if (addMethod == null)
                {
                    string message = Error.Format(SRResources.CollectionShouldHaveAddMethod, propertyType.FullName, propertyName, resourceType.FullName);
                    throw new SerializationException(message);
                }
            }
            else if (list.GetType().IsArray)
            {
                string message = Error.Format(SRResources.GetOnlyCollectionCannotBeArray, propertyName, resourceType.FullName);
                throw new SerializationException(message);
            }

            bool isNonstandardEdmPrimitiveCollection;

            EdmLibHelpers.IsNonstandardEdmPrimitive(elementType, out isNonstandardEdmPrimitiveCollection);

            foreach (object item in items)
            {
                object element = item;

                if (isNonstandardEdmPrimitiveCollection && element != null)
                {
                    // convert non-standard edm primitives if required.
                    element = EdmPrimitiveHelpers.ConvertPrimitiveValue(element, elementType);
                }

                if (list != null)
                {
                    list.Add(element);
                }
                else
                {
                    Contract.Assert(addMethod != null);
                    addMethod.Invoke(collection, new object[] { element });
                }
            }
        }
Ejemplo n.º 8
0
        public void ConvertDateTimeValue_NonStandardPrimitives_CustomTimeZoneInfo(DateTimeOffset valueToConvert)
        {
            // Arrange & Act
            var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");

            TimeZoneInfoHelper.TimeZone = timeZone;
            object actual = EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, typeof(DateTime));

            // Assert
            DateTime dt = Assert.IsType <DateTime>(actual);

            Assert.Equal(TimeZoneInfo.ConvertTime(valueToConvert, timeZone).DateTime, dt);
        }
        internal static void ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, object resource,
                                           ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext)
        {
            IEdmProperty edmProperty = resourceType.FindProperty(property.Name);

            // try to deserializer the dynamic properties for open type.
            if (edmProperty == null)
            {
                // the logic here works for open complex type and open entity type.
                IEdmStructuredType structuredType = resourceType.StructuredDefinition();
                if (structuredType != null && structuredType.IsOpen)
                {
                    if (ApplyDynamicProperty(property, structuredType, resource, deserializerProvider, readContext))
                    {
                        return;
                    }
                }
            }

            string propertyName = property.Name;

            if (edmProperty != null)
            {
                propertyName = EdmLibHelpers.GetClrPropertyName(edmProperty, readContext.Model);
            }
            IEdmTypeReference propertyType = edmProperty != null ? edmProperty.Type : null; // open properties have null values

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

            if (propertyKind == EdmTypeKind.Collection)
            {
                SetCollectionProperty(resource, edmProperty, value, propertyName);
            }
            else
            {
                if (!readContext.IsUntyped)
                {
                    if (propertyKind == EdmTypeKind.Primitive)
                    {
                        value = EdmPrimitiveHelpers.ConvertPrimitiveValue(value, GetPropertyType(resource, propertyName));
                    }
                    else if (propertyKind == EdmTypeKind.Enum)
                    {
                        value = EnumDeserializationHelpers.ConvertEnumValue(value, GetPropertyType(resource, propertyName));
                    }
                }

                SetProperty(resource, propertyName, value);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Deserializes the primitive from the given <paramref name="primitiveProperty"/> under the given <paramref name="readContext"/>.
        /// </summary>
        /// <param name="primitiveProperty">The primitive property to deserialize.</param>
        /// <param name="readContext">The deserializer context.</param>
        /// <returns>The deserialized OData primitive value.</returns>
        public virtual object ReadPrimitive(ODataProperty primitiveProperty, ODataDeserializerContext readContext)
        {
            if (primitiveProperty == null)
            {
                throw Error.ArgumentNull("primitiveProperty");
            }

            if (readContext == null)
            {
                throw Error.ArgumentNull("readContext");
            }

            //Try and change the value appropriately if type is specified
            if (readContext.ResourceType != null && primitiveProperty.Value != null)
            {
                return(EdmPrimitiveHelpers.ConvertPrimitiveValue(primitiveProperty.Value, readContext.ResourceType));
            }

            return(primitiveProperty.Value);
        }
Ejemplo n.º 11
0
        internal static Expression DateTimeOffsetToDateTime(Expression expression)
        {
            var unaryExpression = expression as UnaryExpression;

            if (unaryExpression != null)
            {
                if (Nullable.GetUnderlyingType(unaryExpression.Type) == unaryExpression.Operand.Type)
                {
                    // this is a cast from T to Nullable<T> which is redundant.
                    expression = unaryExpression.Operand;
                }
            }
            var parameterizedConstantValue = ExtractParameterizedConstant(expression);
            var dto = parameterizedConstantValue as DateTimeOffset?;

            if (dto != null)
            {
                expression = Expression.Constant(EdmPrimitiveHelpers.ConvertPrimitiveValue(dto.Value, typeof(DateTime)));
            }
            return(expression);
        }
Ejemplo n.º 12
0
        internal static void SetDeclaredProperty(object resource, EdmTypeKind propertyKind, string propertyName,
                                                 object propertyValue, IEdmProperty edmProperty, ODataDeserializerContext readContext, AssembliesResolver assembliesResolver)
        {
            if (propertyKind == EdmTypeKind.Collection)
            {
                SetCollectionProperty(resource, edmProperty, propertyValue, propertyName, assembliesResolver);
            }
            else
            {
                if (!readContext.IsUntyped)
                {
                    if (propertyKind == EdmTypeKind.Primitive)
                    {
                        propertyValue = EdmPrimitiveHelpers.ConvertPrimitiveValue(propertyValue,
                                                                                  GetPropertyType(resource, propertyName));
                    }
                }

                SetProperty(resource, propertyName, propertyValue);
            }
        }
Ejemplo n.º 13
0
        internal static void ApplyProperty(ODataProperty property, IEdmStructuredTypeReference resourceType, object resource, ODataDeserializerProvider deserializerProvider, ODataDeserializerContext readContext)
        {
            IEdmProperty      property1 = resourceType.FindProperty(property.Name);
            string            name      = property.Name;
            IEdmTypeReference type      = property1?.Type;
            EdmTypeKind       typeKind;
            object            obj = DeserializationHelpers.ConvertValue(property.Value, ref type, deserializerProvider, readContext, out typeKind);

            switch (typeKind)
            {
            case EdmTypeKind.Primitive:
                if (!readContext.IsUntyped)
                {
                    obj = EdmPrimitiveHelpers.ConvertPrimitiveValue(obj, DeserializationHelpers.GetPropertyType(resource, name));
                    break;
                }
                break;

            case EdmTypeKind.Collection:
                DeserializationHelpers.SetCollectionProperty(resource, property1, obj);
                return;
            }
            DeserializationHelpers.SetProperty(resource, name, obj);
        }
Ejemplo n.º 14
0
 public void ConvertPrimitiveValueToChar_Throws(string input)
 {
     ExceptionAssert.Throws <ValidationException>(
         () => EdmPrimitiveHelpers.ConvertPrimitiveValue(input, typeof(char)),
         "The value must be a string with a length of 1.");
 }
Ejemplo n.º 15
0
        internal static object ConvertAnnotationValue(object oDataValue, ref IEdmTypeReference propertyType, ODataDeserializerProvider deserializerProvider,
                                                      ODataDeserializerContext readContext)
        {
            if (oDataValue == null)
            {
                return(null);
            }

            ODataEnumValue enumValue = oDataValue as ODataEnumValue;

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

            ODataCollectionValue collection = oDataValue as ODataCollectionValue;

            if (collection != null)
            {
                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);
            }

            ODataResourceValue annotationVal = oDataValue as ODataResourceValue;

            if (annotationVal != null)
            {
                var annotationType = readContext.Model.FindDeclaredType(annotationVal.TypeName).ToEdmTypeReference(true) as IEdmStructuredTypeReference;

                ODataResourceDeserializer deserializer = new ODataResourceDeserializer(deserializerProvider);

                object resource = deserializer.CreateResourceInstance(annotationType, readContext);

                if (resource != null)
                {
                    foreach (var prop in annotationVal.Properties)
                    {
                        deserializer.ApplyStructuralProperty(resource, prop, annotationType, readContext);
                    }
                }

                return(resource);
            }

            ODataPrimitiveValue primitiveValue = oDataValue as ODataPrimitiveValue;

            if (primitiveValue != null)
            {
                return(EdmPrimitiveHelpers.ConvertPrimitiveValue(primitiveValue.Value, primitiveValue.Value.GetType()));
            }

            return(oDataValue);
        }
Ejemplo n.º 16
0
 public void ConvertPrimitiveValueToNullableChar_Throws()
 {
     ExceptionAssert.Throws <ValidationException>(
         () => EdmPrimitiveHelpers.ConvertPrimitiveValue("123", typeof(char?)),
         "The value must be a string with a maximum length of 1.");
 }
Ejemplo n.º 17
0
 public void ConvertPrimitiveValue_NonStandardPrimitives(object valueToConvert, object result, Type conversionType)
 {
     Assert.Equal(result.GetType(), EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, conversionType).GetType());
     Assert.Equal(result.ToString(), EdmPrimitiveHelpers.ConvertPrimitiveValue(valueToConvert, conversionType).ToString());
 }
Ejemplo n.º 18
0
 public void ConvertPrimitiveValueToXElement_Throws_IfInputIsNotString()
 {
     ExceptionAssert.Throws <ValidationException>(
         () => EdmPrimitiveHelpers.ConvertPrimitiveValue(123, typeof(XElement)),
         "The value must be a string.");
 }