Exemple #1
0
        public static object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference)
        {
            Exception       exception;
            ExpressionToken token;

            ExceptionUtils.CheckArgumentNotNull <string>(value, "value");
            if ((typeReference != null) && (model == null))
            {
                throw new ODataException(Microsoft.Data.OData.Strings.ODataUriUtils_ConvertFromUriLiteralTypeRefWithoutModel);
            }
            if (model == null)
            {
                model = EdmCoreModel.Instance;
            }
            ExpressionLexer lexer = new ExpressionLexer(value, false);

            if (!lexer.TryPeekNextToken(out token, out exception))
            {
                return(ODataUriConversionUtils.ConvertFromComplexOrCollectionValue(value, version, model, typeReference));
            }
            object primitiveValue = lexer.ReadLiteralToken();

            if (typeReference != null)
            {
                primitiveValue = ODataUriConversionUtils.VerifyAndCoerceUriPrimitiveLiteral(primitiveValue, model, typeReference, version);
            }
            if (primitiveValue is ISpatial)
            {
                ODataVersionChecker.CheckSpatialValue(version);
            }
            return(primitiveValue);
        }
Exemple #2
0
        public static string ConvertToUriLiteral(object value, ODataVersion version, IEdmModel model)
        {
            if (value == null)
            {
                value = new ODataUriNullValue();
            }
            if (model == null)
            {
                model = EdmCoreModel.Instance;
            }
            ODataUriNullValue nullValue = value as ODataUriNullValue;

            if (nullValue != null)
            {
                return(ODataUriConversionUtils.ConvertToUriNullValue(nullValue, model));
            }
            ODataCollectionValue collectionValue = value as ODataCollectionValue;

            if (collectionValue != null)
            {
                return(ODataUriConversionUtils.ConvertToUriCollectionLiteral(collectionValue, model, version));
            }
            ODataComplexValue complexValue = value as ODataComplexValue;

            if (complexValue != null)
            {
                return(ODataUriConversionUtils.ConvertToUriComplexLiteral(complexValue, model, version));
            }
            return(ODataUriConversionUtils.ConvertToUriPrimitiveLiteral(value, version));
        }
Exemple #3
0
        /// <summary>
        /// Converts the given <paramref name="value"/> to a corresponding CLR type. Expects the
        /// <paramref name="value"/> to have already been properly unescaped from an actual Uri.
        /// </summary>
        /// <param name="value">Value from a Uri to be converted.</param>
        /// <param name="version">Version to be compliant with.</param>
        /// <param name="model">Optional model to perform verification against.</param>
        /// <param name="typeReference">Optional IEdmTypeReference to perform verification against.
        ///  Callers must provide a <paramref name="model"/> containing this type if it is specified.</param>
        /// <returns>A CLR object that the <paramref name="value"/> represents.</returns>
        public static object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference)
        {
            ExceptionUtils.CheckArgumentNotNull(value, "value");
            if (typeReference != null && model == null)
            {
                throw new ODataException(ODataErrorStrings.ODataUriUtils_ConvertFromUriLiteralTypeRefWithoutModel);
            }

            if (model == null)
            {
                model = Microsoft.Data.Edm.Library.EdmCoreModel.Instance;
            }

            // Let ExpressionLexer try to get a primitive
            ExpressionLexer lexer = new ExpressionLexer(value, false /*moveToFirstToken*/, false /*useSemicolonDelimeter*/);
            Exception       error;
            ExpressionToken token;

            lexer.TryPeekNextToken(out token, out error);

            if (token.Kind == ExpressionTokenKind.BracketedExpression)
            {
                // Should be a complex or collection value
                return(ODataUriConversionUtils.ConvertFromComplexOrCollectionValue(value, version, model, typeReference));
            }

            object result = lexer.ReadLiteralToken();

            // If we have a typeReference then perform verification and convert if necessary
            if (typeReference != null)
            {
                result = ODataUriConversionUtils.VerifyAndCoerceUriPrimitiveLiteral(result, model, typeReference, version);
            }

            if (result is ISpatial)
            {
                ODataVersionChecker.CheckSpatialValue(version);
            }

            return(result);
        }