Beispiel #1
0
        private void AppendList(StringBuilder sb, object value, EntityDataType dataType)
        {
            if (value == null)
            {
                return;
            }

            var enumerable = value as IEnumerable;

            if (enumerable == null)
            {
                throw new ApiException("Expected parameter to the IN filter to be a collection");
            }

            bool hadOne = false;

            foreach (object element in enumerable)
            {
                string serialized = ApiUtils.Serialize(element, dataType) ?? "";

                if (serialized.IndexOf('\'') != -1 || serialized.IndexOf(',') != -1)
                {
                    serialized = "'" + serialized.Replace("'", "''") + "'";
                }

                if (hadOne)
                {
                    sb.Append(',');
                }
                else
                {
                    hadOne = true;
                }

                sb.Append(Uri.EscapeDataString(serialized));
            }
        }
Beispiel #2
0
 private void Append(StringBuilder sb, object value, EntityDataType dataType)
 {
     sb.Append(Uri.EscapeDataString(ApiUtils.Serialize(value, dataType)));
 }
Beispiel #3
0
        public static string Serialize(object value, EntityDataType dataType)
        {
            if (value == null)
            {
                return("");
            }
            if (value is string)
            {
                return((string)value);
            }
            if (value is DateTime)
            {
                switch (dataType)
                {
                case EntityDataType.Date:
                    return(ApiUtils.PrintDate((DateTime)value));

                case EntityDataType.DateTime:
                    return(ApiUtils.PrintDateTime((DateTime)value));

                case EntityDataType.DateTimeTz:
                    return(ApiUtils.PrintDateTimeOffset((DateTime)value));

                default:
                    throw new ArgumentOutOfRangeException(nameof(value));
                }
            }
            if (value is DateTimeOffset)
            {
                switch (dataType)
                {
                case EntityDataType.Date:
                    return(ApiUtils.PrintDate((DateTimeOffset)value));

                case EntityDataType.DateTime:
                    return(ApiUtils.PrintDateTime((DateTimeOffset)value));

                case EntityDataType.DateTimeTz:
                    return(ApiUtils.PrintDateTimeOffset((DateTimeOffset)value));

                default:
                    throw new ArgumentOutOfRangeException(nameof(value));
                }
            }
            if (value is int)
            {
                return(((int)value).ToString(CultureInfo.InvariantCulture));
            }
            if (value is long)
            {
                return(((long)value).ToString(CultureInfo.InvariantCulture));
            }
            if (value is float)
            {
                return(((float)value).ToString(CultureInfo.InvariantCulture));
            }
            if (value is double)
            {
                return(((double)value).ToString(CultureInfo.InvariantCulture));
            }
            if (value is decimal)
            {
                return(((decimal)value).ToString(CultureInfo.InvariantCulture));
            }

            throw new ArgumentOutOfRangeException(nameof(value));
        }