Exemplo n.º 1
0
        /// <summary> 获取一个键
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private string ReadKey(UnsafeJsonReader reader)
        {
            reader.SkipWhiteWord();
            string key;

            if (reader.Current == '"' || reader.Current == '\'')
            {
                key = reader.ReadString();
            }
            else
            {
                key = reader.ReadWord();
            }
            if (reader.SkipChar(':') == false)  //跳过:号
            {
                ThrowMissingCharException(':'); //失败,终止方法
            }
            return(key);
        }
Exemplo n.º 2
0
        /// <summary> 将字符串解析为指定类型
        /// </summary>
        /// <param name="str"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private object ParseString(UnsafeJsonReader reader, Type type)
        {
            char   quot;
            string str;
            var    typecode = Type.GetTypeCode(type);

            if ((typecode >= TypeCode.SByte && typecode <= TypeCode.Decimal) || typecode == TypeCode.Boolean)
            {
                if (type.IsSubclassOf(typeof(Enum)))
                {
                    return(Enum.Parse(type, reader.ReadString()));
                }
                quot = reader.Current;
                if (quot != '\"' && quot != '\'')
                {
                    ThrowMissingCharException(quot);
                }
                if (reader.SkipChar(quot) == false)
                {
                    ThrowMissingCharException(quot);
                }
                var val = Convert.ChangeType(reader.ReadConsts(), type);
                if (reader.SkipChar(quot) == false)
                {
                    ThrowMissingCharException(quot);
                }
                return(val);
            }
            switch (typecode)
            {
            case TypeCode.DateTime:
                return(reader.ReadDateTime());

            case TypeCode.Object:
                str = reader.ReadString();
                if (type == typeof(Guid))
                {
                    try
                    {
                        if (str.Length > 30)
                        {
                            return(new Guid(str));
                        }
                        else
                        {
                            return(new Guid(Convert.FromBase64String(str)));
                        }
                    }
                    catch
                    {
                        return(Guid.Empty);
                    }
                }
                else if (type == typeof(Object))
                {
                    return(str);
                }
                throw new Exception();

            case TypeCode.Char:
                return(Char.Parse(reader.ReadString()));

            case TypeCode.String:
                return(reader.ReadString());

            case TypeCode.DBNull:
                str = reader.ReadString();
                if (str.Length == 0 ||
                    str == "null" ||
                    str == "undefined" ||
                    string.IsNullOrEmpty(str))
                {
                    return(DBNull.Value);
                }
                throw new Exception();

            default:
                return(Convert.ChangeType(reader.ReadString(), type));
            }
        }