コード例 #1
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override ICollection <T> ChangeType(ConvertContext context, string input, Type outputType,
                                                      out bool success)
        {
            input = input.Trim();
            if (input.Length == 0)
            {
                var builder = new ListBuilder(context, outputType);
                // ReSharper disable once AssignmentInConditionalExpression
                return((success = builder.TryCreateInstance()) ? builder.Instance : null);
            }
            if ((input[0] == '[') && (input[input.Length - 1] == ']'))
            {
                try
                {
                    var result = ComponentServices.ToJsonObject(outputType, input);
                    success = true;
                    return((ICollection <T>)result);
                }
                catch (Exception ex)
                {
                    context.AddException(ex);
                    success = false;
                    return(null);
                }
            }
            var arr = input.Split(_Separator, StringSplitOptions.None);

            return(ChangeType(context, arr, outputType, out success));
        }
コード例 #2
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override IList ChangeType(ConvertContext context, string input, Type outputType, out bool success)
        {
            input = input?.Trim();
            if ((input == null) || (input.Length <= 1))
            {
                success = false;
                return(null);
            }
            if ((input[0] == '[') && (input[input.Length - 1] == ']'))
            {
                try
                {
                    var result = ComponentServices.ToJsonObject(outputType, input);
                    success = true;
                    return((IList)result);
                }
                catch (Exception ex)
                {
                    context.AddException(ex);
                    success = false;
                    return(null);
                }
            }
            var arr = input.Split(_Separator, StringSplitOptions.None);

            return(ChangeType(context, arr, outputType, out success));
        }
コード例 #3
0
ファイル: CIDictionary.cs プロジェクト: blqw/blqw.Convert3
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override IDictionary <K, V> ChangeType(ConvertContext context, string input, Type outputType,
                                                         out bool success)
        {
            input = input?.Trim() ?? "";
            if (input.Length == 0)
            {
                var keyConvertor   = context.Get <K>();
                var valueConvertor = context.Get <V>();

                var builder = new DictionaryBuilder(context, outputType, keyConvertor, valueConvertor);
                // ReSharper disable once AssignmentInConditionalExpression
                return((success = builder.TryCreateInstance()) ? builder.Instance : null);
            }
            if (input?.Length > 2)
            {
                if ((input[0] == '{') && (input[input.Length - 1] == '}'))
                {
                    try
                    {
                        var result = ComponentServices.ToJsonObject(outputType, input);
                        success = true;
                        return((IDictionary <K, V>)result);
                    }
                    catch (Exception ex)
                    {
                        context.AddException(ex);
                    }
                }
            }
            success = false;
            return(null);
        }
コード例 #4
0
        /// <summary>
        /// 将字节流转换为键值对枚举
        /// </summary>
        /// <param name="bytes"> </param>
        /// <param name="formatProvider"> 它提供有关当前实例的格式信息 </param>
        /// <returns> </returns>
        public override IEnumerable <KeyValuePair <string, object> > Deserialize(byte[] bytes, IFormatProvider formatProvider)
        {
            var charset = GetEncoding(formatProvider) ?? Encoding.UTF8;
            var json    = charset.GetString(bytes);

            return((Dictionary <string, object>)ComponentServices.ToJsonObject(typeof(Dictionary <string, object>), json));
        }
コード例 #5
0
        /// <summary>
        /// 将字节流转换为指定对象
        /// </summary>
        /// <param name="bytes"> </param>
        /// <param name="formatProvider"> 它提供有关当前实例的格式信息 </param>
        /// <returns> </returns>
        public override T Deserialize <T>(byte[] bytes, IFormatProvider formatProvider)
        {
            var charset = GetEncoding(formatProvider) ?? Encoding.UTF8;
            var json    = charset.GetString(bytes);

            return((T)ComponentServices.ToJsonObject(typeof(T), json));
        }
コード例 #6
0
ファイル: CIDictionary.cs プロジェクト: blqw/blqw.Convert3
 /// <summary>
 /// 返回指定类型的对象,其值等效于指定字符串对象。
 /// </summary>
 /// <param name="context"> </param>
 /// <param name="input"> 需要转换类型的字符串对象 </param>
 /// <param name="outputType"> 换转后的类型 </param>
 /// <param name="success"> 是否成功 </param>
 protected override IDictionary ChangeType(ConvertContext context, string input, Type outputType,
                                           out bool success)
 {
     if (string.IsNullOrWhiteSpace(input))
     {
         success = false;
         return(null);
     }
     input = input.Trim();
     if ((input[0] == '{') && (input[input.Length - 1] == '}'))
     {
         try
         {
             var result = ComponentServices.ToJsonObject(outputType, input);
             success = true;
             return((IDictionary)result);
         }
         catch (Exception ex)
         {
             context.AddException(ex);
         }
     }
     success = false;
     return(null);
 }
コード例 #7
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override object ChangeType(ConvertContext context, string input, Type outputType, out bool success)
        {
            if (input?.Length > 2)
            {
                switch (input[0])
                {
                case '"':
                    if (input[input.Length - 1] != '"')
                    {
                        return(base.ChangeType(context, input, outputType, out success));
                    }
                    break;

                case '\'':
                    if (input[input.Length - 1] != '\'')
                    {
                        return(base.ChangeType(context, input, outputType, out success));
                    }
                    break;

                case '{':
                    if (input[input.Length - 1] != '}')
                    {
                        return(base.ChangeType(context, input, outputType, out success));
                    }
                    break;

                case '[':
                    if (input[input.Length - 1] != ']')
                    {
                        return(base.ChangeType(context, input, outputType, out success));
                    }
                    break;

                default:
                    return(base.ChangeType(context, input, outputType, out success));
                }
                try
                {
                    success = true;
                    return(ComponentServices.ToJsonObject(outputType, input));
                }
                catch (Exception ex)
                {
                    context.AddException(ex);
                }
            }
            return(base.ChangeType(context, input, outputType, out success));
        }
コード例 #8
0
        /// <summary>
        /// 追加 Json 对象到 Http 参数
        /// </summary>
        /// <param name="param"> 参数将被追加到的参数集合 </param>
        /// <param name="json"> 需要解析的 Json 字符串 </param>
        public static void AddJson(this HttpParamsBase <object> param, string json)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (string.IsNullOrWhiteSpace(json))
            {
                return;
            }

            var dict = (Dictionary <string, object>)ComponentServices.ToJsonObject(typeof(Dictionary <string, object>), json);

            foreach (var item in dict)
            {
                param[item.Key] = item.Value;
            }
        }
コード例 #9
0
        /// <summary>
        /// 追加 Json 对象到 Http 参数
        /// </summary>
        /// <param name="param"> 参数将被追加到的参数集合 </param>
        /// <param name="json"> 需要解析的 Json 字符串 </param>
        public static void AddJson(this HttpParamsBase <string> param, string json)
        {
            if (param == null)
            {
                throw new ArgumentNullException(nameof(param));
            }
            if (string.IsNullOrWhiteSpace(json))
            {
                return;
            }

            var nv = (NameValueCollection)ComponentServices.ToJsonObject(typeof(NameValueCollection), json);

            for (int i = 0, length = nv.Count; i < length; i++)
            {
                var key = nv.GetKey(i);
                var val = nv[i];
                param[key] = val;
            }
        }
コード例 #10
0
 /// <summary>
 /// 返回指定类型的对象,其值等效于指定字符串对象。
 /// </summary>
 /// <param name="context"> </param>
 /// <param name="input"> 需要转换类型的字符串对象 </param>
 /// <param name="outputType"> 换转后的类型 </param>
 /// <param name="success"> 是否成功 </param>
 protected override NameValueCollection ChangeType(ConvertContext context, string input, Type outputType,
                                                   out bool success)
 {
     input = input?.Trim();
     if ((input?.Length > 1) && (input[0] == '{') && (input[input.Length - 1] == '}'))
     {
         try
         {
             var result = ComponentServices.ToJsonObject(outputType, input);
             success = true;
             return((NameValueCollection)result);
         }
         catch (Exception ex)
         {
             context.AddException(ex);
         }
     }
     success = false;
     return(null);
 }
コード例 #11
0
        /// <summary>
        /// 返回指定类型的对象,其值等效于指定字符串对象。
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="input"> 需要转换类型的字符串对象 </param>
        /// <param name="outputType"> 换转后的类型 </param>
        /// <param name="success"> 是否成功 </param>
        protected override T[] ChangeType(ConvertContext context, string input, Type outputType, out bool success)
        {
            input = input?.Trim() ?? "";
            if (input.Length == 0)
            {
                success = true;
                return((T[])Array.CreateInstance(outputType.GetElementType(), 0));
            }
            if ((input[0] == '[') && (input[input.Length - 1] == ']')) //判断如果是json字符串,使用json方式转换
            {
                try
                {
                    var result = ComponentServices.ToJsonObject(outputType, input);
                    success = true;
                    return((T[])result);
                }
                catch (Exception ex)
                {
                    context.AddException(ex);
                    success = false;
                    return(null);
                }
            }

            var convertor = context.Get <T>();
            var items     = input.Split(_Separator, StringSplitOptions.None);
            var array     = Array.CreateInstance(convertor.OutputType, items.Length);

            for (var i = 0; i < items.Length; i++)
            {
                var value = convertor.ChangeType(context, items[i], convertor.OutputType, out success);
                if (success == false)
                {
                    context.AddException($"{value?.ToString() ?? "<null>"} 写入数组失败!");
                    return(null);
                }
                array.SetValue(value, i);
            }
            success = true;
            return((T[])array);
        }
コード例 #12
0
 public JsonFormBody(string json)
 {
     _data = ComponentServices.ToJsonObject(null, json);
     _json = json;
 }