示例#1
0
        /// <summary>
        ///     是否目标类型,如果不是直接跳过
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private TypeCheckState IsDestArrayType(TsonDataType type)
        {
            if (Ver == 1)
            {
                return(TypeCheckState.Success);
            }
            var srcType = ReadType();

            if (srcType == TsonDataType.Empty)
            {
                return(TypeCheckState.Empty);
            }
            if (srcType == TsonDataType.Nil)
            {
                return(TypeCheckState.Nil);
            }
            if (srcType != TsonDataType.Array)
            {
                SkipByType(srcType);
                return(TypeCheckState.State);
            }

            if (ReadType() == type)
            {
                return(TypeCheckState.Success);
            }
            Postion--;
            SkipByType(srcType);
            return(TypeCheckState.State);
        }
示例#2
0
 /// <summary>
 ///     写入值
 /// </summary>
 /// <param name="index"> 序号</param>
 /// <param name="value">值 </param>
 /// <param name="type">类型</param>
 /// <param name="write">写入方法 </param>
 public void Write <T>(byte index, T[] value, TsonDataType type, Action <T> write)
 {
     if (value == null || value.Length == 0)
     {
         return;
     }
     WriteIndex(index);
     Write(value, type, write);
 }
示例#3
0
 /// <summary>
 ///     写入值
 /// </summary>
 /// <param name="index"> 序号</param>
 /// <param name="value">值 </param>
 /// <param name="type">类型</param>
 /// <param name="write">写入方法 </param>
 public void Write <T>(byte index, List <T> value, TsonDataType type, Action <T> write)
 {
     if (value == null || value.Count == 0)
     {
         return;
     }
     WriteIndex(index);
     Write(value, type, write);
 }
示例#4
0
        /// <summary>
        ///     根据类型跳过
        /// </summary>
        /// <param name="type"></param>
        private void SkipByType(TsonDataType type)
        {
            switch (type)
            {
            case TsonDataType.Boolean:
            case TsonDataType.Byte:
                ++Postion;
                break;

            case TsonDataType.UShort:
            case TsonDataType.Short:
                Postion += 2;
                break;

            case TsonDataType.UInt:
            case TsonDataType.Int:
            case TsonDataType.Float:
                Postion += 4;
                break;

            case TsonDataType.Long:
            case TsonDataType.ULong:
            case TsonDataType.Decimal:
            case TsonDataType.Double:
            case TsonDataType.DateTime:
                Postion += 8;
                break;

            case TsonDataType.Guid:
                Postion += 16;
                break;

            case TsonDataType.String:
                Postion += ReadLen();
                break;

            case TsonDataType.Object:
                ReadVer();
                SkipToEnd();
                break;

            case TsonDataType.Array:
                type = ReadType();
                var size = ReadLen();
                for (var i = 0; i < size; i++)
                {
                    SkipByType(type);
                }
                break;
            }
        }
示例#5
0
        /// <summary>
        ///     读一个数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="read"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public T[] ReadArray <T>(Func <T> read, TsonDataType type)
        {
            var state = IsDestArrayType(type);

            if (state == TypeCheckState.State || state == TypeCheckState.Nil)
            {
                return(null);
            }
            if (state == TypeCheckState.Empty)
            {
                return(new T[0]);
            }
            return(ReadArray(read));
        }
示例#6
0
        public void Write <T>(T[] array, TsonDataType type, Action <T> write)
        {
            if (array == null || array.Length == 0)
            {
                //WriteType(TsonFieldType.Nil);
                return;
            }

            //WriteType(TsonFieldType.Array);
            //WriteType(type);
            WriteLen(array.Length);
            foreach (var item in array)
            {
                write(item);
            }
        }
示例#7
0
        /// <summary>
        ///     写入值
        /// </summary>
        /// <param name="array"> </param>
        /// <param name="type"></param>
        /// <param name="write"></param>
        /// <returns> </returns>
        public void Write <T>(List <T> array, TsonDataType type, Action <T> write)
        {
            if (array == null || array.Count == 0)
            {
                WriteType(TsonDataType.Nil);
                return;
            }

            WriteType(TsonDataType.Array);
            WriteType(type);
            WriteLen(array.Count);
            foreach (var item in array)
            {
                write(item);
            }
        }
示例#8
0
        /// <summary>
        ///     读一个列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="read"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public List <T> ReadList <T>(Func <T> read, TsonDataType type)
        {
            var state = IsDestArrayType(type);

            if (state == TypeCheckState.State || state == TypeCheckState.Nil)
            {
                return(null);
            }
            var array = new List <T>();

            if (state == TypeCheckState.Empty)
            {
                return(array);
            }
            ReadList(array, read);
            return(array);
        }
示例#9
0
        /// <summary>
        ///     读一个列表
        /// </summary>
        /// <typeparam name="TList"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="read"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public TList ReadList <TList, TValue>(Func <TValue> read, TsonDataType type)
            where TList : class, IList <TValue>, new()
        {
            var state = IsDestArrayType(type);

            if (state == TypeCheckState.State || state == TypeCheckState.Nil)
            {
                return(null);
            }
            var array = new TList();

            if (state == TypeCheckState.Empty)
            {
                return(array);
            }
            ReadList(array, read);
            return(array);
        }
示例#10
0
        /// <summary>
        ///     是否目标类型,如果不是直接跳过
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private TypeCheckState IsDestType(TsonDataType type)
        {
            var srcType = ReadType();

            if (srcType == TsonDataType.Empty)
            {
                return(TypeCheckState.Empty);
            }
            if (srcType == TsonDataType.Nil)
            {
                return(TypeCheckState.Nil);
            }
            if (srcType == type)
            {
                return(TypeCheckState.Success);
            }
            SkipByType(srcType);
            return(TypeCheckState.State);
        }
示例#11
0
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="buffer"></param>
        public TsonDeserializerBase(byte[] buffer)
        {
            if (buffer == null || buffer.Length < 3 || buffer[buffer.Length - 2] != 0xFF || buffer[buffer.Length - 1] != 0xFF)
            {
                throw new NullReferenceException("buffer不能为空且必须为有效的TSON数据");
            }
            Buffer  = buffer;
            Postion = 0;
            Ver     = ReadVer();
            if (Ver > 2)
            {
                throw new NullReferenceException("buffer必须为有效的TSON数据");
            }
            var type = ReadByte();

            if (type > (byte)TsonDataType.DateTime && type < (byte)TsonDataType.Object)
            {
                throw new NullReferenceException("buffer必须为有效的TSON数据");
            }
            DataType = (TsonDataType)type;
        }
示例#12
0
 /// <summary>
 /// 生成范围
 /// </summary>
 /// <param name="s"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 public static ScopeBase CreateScope(ITsonSerializer s, TsonDataType type = TsonDataType.Object) => new TsonObjectSerializeScope(s, type);
示例#13
0
 private TsonObjectScope(ITsonDeserializer s)
 {
     deserializer = s;
     DataType     = s.Begin(out Ver);
 }
示例#14
0
 /// <summary>
 ///     写入字段类型
 /// </summary>
 /// <param name="type"></param>
 public void WriteType(TsonDataType type)
 {
     WriteValue((byte)type);
 }
示例#15
0
 /// <summary>
 /// 构造
 /// </summary>
 public TsonSerializerV1(TsonDataType type = TsonDataType.Object) : base(1, type)
 {
 }
示例#16
0
 private TsonObjectSerializeScope(ITsonSerializer s, TsonDataType type)
 {
     serializer = s;
     s.Begin(type);
 }
示例#17
0
 /// <summary>
 /// 构造
 /// </summary>
 /// <param name="ver"></param>
 /// <param name="type"></param>
 protected TsonSerializerBase(byte ver, TsonDataType type)
 {
     Ver = ver;
     Begin(type);
 }
示例#18
0
 /// <summary>
 ///     开始
 /// </summary>
 public void Begin(TsonDataType type = TsonDataType.Object)
 {
     Stream.WriteByte(Ver);
     Stream.WriteByte((byte)type);
 }