/// <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); }
/// <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); }
/// <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); }
/// <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; } }
/// <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)); }
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); } }
/// <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); } }
/// <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); }
/// <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); }
/// <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); }
/// <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; }
/// <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);
private TsonObjectScope(ITsonDeserializer s) { deserializer = s; DataType = s.Begin(out Ver); }
/// <summary> /// 写入字段类型 /// </summary> /// <param name="type"></param> public void WriteType(TsonDataType type) { WriteValue((byte)type); }
/// <summary> /// 构造 /// </summary> public TsonSerializerV1(TsonDataType type = TsonDataType.Object) : base(1, type) { }
private TsonObjectSerializeScope(ITsonSerializer s, TsonDataType type) { serializer = s; s.Begin(type); }
/// <summary> /// 构造 /// </summary> /// <param name="ver"></param> /// <param name="type"></param> protected TsonSerializerBase(byte ver, TsonDataType type) { Ver = ver; Begin(type); }
/// <summary> /// 开始 /// </summary> public void Begin(TsonDataType type = TsonDataType.Object) { Stream.WriteByte(Ver); Stream.WriteByte((byte)type); }