Exemple #1
0
        /// <summary>
        /// 解析枚举
        /// </summary>
        /// <param name="type"></param>
        /// <param name="jsonValue"></param>
        /// <param name="report"></param>
        /// <returns></returns>
        private static object ParseEnum(Enum type, JsonParser.JsonValueContext jsonValue, ReportWrap report)
        {
            IToken token = jsonValue.strValue ?? jsonValue.intValue ?? jsonValue.floatValue;

            if (token != null)
            {
                object result = BaseUtil.GetEnum(type, token.Text.Trim('"'));
                if (result != null)
                {
                    return(result);
                }
                else
                {
                    report.OnError(string.Format("值\"{0}\"无法转换成枚举{1}.", token.Text.Trim('"'), type), token);
                }
            }
            else
            {
                report.OnError(string.Format("\"{0}\"无法转换成枚举{1}.", jsonValue.GetText(), type), jsonValue);
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// 获取列表格式的结构
        /// </summary>
        /// <param name="literal"></param>
        /// <param name="type"></param>
        /// <param name="text"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        private static object GetStruct(StructLiteral literal, Model.Struct type, string text, List <string> errors)
        {
            text = text.Trim();

            //查找自定义的分割规则
            var separator = ",";

            if (literal != null)
            {
                separator = literal.separator;
                if (text.StartsWith(literal.beginning))
                {
                    text = text.Substring(literal.beginning.Length);
                }
                if (text.EndsWith(literal.ending))
                {
                    text = text.Substring(0, text.Length - literal.ending.Length);
                }
            }

            //分割字符串
            var texts = new string[] { };

            if (!string.IsNullOrEmpty(text))
            {
                texts = text.Split(new string[] { separator }, StringSplitOptions.None);
            }

            //解析字符串
            var values = new Dictionary <string, object>();

            for (int i = 0; i < texts.Length; i++)
            {
                var txt = texts[i].Trim();
                if (i < type.Fields.Count)
                {
                    var field     = type.Fields[i];
                    var fieldType = field.Type;
                    if (BaseUtil.IsInteger(fieldType) || BaseUtil.IsFloat(fieldType) || BaseUtil.IsBool(fieldType))
                    {
                        object value = BaseUtil.GetScalar(fieldType, txt);
                        if (value != null)
                        {
                            values.Add(field.Name, value);
                        }
                        else
                        {
                            errors.Add(string.Format("第{0}个元素{1}无法解析成{2}。", i, txt, fieldType));
                        }
                    }
                    else if (BaseUtil.IsString(fieldType))
                    {
                        values.Add(field.Name, txt);
                    }
                    else if (field.TypeDefined is Model.Enum)
                    {
                        var value = BaseUtil.GetEnum(field.TypeDefined as Model.Enum, txt);
                        if (value != null)
                        {
                            values.Add(field.Name, value);
                        }
                        else
                        {
                            errors.Add(string.Format("第{0}个元素{1}无法解析成{2}。", i, txt, fieldType));
                        }
                    }
                    else if (field.TypeDefined is Model.Struct)
                    {
                        var value = GetStruct(field.Attributes, field.TypeDefined as Model.Struct, txt, errors);
                        if (value != null)
                        {
                            values.Add(field.Name, value);
                        }
                    }
                }
                else
                {
                    errors.Add(string.Format("第{0}个元素将被忽略,因为{1}的字段数量只有{2}个。", i, type.Name, type.Fields.Count));
                }
            }

            for (int i = texts.Length; i < type.Fields.Count; i++)
            {
                errors.Add(String.Format("字段{0}没有对应的数据项,因为数据只有{1}个元素。", type.Fields[i].Name, texts.Length));
            }

            return(values);
        }