/// <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(EdmPrimitiveHelpers.ConvertPrimitiveValue(node != null ? node.Value : graph, clrType)); } // Resource, ResourceSet, Entity Reference or collection of entity reference return(ConvertResourceOrResourceSet(graph, edmTypeReference, readContext)); }
internal static object ConvertTo(string valueString, Type type) { if (valueString == null) { return(null); } if (TypeHelper.IsNullable(type) && String.Equals(valueString, "null", StringComparison.Ordinal)) { return(null); } // TODO 1668: ODL beta1's ODataUriUtils.ConvertFromUriLiteral does not support converting uri literal // to ODataEnumValue, but beta1's ODataUriUtils.ConvertToUriLiteral supports converting ODataEnumValue // to uri literal. if (TypeHelper.IsEnum(type)) { string[] values = valueString.Split(new[] { '\'' }, StringSplitOptions.None); if (values.Length == 3 && String.IsNullOrEmpty(values[2])) { // Remove the type name if the enum value is a fully qualified literal. valueString = values[1]; } Type enumType = TypeHelper.GetUnderlyingTypeOrSelf(type); object[] parameters = new[] { valueString, Enum.ToObject(enumType, 0) }; bool isSuccessful = (bool)EnumTryParseMethod.MakeGenericMethod(enumType).Invoke(null, parameters); if (!isSuccessful) { throw Error.InvalidOperation(SRResources.ModelBinderUtil_ValueCannotBeEnum, valueString, type.Name); } return(parameters[1]); } // The logic of "public static object ConvertFromUriLiteral(string value, ODataVersion version);" treats // the date value string (for example: 2015-01-02) as DateTimeOffset literal, and return a DateTimeOffset // object. However, the logic of // "object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference);" // can return the correct Date object. if (type == typeof(Date) || type == typeof(Date?)) { EdmCoreModel model = EdmCoreModel.Instance; IEdmPrimitiveTypeReference dateTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(type); return(ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4, model, dateTypeReference)); } object value; try { value = ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4); } catch { if (type == typeof(string)) { return(valueString); } throw; } bool isNonStandardEdmPrimitive; EdmLibHelpers.IsNonstandardEdmPrimitive(type, out isNonStandardEdmPrimitive); if (isNonStandardEdmPrimitive) { return(EdmPrimitiveHelpers.ConvertPrimitiveValue(value, type)); } else { type = Nullable.GetUnderlyingType(type) ?? type; return(System.Convert.ChangeType(value, type, CultureInfo.InvariantCulture)); } }