Exemplo n.º 1
0
        /// <summary>
        /// Converts the given JSON string value to the expected type as per OData conversion rules for JSON values.
        /// </summary>
        /// <param name="stringValue">String value to the converted.</param>
        /// <param name="targetType">Target type to which the string value needs to be converted.</param>
        /// <returns>Object which is in sync with the target type.</returns>
        private static object ConvertStringValue(string stringValue, Type targetType)
        {
            if (targetType == typeof(byte[]))
            {
                return(Convert.FromBase64String(stringValue));
            }

            if (targetType == typeof(Guid))
            {
                return(new Guid(stringValue));
            }

            // Convert.ChangeType does not support TimeSpan.
            if (targetType == typeof(TimeSpan))
            {
                return(XmlConvert.ToTimeSpan(stringValue));
            }

            // DateTime needs to be read using the XML rules (as per the JSON Light spec).
            if (targetType == typeof(DateTime))
            {
                return(PlatformHelper.ConvertStringToDateTime(stringValue));
            }

            // DateTimeOffset needs to be read using the XML rules (as per the JSON Light spec).
            if (targetType == typeof(DateTimeOffset))
            {
                return(PlatformHelper.ConvertStringToDateTimeOffset(stringValue));
            }

            // For string types, we support conversion to all possible primitive types
            return(Convert.ChangeType(stringValue, targetType, CultureInfo.InvariantCulture));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts a string to a primitive type string.
        /// </summary>
        /// <param name="key">Original string value.</param>
        /// <returns>Formatted string</returns>
        public static object KeyStringToPrimitive(string key)
        {
            Match match;

            if (string.IsNullOrEmpty(key))
            {
                return(key);
            }
            else if ((match = DateTimeRegex.Match(key)).Success)
            {
                return(PlatformHelper.ConvertStringToDateTime(Uri.UnescapeDataString(match.Groups[1].Value)));
            }
            else if ((match = GuidRegex.Match(key)).Success)
            {
                return(XmlConvert.ToGuid(match.Groups[1].Value));
            }
            else if ((match = LongRegex.Match(key)).Success)
            {
                return(XmlConvert.ToInt64(match.Groups[1].Value));
            }
            else if ((match = DecimalRegex.Match(key)).Success)
            {
                return(XmlConvert.ToDecimal(match.Groups[1].Value));
            }
            else if (DoubleInfinityRegex.IsMatch(key) || DoubleNanRegex.IsMatch(key))
            {
                return(XmlConvert.ToDouble(key));
            }
            else if ((match = FloatRegex.Match(key)).Success)
            {
                return(XmlConvert.ToSingle(Uri.UnescapeDataString(match.Groups[1].Value)));
            }
            else if ((match = DoubleRegex.Match(key)).Success)
            {
                return(XmlConvert.ToDouble(Uri.UnescapeDataString(match.Groups[1].Value)));
            }
            else if ((match = StringRegex.Match(key)).Success)
            {
                return(match.Groups[1].Value);
            }
            else if ((match = DateTimeOffsetRegex.Match(key)).Success)
            {
                return(XmlConvert.ToDateTimeOffset(Uri.UnescapeDataString(match.Groups[1].Value)));
            }
            else if ((match = TimeSpanRegex.Match(key)).Success)
            {
                return(XmlConvert.ToTimeSpan(Uri.UnescapeDataString(match.Groups[1].Value)));
            }
            else
            {
                throw new TaupoInvalidOperationException("Could not parse key string: '" + key + "'");
            }
        }
Exemplo n.º 3
0
 internal static bool TryParseDateTime(string value, out DateTime?result)
 {
     try
     {
         result = PlatformHelper.ConvertStringToDateTime(value);
         return(true);
     }
     catch (FormatException)
     {
         result = null;
         return(false);
     }
 }
Exemplo n.º 4
0
        internal static bool TryParseDateTime(string value, out DateTime?result)
        {
            bool flag;

            try
            {
                result = new DateTime?(PlatformHelper.ConvertStringToDateTime(value));
                flag   = true;
            }
            catch (FormatException formatException)
            {
                result = null;
                flag   = false;
            }
            return(flag);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Converts the given JSON string value to the expected type as per OData conversion rules for JSON values.
        /// </summary>
        /// <param name="stringValue">String value to the converted.</param>
        /// <param name="targetType">Target type to which the string value needs to be converted.</param>
        /// <param name="version">The version of the payload being read.</param>
        /// <returns>Object which is in sync with the target type.</returns>
        private static object ConvertStringValue(string stringValue, Type targetType, ODataVersion version)
        {
            if (targetType == typeof(byte[]))
            {
                return(Convert.FromBase64String(stringValue));
            }

            if (targetType == typeof(Guid))
            {
                return(new Guid(stringValue));
            }

            // Convert.ChangeType does not support TimeSpan.
            if (targetType == typeof(TimeSpan))
            {
                return(XmlConvert.ToTimeSpan(stringValue));
            }

            // Convert.ChangeType does not support DateTimeOffset.
            // Convert.ChangeType does support DateTime, and hence the ChangeType
            // call below should handle the DateTime case.
            if (targetType == typeof(DateTimeOffset))
            {
                return(XmlConvert.ToDateTimeOffset(stringValue));
            }

            // In Verbose JSON V3 the DateTime fomat is the ISO one, so we need to call XmlConvert instead of Convert
            // Convert doesn't read that value correctly (converts the result to Local kind always).
            if (targetType == typeof(DateTime) && version >= ODataVersion.V3)
            {
                try
                {
                    return(PlatformHelper.ConvertStringToDateTime(stringValue));
                }
                catch (FormatException)
                {
                    // If the XmlConvert fails to convert we need to try Convert.ChangeType on the value anyway
                    // so that we can still read values like MM/DD/YYYY which we supported just fine in V1/V2.
                }
            }

            // For string types, we support conversion to all possible primitive types
            return(Convert.ChangeType(stringValue, targetType, CultureInfo.InvariantCulture));
        }
Exemplo n.º 6
0
 /// <summary>
 /// Create an instance of primitive type from a string representation
 /// </summary>
 /// <param name="text">The string representation</param>
 /// <returns>An instance of primitive type</returns>
 internal override object Parse(String text)
 {
     return(PlatformHelper.ConvertStringToDateTime(text));
 }
Exemplo n.º 7
0
 static DataGenerationHints()
 {
     hintsCreators = new Dictionary <string, Func <string, DataGenerationHint> >()
     {
         { GetHintKey <AnsiStringHint>(), (s) => DataGenerationHints.AnsiString },
         { GetHintKey <NoNullsHint>(), (s) => DataGenerationHints.NoNulls },
         { GetHintKey <EnumMembersOnlyHint>(), (s) => DataGenerationHints.EnumMembersOnly },
         { GetHintKey <AllNullsHint>(), (s) => DataGenerationHints.AllNulls },
         { GetHintKey <AllUniqueHint>(), (s) => DataGenerationHints.AllUnique },
         { GetHintKey <NoSecondsHint>(), (s) => DataGenerationHints.NoSeconds },
         { GetHintKey <NoTimeHint>(), (s) => DataGenerationHints.NoTime },
         { GetHintKey <NoRandomValuesHint>(), (s) => DataGenerationHints.NoRandomValues },
         { GetHintKey <CollectionMaxCountHint>(), (s) => DataGenerationHints.MaxCount(XmlConvert.ToInt32(s)) },
         { GetHintKey <CollectionMinCountHint>(), (s) => DataGenerationHints.MinCount(XmlConvert.ToInt32(s)) },
         { GetHintKey <FractionalDigitsHint>(), (s) => DataGenerationHints.FractionalDigits(XmlConvert.ToInt32(s)) },
         { GetHintKey <FractionalSecondsHint>(), (s) => DataGenerationHints.FractionalSeconds(XmlConvert.ToInt32(s)) },
         { GetHintKey <MaxLengthHint>(), (s) => DataGenerationHints.MaxLength(XmlConvert.ToInt32(s)) },
         { GetHintKey <MinLengthHint>(), (s) => DataGenerationHints.MinLength(XmlConvert.ToInt32(s)) },
         { GetHintKey <NumericPrecisionHint>(), (s) => DataGenerationHints.NumericPrecision(XmlConvert.ToInt32(s)) },
         { GetHintKey <NumericScaleHint>(), (s) => DataGenerationHints.NumericScale(XmlConvert.ToInt32(s)) },
         { GetHintKey <MaxValueHint <int> >(), (s) => DataGenerationHints.MaxValue(XmlConvert.ToInt32(s)) },
         { GetHintKey <MinValueHint <int> >(), (s) => DataGenerationHints.MinValue(XmlConvert.ToInt32(s)) },
         { GetHintKey <MaxValueHint <short> >(), (s) => DataGenerationHints.MaxValue(XmlConvert.ToInt16(s)) },
         { GetHintKey <MinValueHint <short> >(), (s) => DataGenerationHints.MinValue(XmlConvert.ToInt16(s)) },
         { GetHintKey <MaxValueHint <long> >(), (s) => DataGenerationHints.MaxValue(XmlConvert.ToInt64(s)) },
         { GetHintKey <MinValueHint <long> >(), (s) => DataGenerationHints.MinValue(XmlConvert.ToInt64(s)) },
         { GetHintKey <MaxValueHint <byte> >(), (s) => DataGenerationHints.MaxValue(XmlConvert.ToByte(s)) },
         { GetHintKey <MinValueHint <byte> >(), (s) => DataGenerationHints.MinValue(XmlConvert.ToByte(s)) },
         { GetHintKey <MaxValueHint <DateTime> >(), (s) => DataGenerationHints.MaxValue(PlatformHelper.ConvertStringToDateTime(s)) },
         { GetHintKey <MinValueHint <DateTime> >(), (s) => DataGenerationHints.MinValue(PlatformHelper.ConvertStringToDateTime(s)) },
         { GetHintKey <MaxValueHint <DateTimeOffset> >(), (s) => DataGenerationHints.MaxValue(XmlConvert.ToDateTimeOffset(s)) },
         { GetHintKey <MinValueHint <DateTimeOffset> >(), (s) => DataGenerationHints.MinValue(XmlConvert.ToDateTimeOffset(s)) },
         { GetHintKey <MaxValueHint <TimeSpan> >(), (s) => DataGenerationHints.MaxValue(XmlConvert.ToTimeSpan(s)) },
         { GetHintKey <MinValueHint <TimeSpan> >(), (s) => DataGenerationHints.MinValue(XmlConvert.ToTimeSpan(s)) },
         { GetHintKey <MaxValueHint <float> >(), (s) => DataGenerationHints.MaxValue(XmlConvert.ToSingle(s)) },
         { GetHintKey <MinValueHint <float> >(), (s) => DataGenerationHints.MinValue(XmlConvert.ToSingle(s)) },
         { GetHintKey <MaxValueHint <double> >(), (s) => DataGenerationHints.MaxValue(XmlConvert.ToDouble(s)) },
         { GetHintKey <MinValueHint <double> >(), (s) => DataGenerationHints.MinValue(XmlConvert.ToDouble(s)) },
         { GetHintKey <MaxValueHint <decimal> >(), (s) => DataGenerationHints.MaxValue(XmlConvert.ToDecimal(s)) },
         { GetHintKey <MinValueHint <decimal> >(), (s) => DataGenerationHints.MinValue(XmlConvert.ToDecimal(s)) },
         { GetHintKey <InterestingValueHint <int> >(), (s) => DataGenerationHints.InterestingValue(XmlConvert.ToInt32(s)) },
         { GetHintKey <InterestingValueHint <short> >(), (s) => DataGenerationHints.InterestingValue(XmlConvert.ToInt16(s)) },
         { GetHintKey <InterestingValueHint <long> >(), (s) => DataGenerationHints.InterestingValue(XmlConvert.ToInt64(s)) },
         { GetHintKey <InterestingValueHint <byte> >(), (s) => DataGenerationHints.InterestingValue(XmlConvert.ToByte(s)) },
         { GetHintKey <InterestingValueHint <DateTime> >(), (s) => DataGenerationHints.InterestingValue(PlatformHelper.ConvertStringToDateTime(s)) },
         { GetHintKey <InterestingValueHint <DateTimeOffset> >(), (s) => DataGenerationHints.InterestingValue(XmlConvert.ToDateTimeOffset(s)) },
         { GetHintKey <InterestingValueHint <TimeSpan> >(), (s) => DataGenerationHints.InterestingValue(XmlConvert.ToTimeSpan(s)) },
         { GetHintKey <InterestingValueHint <float> >(), (s) => DataGenerationHints.InterestingValue(XmlConvert.ToSingle(s)) },
         { GetHintKey <InterestingValueHint <double> >(), (s) => DataGenerationHints.InterestingValue(XmlConvert.ToDouble(s)) },
         { GetHintKey <InterestingValueHint <decimal> >(), (s) => DataGenerationHints.InterestingValue(XmlConvert.ToDecimal(s)) },
         { GetHintKey <InterestingValueHint <Guid> >(), (s) => DataGenerationHints.InterestingValue(XmlConvert.ToGuid(s)) },
         { GetHintKey <InterestingValueHint <bool> >(), (s) => DataGenerationHints.InterestingValue(XmlConvert.ToBoolean(s)) },
         { GetHintKey <InterestingValueHint <string> >(), (s) => DataGenerationHints.InterestingValue(s) },
         { GetHintKey <StringPrefixHint>(), (s) => DataGenerationHints.StringPrefixHint(s) },
         { GetHintKey <SpatialReferenceNumberHint>(), (s) => DataGenerationHints.SpatialReferenceNumber(XmlConvert.ToInt32(s)) },
         { GetHintKey <SpatialShapeKindHint>(), (s) => DataGenerationHints.SpatialShapeKind((SpatialShapeKind)Enum.Parse(typeof(SpatialShapeKind), s, true)) },
     };
 }
Exemplo n.º 8
0
 /// <summary>
 /// Deserialize an DateTime value using XmlConvert
 /// </summary>
 /// <param name="value">The wire representation of the value</param>
 /// <returns>the value as a clr instance</returns>
 public override DateTime DeserializeDateTime(string value)
 {
     return(PlatformHelper.ConvertStringToDateTime(value));
 }