Example #1
0
        /// <summary>Converts the specified value to a serializable string in ATOM format.</summary>
        /// <param name="value">Non-null value to convert.</param>
        /// <param name="result">The specified value converted to an ATOM string.</param>
        /// <returns>boolean value indicating conversion successful conversion</returns>
        internal static bool TryConvertPrimitiveToString(object value, out string result)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(value != null, "value != null");
            result = null;

            TypeCode typeCode = PlatformHelper.GetTypeCode(value.GetType());

            switch (typeCode)
            {
            case TypeCode.Boolean:
                result = ODataAtomConvert.ToString((bool)value);
                break;

            case TypeCode.Byte:
                result = ODataAtomConvert.ToString((byte)value);
                break;

            case TypeCode.DateTime:
                result = ODataAtomConvert.ToString((DateTime)value);
                break;

            case TypeCode.Decimal:
                result = ODataAtomConvert.ToString((decimal)value);
                break;

            case TypeCode.Double:
                result = ODataAtomConvert.ToString((double)value);
                break;

            case TypeCode.Int16:
                result = ODataAtomConvert.ToString((Int16)value);
                break;

            case TypeCode.Int32:
                result = ODataAtomConvert.ToString((Int32)value);
                break;

            case TypeCode.Int64:
                result = ODataAtomConvert.ToString((Int64)value);
                break;

            case TypeCode.SByte:
                result = ODataAtomConvert.ToString((SByte)value);
                break;

            case TypeCode.String:
                result = (string)value;
                break;

            case TypeCode.Single:
                result = ODataAtomConvert.ToString((Single)value);
                break;

            default:
                byte[] bytes = value as byte[];
                if (bytes != null)
                {
                    result = ODataAtomConvert.ToString(bytes);
                    break;
                }

                if (value is DateTimeOffset)
                {
                    result = ODataAtomConvert.ToString((DateTimeOffset)value);
                    break;
                }

                if (value is Guid)
                {
                    result = ODataAtomConvert.ToString((Guid)value);
                    break;
                }

                if (value is TimeSpan)
                {
                    // Edm.Time
                    result = ODataAtomConvert.ToString((TimeSpan)value);
                    break;
                }

                return(false);
            }

            Debug.Assert(result != null, "result != null");
            return(true);
        }
Example #2
0
        internal static bool TryConvertPrimitiveToString(object value, out string result)
        {
            result = null;
            switch (Microsoft.Data.OData.PlatformHelper.GetTypeCode(value.GetType()))
            {
            case TypeCode.Boolean:
                result = ODataAtomConvert.ToString((bool)value);
                break;

            case TypeCode.SByte:
                result = ((sbyte)value).ToString();
                break;

            case TypeCode.Byte:
                result = ODataAtomConvert.ToString((byte)value);
                break;

            case TypeCode.Int16:
                result = ((short)value).ToString();
                break;

            case TypeCode.Int32:
                result = ((int)value).ToString();
                break;

            case TypeCode.Int64:
                result = ((long)value).ToString();
                break;

            case TypeCode.Single:
                result = ((float)value).ToString();
                break;

            case TypeCode.Double:
                result = ((double)value).ToString();
                break;

            case TypeCode.Decimal:
                result = ODataAtomConvert.ToString((decimal)value);
                break;

            case TypeCode.DateTime:
                result = ((DateTime)value).ToString();
                break;

            case TypeCode.String:
                result = (string)value;
                break;

            default:
            {
                byte[] bytes = value as byte[];
                if (bytes != null)
                {
                    result = bytes.ToString();
                }
                else if (value is DateTimeOffset)
                {
                    result = ODataAtomConvert.ToString((DateTimeOffset)value);
                }
                else if (value is Guid)
                {
                    result = ((Guid)value).ToString();
                }
                else if (value is TimeSpan)
                {
                    result = ((TimeSpan)value).ToString();
                }
                else
                {
                    return(false);
                }
                break;
            }
            }
            return(true);
        }