예제 #1
0
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="node">节点流内容</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        public override byte Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(0);
            }

            var value = node.GetValue();

            if (value.IsNullOrEmpty)
            {
                return(0);
            }

            try
            {
                if (checkNullValue && this.IsNullValue(value))
                {
                    return(0);
                }

                return(ParseByte(value));
            }
            catch (ArgumentOutOfRangeException)
            {
                throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在[0-9]范围内", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }
            catch (ArgumentException)
            {
                throw new ArgumentOutOfRangeException(node.Key, string.Format("数值溢出,在字符{0}至{1}处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }
        }
예제 #2
0
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="node">节点流内容</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        public override bool Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(false);
            }

            var value = node.GetValue();

            if (value.IsNullOrEmpty)
            {
                return(false);
            }

            if (value.Length == 1)
            {
                return(value[0] == '1');
            }

            if (IsNullValue(value))
            {
                return(false);
            }

            if (value.Length != 4)
            {
                return(false);
            }

            return((value[0] == 't' || value[0] == 'T')
                   & (value[1] == 'r' || value[1] == 'R')
                   & (value[2] == 'u' || value[2] == 'U')
                   & (value[3] == 'e' || value[3] == 'e'));
        }
예제 #3
0
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="node">节点流内容</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        public override DateTime Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(new DateTime());
            }

            var value = node.GetValue();

            if (value.IsNullOrEmpty)
            {
                return(new DateTime());
            }

            if (checkNullValue && this.IsNullValue(value))
            {
                return(new DateTime());
            }

            if (value[value.Length - 1] == 'Z')
            {
                return(ISO8601StyleDateMethodProvider.Default.Parse(reader, setting, node, checkNullValue));
            }

            if (value[value.Length - 1] == 'T')
            {
                return(RFC1123StyleDateMethodProvider.Default.Parse(reader, setting, node, checkNullValue));
            }

            if (value[value.Length - 1] == '/')
            {
                return(MicrosoftStyleDateMethodProvider.Default.Parse(reader, setting, node, checkNullValue));
            }

            if (value.Length > 10)
            {
                if (value[10] == ' ' || value[9] == ' ' || value[8] == ' ')
                {
                    return(ChineseStyleDateMethodProvider.Default.Parse(reader, setting, node, checkNullValue));
                }
            }

            try
            {
                DateTime time;
                DateTime.TryParse(value.ToString(), out time);
                return(time);
            }
            catch (OverflowException)
            {
                throw new ArgumentOutOfRangeException(node.Key, string.Format("数值溢出,在字符{0}至{1}处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }
            catch
            {
                throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }
        }
예제 #4
0
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="node">节点流内容</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        public override T Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(default(T));
            }

            var value = node.GetValue();

            if (value.IsNullOrEmpty)
            {
                return(default(T));
            }

            return(EnumDeseralizerBuilder <T> .Register(setting).Invoke(value.ToString()));
        }
예제 #5
0
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="node">节点流内容</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        public override int?Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(null);
            }

            var value = node.GetValue();

            if (value.IsNullOrEmpty)
            {
                return(null);
            }

            if (checkNullValue && this.IsNullValue(value))
            {
                return(null);
            }

            return(MethodProviders.Int32MethodProvider.Default.Parse(reader, setting, node, false));
        }
예제 #6
0
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="node">节点流内容</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        public override char Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(default(char));
            }

            var value = node.GetValue();

            if (value.IsNullOrEmpty)
            {
                return(default(char));
            }

            if (checkNullValue && this.IsNullValue(value))
            {
                return(' ');
            }

            if (value.Length == 1)
            {
                return(value[0]);
            }

            if (value.Length == 2)
            {
                return(value[1]);
            }

            if (value[1] != 'u')
            {
                throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,char取值只能一位", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }

            if (value.Length != 6)
            {
                throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,char取值只能一位", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }

            var ret = 0;

            for (var i = 2; i < 6; i++)
            {
                switch (value[i])
                {
                case '0':
                {
                }
                break;

                case '1':
                {
                    ret += _16Pow(5 - i);
                }
                break;

                case '2':
                {
                    ret += 2 * _16Pow(5 - i);
                }
                break;

                case '3':
                {
                    ret += 3 * _16Pow(5 - i);
                }
                break;

                case '4':
                {
                    ret += 4 * _16Pow(5 - i);
                }
                break;

                case '5':
                {
                    ret += 5 * _16Pow(5 - i);
                }
                break;

                case '6':
                {
                    ret += 6 * _16Pow(5 - i);
                }
                break;

                case '7':
                {
                    ret += 7 * _16Pow(5 - i);
                }
                break;

                case '8':
                {
                    ret += 8 * _16Pow(5 - i);
                }
                break;

                case '9':
                {
                    ret += 9 * _16Pow(5 - i);
                }
                break;

                case 'a':
                case 'A':
                {
                    ret += 10 * _16Pow(5 - i);
                }
                break;

                case 'b':
                case 'B':
                {
                    ret += 11 * _16Pow(5 - i);
                }
                break;

                case 'c':
                case 'C':
                {
                    ret += 12 * _16Pow(5 - i);
                }
                break;

                case 'd':
                case 'D':
                {
                    ret += 13 * _16Pow(5 - i);
                }
                break;

                case 'e':
                case 'E':
                {
                    ret += 14 * _16Pow(5 - i);
                }
                break;

                case 'f':
                case 'F':
                {
                    ret += 15 * _16Pow(5 - i);
                }
                break;

                default:
                {
                    throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,char取值只能一位", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
                }
                }
            }

            return((char)ret);
        }
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="node">节点流内容</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        public override DateTime Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(new DateTime());
            }

            var value = node.GetValue();

            try
            {
                double ret = 0d;
                for (var i = 7; i <= 19; i++)
                {
                    switch (value[i])
                    {
                    case '0':
                    {
                    }
                    break;

                    case '1':
                    {
                        ret += GetInteger(20 - i);
                    }
                    break;

                    case '2':
                    {
                        ret += 2 * GetInteger(20 - i);
                    }
                    break;

                    case '3':
                    {
                        ret += 3 * GetInteger(20 - i);
                    }
                    break;

                    case '4':
                    {
                        ret += 4 * GetInteger(20 - i);
                    }
                    break;

                    case '5':
                    {
                        ret += 5 * GetInteger(20 - i);
                    }
                    break;

                    case '6':
                    {
                        ret += 6 * GetInteger(20 - i);
                    }
                    break;

                    case '7':
                    {
                        ret += 7 * GetInteger(20 - i);
                    }
                    break;

                    case '8':
                    {
                        ret += 8 * GetInteger(20 - i);
                    }
                    break;

                    case '9':
                    {
                        ret += 9 * GetInteger(20 - i);
                    }
                    break;

                    default:
                        throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在[0,9]范围内", (node.Segment.Offset).ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
                    }
                }

                return(_1970.AddMilliseconds(ret));
            }
            catch (OverflowException)
            {
                throw new ArgumentOutOfRangeException(node.Key, string.Format("数值溢出,在字符{0}至{1}处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }
            catch
            {
                throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在[0-9]范围内", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }
        }
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="node">节点流内容</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        /// <exception cref="System.ArgumentNullException"></exception>
        public override DateTime Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(new DateTime());
            }

            var value = node.GetValue();

            try
            {
                int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, mill = 0;
                if (value.Length > 22)
                {
                    year   = GetGigitInZeroToNightChar(value[0]) * 1000 + GetGigitInZeroToNightChar(value[1]) * 100 + GetGigitInZeroToNightChar(value[2]) * 10 + GetGigitInZeroToNightChar(value[3]);
                    month  = GetGigitInZeroToNightChar(value[5]) * 10 + GetGigitInZeroToNightChar(value[6]);
                    day    = GetGigitInZeroToNightChar(value[8]) * 10 + GetGigitInZeroToNightChar(value[9]);
                    hour   = GetGigitInZeroToNightChar(value[11]) * 10 + GetGigitInZeroToNightChar(value[12]);
                    minute = GetGigitInZeroToNightChar(value[14]) * 10 + GetGigitInZeroToNightChar(value[15]);
                    second = GetGigitInZeroToNightChar(value[17]) * 10 + GetGigitInZeroToNightChar(value[18]);
                    mill   = GetGigitInZeroToNightChar(value[20]) * 100 + GetGigitInZeroToNightChar(value[21]) * 10 + GetGigitInZeroToNightChar(value[22]);
                }
                else if (value.Length > 18)
                {
                    year   = GetGigitInZeroToNightChar(value[0]) * 1000 + GetGigitInZeroToNightChar(value[1]) * 100 + GetGigitInZeroToNightChar(value[2]) * 10 + GetGigitInZeroToNightChar(value[3]);
                    month  = GetGigitInZeroToNightChar(value[5]) * 10 + GetGigitInZeroToNightChar(value[6]);
                    day    = GetGigitInZeroToNightChar(value[8]) * 10 + GetGigitInZeroToNightChar(value[9]);
                    hour   = GetGigitInZeroToNightChar(value[11]) * 10 + GetGigitInZeroToNightChar(value[12]);
                    minute = GetGigitInZeroToNightChar(value[14]) * 10 + GetGigitInZeroToNightChar(value[15]);
                    second = GetGigitInZeroToNightChar(value[17]) * 10 + GetGigitInZeroToNightChar(value[18]);
                }
                else if (value.Length > 15)
                {
                    year   = GetGigitInZeroToNightChar(value[0]) * 1000 + GetGigitInZeroToNightChar(value[1]) * 100 + GetGigitInZeroToNightChar(value[2]) * 10 + GetGigitInZeroToNightChar(value[3]);
                    month  = GetGigitInZeroToNightChar(value[5]) * 10 + GetGigitInZeroToNightChar(value[6]);
                    day    = GetGigitInZeroToNightChar(value[8]) * 10 + GetGigitInZeroToNightChar(value[9]);
                    hour   = GetGigitInZeroToNightChar(value[11]) * 10 + GetGigitInZeroToNightChar(value[12]);
                    minute = GetGigitInZeroToNightChar(value[14]) * 10 + GetGigitInZeroToNightChar(value[15]);
                }
                else if (value.Length > 12)
                {
                    year  = GetGigitInZeroToNightChar(value[0]) * 1000 + GetGigitInZeroToNightChar(value[1]) * 100 + GetGigitInZeroToNightChar(value[2]) * 10 + GetGigitInZeroToNightChar(value[3]);
                    month = GetGigitInZeroToNightChar(value[5]) * 10 + GetGigitInZeroToNightChar(value[6]);
                    day   = GetGigitInZeroToNightChar(value[8]) * 10 + GetGigitInZeroToNightChar(value[9]);
                    hour  = GetGigitInZeroToNightChar(value[11]) * 10 + GetGigitInZeroToNightChar(value[12]);
                }
                else if (value.Length > 9)
                {
                    year  = GetGigitInZeroToNightChar(value[0]) * 1000 + GetGigitInZeroToNightChar(value[1]) * 100 + GetGigitInZeroToNightChar(value[2]) * 10 + GetGigitInZeroToNightChar(value[3]);
                    month = GetGigitInZeroToNightChar(value[5]) * 10 + GetGigitInZeroToNightChar(value[6]);
                    day   = GetGigitInZeroToNightChar(value[8]) * 10 + GetGigitInZeroToNightChar(value[9]);
                }
                else if (value.Length > 6)
                {
                    year  = GetGigitInZeroToNightChar(value[0]) * 1000 + GetGigitInZeroToNightChar(value[1]) * 100 + GetGigitInZeroToNightChar(value[2]) * 10 + GetGigitInZeroToNightChar(value[3]);
                    month = GetGigitInZeroToNightChar(value[5]) * 10 + GetGigitInZeroToNightChar(value[6]);
                }
                else
                {
                    year = GetGigitInZeroToNightChar(value[0]) * 1000 + GetGigitInZeroToNightChar(value[1]) * 100 + GetGigitInZeroToNightChar(value[2]) * 10 + GetGigitInZeroToNightChar(value[3]);
                }
                //year = GetGigitInZeroToNightChar(node.Value[0]) * 1000 + GetGigitInZeroToNightChar(node.Value[1]) * 100 + GetGigitInZeroToNightChar(node.Value[2]) * 10 + GetGigitInZeroToNightChar(node.Value[3]);
                //month = GetGigitInZeroToNightChar(node.Value[5]) * 10 + GetGigitInZeroToNightChar(node.Value[6]);
                //day = GetGigitInZeroToNightChar(node.Value[8]) * 10 + GetGigitInZeroToNightChar(node.Value[9]);
                //hour = GetGigitInZeroToNightChar(node.Value[11]) * 10 + GetGigitInZeroToNightChar(node.Value[12]);
                //minute = GetGigitInZeroToNightChar(node.Value[14]) * 10 + GetGigitInZeroToNightChar(node.Value[15]);
                //second = GetGigitInZeroToNightChar(node.Value[17]) * 10 + GetGigitInZeroToNightChar(node.Value[18]);
                //if (node.Value.Length > 22)
                //    mill = GetGigitInZeroToNightChar(node.Value[20]) * 100 + GetGigitInZeroToNightChar(node.Value[21]) * 10 + GetGigitInZeroToNightChar(node.Value[22]);

                return(new DateTime(year, month, day, hour, minute, second, mill));
            }
            catch (OverflowException)
            {
                throw new ArgumentOutOfRangeException(node.Key, string.Format("数值溢出,在字符{0}至{1}处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }
            catch
            {
                throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }
        }
예제 #9
0
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="node">节点流内容</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        public override TimeSpan Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(TimeSpan.Zero);
            }

            var value = node.GetValue();

            if (value.IsNullOrEmpty)
            {
                return(TimeSpan.Zero);
            }

            if (checkNullValue && this.IsNullValue(value))
            {
                return(TimeSpan.Zero);
            }

            int point = 0;

            for (var i = 0; i < value.Length; i++)
            {
                if (value[i] == ':')
                {
                    goto _noday;
                }

                if (value[i] == '.')
                {
                    break;
                }

                point++;
            }

            goto _hasday;

_noday:
            {
                try
                {
                    point = 0;
                    int hour = 0;
                    if (value.Length >= point + 2)
                    {
                        hour = GetGigitInZeroToNightChar(value[0]) * 10 + GetGigitInZeroToNightChar(value[1]);
                    }
                    point += 3;

                    int minute = 0;
                    if (value.Length >= point + 2)
                    {
                        minute = GetGigitInZeroToNightChar(value[point]) * 10 + GetGigitInZeroToNightChar(value[point + 1]);
                    }
                    point += 3;

                    int second = 0;
                    if (value.Length >= point + 2)
                    {
                        second = GetGigitInZeroToNightChar(value[point]) * 10 + GetGigitInZeroToNightChar(value[point + 1]);
                    }
                    point += 3;

                    int mill = 0;
                    if (value.Length >= point + 3)
                    {
                        mill = GetGigitInZeroToNightChar(value[point]) * 100 + GetGigitInZeroToNightChar(value[point + 1]) * 10 + GetGigitInZeroToNightChar(value[point + 2]);
                    }

                    return(new TimeSpan(0, hour, minute, second, mill));
                }
                catch (OverflowException)
                {
                    throw new ArgumentOutOfRangeException(node.Key, string.Format("数值溢出,在字符{0}至{1}处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
                }
                catch
                {
                    throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
                }
            };

_hasday:
            {
                try
                {
                    int day = 0;
                    for (var i = 0; i < point; i++)
                    {
                        day += GetGigitInZeroToNightChar(value[i]) * (int)GetInteger(point - i);
                    }

                    int hour = 0;
                    if (value.Length >= point + 2)
                    {
                        hour = GetGigitInZeroToNightChar(value[point + 1]) * 10 + GetGigitInZeroToNightChar(value[point + 2]);
                    }
                    point += 3;

                    int minute = 0;
                    if (value.Length >= point + 2)
                    {
                        minute = GetGigitInZeroToNightChar(value[point + 1]) * 10 + GetGigitInZeroToNightChar(value[point + 2]);
                    }
                    point += 3;

                    int second = 0;
                    if (value.Length >= point + 2)
                    {
                        second = GetGigitInZeroToNightChar(value[point + 1]) * 10 + GetGigitInZeroToNightChar(value[point + 2]);
                    }
                    point += 3;

                    int mill = 0;
                    if (value.Length >= point + 3)
                    {
                        mill = GetGigitInZeroToNightChar(value[point + 1]) * 100 + GetGigitInZeroToNightChar(value[point + 2]) * 10 + GetGigitInZeroToNightChar(value[point + 3]);
                    }

                    return(new TimeSpan(day, hour, minute, second, mill));
                }
                catch (OverflowException)
                {
                    throw new ArgumentOutOfRangeException(node.Key, string.Format("数值溢出,在字符{0}至{1}处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
                }
                catch
                {
                    throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
                }
            };
        }
예제 #10
0
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="node">节点流内容</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        public override string Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(null);
            }

            var objnode = node as IObjectContentNode;

            if (objnode == null)
            {
                return(null);
            }

            var objnodeValue = node.GetValue();

            if (objnode.NodeType == ContentNodeType.String)
            {
                if (objnodeValue == null)
                {
                    return(null);
                }

                if (objnodeValue.Length == 0)
                {
                    return(string.Empty);
                }

                if (checkNullValue && this.IsNullValue(objnodeValue))
                {
                    return(null);
                }

                if (!objnode.Escaping)
                {
                    return(objnodeValue.ToString());
                }

                /*有转义符号*/
                var buffer = new char[objnodeValue.Length];
                int index  = 0;
                for (var i = 0; i < objnodeValue.Length; i++)
                {
                    if (i >= objnodeValue.Length)
                    {
                        continue;
                    }

                    switch (objnodeValue[i])
                    {
                    case '\\':
                    {
                        var pre = this.Next(objnodeValue, i);
                        if (!pre.HasValue)
                        {
                            continue;
                        }

                        switch (pre.Value)
                        {
                        case '/':
                        case '\'':
                        case '\"':
                        {
                            i            += 1;
                            buffer[index] = objnodeValue[i];
                            index++;
                        }
                        break;

                        case 'b':
                        {
                            buffer[index] = '\b';
                            index++;
                            i += 1;
                        }
                        break;

                        case 't':
                        {
                            buffer[index] = '\t';
                            index++;
                            i += 1;
                        }
                        break;

                        case 'n':
                        {
                            buffer[index] = '\n';
                            index++;
                            i += 1;
                        }
                        break;

                        case 'f':
                        {
                            buffer[index] = '\f';
                            index++;
                            i += 1;
                        }
                        break;

                        case 'r':
                        {
                            buffer[index] = '\r';
                            index++;
                            i += 1;
                        }
                        break;

                        case '\\':
                        {
                            buffer[index] = '\\';
                            index++;
                            i += 1;
                        }
                        break;

                        case 'u':
                        {
                            var j = i;
                            if (j < 0)
                            {
                                j = 0;
                            }

                            if (objnodeValue.Length >= j + 5)
                            {
                                var @char = (new char[]
                                        {
                                            objnodeValue[j + 0],
                                            objnodeValue[j + 1],
                                            objnodeValue[j + 2],
                                            objnodeValue[j + 3],
                                            objnodeValue[j + 4],
                                            objnodeValue[j + 5],
                                        });

                                var value = 0;
                                var hex   = 0;
                                for (var ae = j + 2; ae < j + 6; ae++)
                                {
                                    /*0-9*/
                                    if (objnodeValue[ae] >= 48 && objnodeValue[ae] <= 57)
                                    {
                                        hex = objnodeValue[ae] - 48;
                                    }
                                    /*A-F*/
                                    else if (objnodeValue[ae] >= 65 && objnodeValue[ae] <= 70)
                                    {
                                        hex = objnodeValue[ae] - 55;
                                    }
                                    /*a-f*/
                                    else if (objnodeValue[ae] >= 97 && objnodeValue[ae] <= 102)
                                    {
                                        hex = objnodeValue[ae] - 87;
                                    }
                                    else
                                    {
                                        throw new ArgumentException(string.Format("在读取{0}处字符失败了:不符合Unicode编码", ae.ToString()));
                                    }

                                    value += hex << (j + 5 - ae) * 4;
                                }
                                buffer[index] = Convert.ToChar(value);
                                i             = j + 5;
                                index++;
                            }
                            else
                            {
                                buffer[index] = objnodeValue[i];
                                index++;
                            }
                        }
                        break;

                        default:
                        {
                            buffer[index] = objnodeValue[i];
                            index++;
                        }
                        break;
                        }
                    }
                    break;

                    default:
                    {
                        buffer[index] = objnodeValue[i];
                        index++;
                    }
                    break;
                    }
                }

                return(new string(buffer, 0, index));
            }

            return(objnode.ToString());
            //return reader.Read(objnode.Segment.Offset, objnode.Segment.Offset + objnode.Segment.Count);
        }
예제 #11
0
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="node">节点流内容</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        public override DateTime Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(new DateTime());
            }

            var value = node.GetValue();

            try
            {
                if (value.IsNullOrEmpty)
                {
                    return(new DateTime());
                }

                /*空格位置*/
                int space = 3;
                /*获取天数*/
                for (var i = 3; i < value.Length; i++)
                {
                    if (value[i] == ' ')
                    {
                        break;
                    }

                    space++;
                }

                int day = GetGigitInZeroToNightChar(value[space + 1]) * 10 + GetGigitInZeroToNightChar(value[space + 2]);
                space += 2;

                /*获取月份*/
                for (var i = space; i < value.Length; i++)
                {
                    if (value[i] == ' ')
                    {
                        break;
                    }

                    space++;
                }

                int month = 0;

                #region month

                switch (value[space + 1])
                {
                case 'j':
                case 'J':
                {
                    switch (value[space + 2])
                    {
                    case 'a':
                    case 'A':
                    {
                        switch (value[space + 3])
                        {
                        case 'n':
                        case 'N':
                        {
                            month = 1;
                        }
                        break;

                        default:
                            throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(nN)范围内", (node.Segment.Offset + 2).ToString(), (node.Segment.Offset + 2).ToString()));
                        }
                    }
                    break;

                    case 'u':
                    case 'U':
                    {
                        switch (value[space + 3])
                        {
                        case 'n':
                        case 'N':
                        {
                            month = 6;
                        }
                        break;

                        case 'l':
                        case 'L':
                        {
                            month = 7;
                        }
                        break;

                        default:
                            throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(nN或lL)范围内", (node.Segment.Offset + 2).ToString(), (node.Segment.Offset + 2).ToString()));
                        }
                    }
                    break;

                    default:
                        throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(aA或uU)范围内", (node.Segment.Offset + 1).ToString(), (node.Segment.Offset + 1).ToString()));
                    }
                }
                break;

                case 'F':
                case 'f':
                {
                    switch (value[space + 2])
                    {
                    case 'e':
                    case 'E':
                    {
                        switch (value[space + 3])
                        {
                        case 'b':
                        case 'B':
                        {
                            month = 2;
                        }
                        break;

                        default:
                            throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(bB)范围内", (node.Segment.Offset + 2).ToString(), (node.Segment.Offset + 2).ToString()));
                        }
                    }
                    break;

                    default:
                        throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(eE)范围内", (node.Segment.Offset + 1).ToString(), (node.Segment.Offset + 1).ToString()));
                    }
                }
                break;

                case 'M':
                case 'm':
                {
                    switch (value[space + 2])
                    {
                    case 'a':
                    case 'A':
                    {
                        switch (value[space + 3])
                        {
                        case 'r':
                        case 'R':
                        {
                            month = 3;
                        }
                        break;

                        case 'y':
                        case 'Y':
                        {
                            month = 5;
                        }
                        break;

                        default:
                            throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(rR或yY)范围内", (node.Segment.Offset + 2).ToString(), (node.Segment.Offset + 2).ToString()));
                        }
                    }
                    break;

                    default:
                        throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(aA)范围内", (node.Segment.Offset + 1).ToString(), (node.Segment.Offset + 1).ToString()));
                    }
                }
                break;

                case 'A':
                case 'a':
                {
                    switch (value[space + 2])
                    {
                    case 'p':
                    case 'P':
                    {
                        switch (value[space + 3])
                        {
                        case 'r':
                        case 'R':
                        {
                            month = 4;
                        }
                        break;

                        default:
                            throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(rR)范围内", (node.Segment.Offset + 2).ToString(), (node.Segment.Offset + 2).ToString()));
                        }
                    }
                    break;

                    case 'U':
                    case 'u':
                    {
                        switch (value[space + 3])
                        {
                        case 'g':
                        case 'G':
                        {
                            month = 8;
                        }
                        break;

                        default:
                            throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(gG)范围内", (node.Segment.Offset + 2).ToString(), (node.Segment.Offset + 2).ToString()));
                        }
                    }
                    break;

                    default:
                        throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(pPUu)范围内", (node.Segment.Offset + 1).ToString(), (node.Segment.Offset + 1).ToString()));
                    }
                }
                break;

                case 'S':
                case 's':
                {
                    switch (value[space + 2])
                    {
                    case 'e':
                    case 'E':
                    {
                        switch (value[space + 3])
                        {
                        case 'p':
                        case 'P':
                        {
                            month = 9;
                        }
                        break;

                        default:
                            throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(pP)范围内", (node.Segment.Offset + 2).ToString(), (node.Segment.Offset + 2).ToString()));
                        }
                    }
                    break;

                    default:
                        throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(eE)范围内", (node.Segment.Offset + 1).ToString(), (node.Segment.Offset + 1).ToString()));
                    }
                }
                break;

                case 'O':
                case 'o':
                {
                    switch (value[space + 2])
                    {
                    case 'c':
                    case 'C':
                    {
                        switch (value[space + 3])
                        {
                        case 't':
                        case 'T':
                        {
                            month = 10;
                        }
                        break;

                        default:
                            throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(tT)范围内", (node.Segment.Offset + 2).ToString(), (node.Segment.Offset + 2).ToString()));
                        }
                    }
                    break;

                    default:
                        throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(cC)范围内", (node.Segment.Offset + 1).ToString(), (node.Segment.Offset + 1).ToString()));
                    }
                }
                break;

                case 'N':
                case 'n':
                {
                    switch (value[space + 2])
                    {
                    case 'o':
                    case 'O':
                    {
                        switch (value[space + 3])
                        {
                        case 'v':
                        case 'V':
                        {
                            month = 11;
                        }
                        break;

                        default:
                            throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(vV)范围内", (node.Segment.Offset + 2).ToString(), (node.Segment.Offset + 2).ToString()));
                        }
                    }
                    break;

                    default:
                        throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(oO)范围内", (node.Segment.Offset + 1).ToString(), (node.Segment.Offset + 1).ToString()));
                    }
                }
                break;

                case 'D':
                case 'd':
                {
                    switch (value[space + 2])
                    {
                    case 'e':
                    case 'E':
                    {
                        switch (value[space + 3])
                        {
                        case 'c':
                        case 'C':
                        {
                            month = 12;
                        }
                        break;

                        default:
                            throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(cC)范围内", (node.Segment.Offset + 2).ToString(), (node.Segment.Offset + 2).ToString()));
                        }
                    }
                    break;

                    default:
                        throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处,取值只能在(eE)范围内", (node.Segment.Offset + 1).ToString(), (node.Segment.Offset + 1).ToString()));
                    }
                }
                break;
                }

                #endregion month

                /*获取年份*/
                space += 3;
                for (var i = space; i < value.Length; i++)
                {
                    if (value[i] == ' ')
                    {
                        break;
                    }

                    space++;
                }
                int year = GetGigitInZeroToNightChar(value[space + 1]) * 1000 + GetGigitInZeroToNightChar(value[space + 2]) * 100 + GetGigitInZeroToNightChar(value[space + 3]) * 10 + GetGigitInZeroToNightChar(value[space + 4]);

                /*获取小时*/
                space += 4;
                for (var i = space; i < value.Length; i++)
                {
                    if (value[i] == ' ')
                    {
                        break;
                    }

                    space++;
                }

                int hour = GetGigitInZeroToNightChar(value[space + 1]) * 10 + GetGigitInZeroToNightChar(value[space + 2]);

                /*获取分钟*/
                int minute = GetGigitInZeroToNightChar(value[space + 4]) * 10 + GetGigitInZeroToNightChar(value[space + 5]);

                /*获取秒数*/
                int second = GetGigitInZeroToNightChar(value[space + 7]) * 10 + GetGigitInZeroToNightChar(value[space + 8]);

                return(new DateTime(year, month, day, hour, minute, second));
            }
            catch (OverflowException)
            {
                throw new ArgumentOutOfRangeException(node.Key, string.Format("数值溢出,在字符{0}至{1}处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }
            catch
            {
                throw new ArgumentNullException(string.Format("读取发现错误,在{0}至{1}字符处", node.Segment.Offset.ToString(), (node.Segment.Offset + node.Segment.Count).ToString()));
            }
        }
예제 #12
0
        /// <summary>
        /// 在流中读取字节后转换为对象
        /// </summary>
        /// <param name="setting">配置项</param>
        /// <param name="reader">字符读取器</param>
        /// <param name="node">节点流内容</param>
        /// <param name="checkNullValue">是否检查空值</param>
        /// <returns></returns>
        public override Guid Parse(IDeserializerReader reader, JsonDeserializeSetting setting, IContentNode node, bool checkNullValue)
        {
            if (node == null)
            {
                return(Guid.Empty);
            }

            var value = node.GetValue();

            if (value.IsNullOrEmpty)
            {
                return(Guid.Empty);
            }

            if (checkNullValue && this.IsNullValue(value))
            {
                return(Guid.Empty);
            }

            if (value.Length == 32)
            {
                /*9c43f081-a003-4b6f-b1fd-a5e8017c6b2b*/
                var buffer = new char[36];
                for (var i = 0; i < 8; i++)
                {
                    buffer[i] = value[i];
                }

                buffer[8] = '-';

                for (var i = 9; i < 13; i++)
                {
                    buffer[i] = value[i - 1];
                }

                buffer[13] = '-';

                for (var i = 14; i < 18; i++)
                {
                    buffer[i] = value[i - 2];
                }

                buffer[18] = '-';

                for (var i = 19; i < 23; i++)
                {
                    buffer[i] = value[i - 3];
                }

                buffer[23] = '-';

                for (var i = 24; i < 36; i++)
                {
                    buffer[i] = value[i - 4];
                }

                return(new Guid(new string(buffer)));
            }

            if (value.Length == 36)
            {
                if (value[8] != '-' || value[13] != '-' || value[18] != '-' || value[23] != '-')
                {
                    throw new ArgumentException("字符中处在8,13,18,23索引的值应该为'-'");
                }

                return(new Guid(value.ToString()));
            }

            return(Guid.Empty);
        }