Ejemplo n.º 1
0
        /// <summary>
        /// 解析标量
        /// </summary>
        /// <param name="type"></param>
        /// <param name="jsonValue"></param>
        /// <param name="report"></param>
        /// <returns></returns>
        private static object ParseScalar(string type, JsonParser.JsonValueContext jsonValue, ReportWrap report)
        {
            IToken token = jsonValue.boolValue ?? jsonValue.intValue ?? jsonValue.floatValue ?? jsonValue.strValue ?? null;

            if (token != null)
            {
                object result = BaseUtil.GetScalar(type, token.Text.Trim('"'));
                if (result != null)
                {
                    return(result);
                }
                else
                {
                    if (BaseUtil.IsInteger(type) || BaseUtil.IsFloat(type))
                    {
                        report.OnError(string.Format("值\"{0}\"无法转换成{1},或超出{1}的精度范围.", token.Text.Trim('"'), type), token);
                    }
                    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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 标量
        /// </summary>
        /// <param name="type"></param>
        /// <param name="cell"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        private object GetScalar(string type, ICell cell, List <string> errors)
        {
            object result = null;

            if (cell.CellType == CellType.Numeric)
            {
                if ((type.Equals("byte") || type.Equals("int8")) && sbyte.MinValue <= cell.NumericCellValue && cell.NumericCellValue <= sbyte.MaxValue)
                {
                    result = (sbyte)cell.NumericCellValue;
                }
                else if ((type.Equals("ubyte") || type.Equals("uint8")) && byte.MinValue <= cell.NumericCellValue && cell.NumericCellValue <= byte.MaxValue)
                {
                    result = (byte)cell.NumericCellValue;
                }
                else if ((type.Equals("short") || type.Equals("int16")) && short.MinValue <= cell.NumericCellValue && cell.NumericCellValue <= short.MaxValue)
                {
                    result = (short)cell.NumericCellValue;
                }
                else if ((type.Equals("ushort") || type.Equals("uint16")) && ushort.MinValue <= cell.NumericCellValue && cell.NumericCellValue <= ushort.MaxValue)
                {
                    result = (ushort)cell.NumericCellValue;
                }
                else if (type.Equals("int") || type.Equals("int32") && int.MinValue <= cell.NumericCellValue && cell.NumericCellValue <= int.MaxValue)
                {
                    result = (int)cell.NumericCellValue;
                }
                else if (type.Equals("uint") || type.Equals("uint32") && uint.MinValue <= cell.NumericCellValue && cell.NumericCellValue <= uint.MaxValue)
                {
                    result = (uint)cell.NumericCellValue;
                }
                else if (type.Equals("long") || type.Equals("int64") && long.MinValue <= cell.NumericCellValue && cell.NumericCellValue <= long.MaxValue)
                {
                    result = (long)cell.NumericCellValue;
                }
                else if (type.Equals("ulong") || type.Equals("uint64") && ulong.MinValue <= cell.NumericCellValue && cell.NumericCellValue <= ulong.MaxValue)
                {
                    result = (ulong)cell.NumericCellValue;
                }
                else if ((type.Equals("float") || type.Equals("float32")) && float.MinValue <= cell.NumericCellValue && cell.NumericCellValue <= float.MaxValue)
                {
                    result = (float)cell.NumericCellValue;
                }
                else if ((type.Equals("double") || type.Equals("double64")) && double.MinValue <= cell.NumericCellValue && cell.NumericCellValue > double.MaxValue)
                {
                    result = cell.NumericCellValue;
                }
                else if (type.Equals("bool"))
                {
                    result = cell.NumericCellValue != 0;
                }
                else if (type.Equals("string"))
                {
                    result = cell.NumericCellValue.ToString();
                }
            }
            else if (cell.CellType == CellType.Boolean)
            {
                if (type.Equals("bool"))
                {
                    result = cell.BooleanCellValue;
                }
                else if (type.Equals("string"))
                {
                    result = cell.BooleanCellValue.ToString();
                }
            }
            else if (cell.CellType == CellType.String)
            {
                if (type.Equals("string"))
                {
                    result = cell.StringCellValue.ToString();
                }
            }

            if (result == null)
            {
                if (BaseUtil.IsInteger(type) || BaseUtil.IsFloat(type))
                {
                    errors.Add(string.Format("内容不是一个有效的{0},或者超出了{0}的有效精度.", type));
                }
                else
                {
                    errors.Add(string.Format("内容不是一个有效的{0}.", type));
                }
            }

            return(result);
        }
Ejemplo n.º 3
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);
        }