public void ShouldEqualExpect(byte type, int tag)
        {
            var buffer = Unpooled.Buffer(2);

            sut.WriteHead(buffer, type, tag);
            var options = new TarsConvertOptions();

            sut.ReadHead(buffer, options);
            Assert.Equal(type, options.TarsType);
            Assert.Equal(tag, options.Tag);
        }
Exemple #2
0
        public override void Serialize(IByteBuffer obj, IByteBuffer buffer, TarsConvertOptions options)
        {
            if (obj == null)
            {
                return;
            }

            int len = obj.ReadableBytes;

            headHandler.Reserve(buffer, 8 + len);
            headHandler.WriteHead(buffer, TarsStructType.ByteArray, options.Tag);
            headHandler.WriteHead(buffer, TarsStructType.Byte, 0);
            buffer.WriteInt(len);
            buffer.WriteBytes(obj);
        }
Exemple #3
0
 public override void Serialize(T obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (obj != null)
     {
         headHandler.Reserve(buffer, 2);
         headHandler.WriteHead(buffer, TarsStructType.StructBegin, options.Tag);
         foreach (var kv in properties)
         {
             options.Tag = kv.Key;
             convert.Serialize(kv.Value.GetValue(obj), kv.Value.GetMemberInfo().PropertyType, buffer, options);
         }
         headHandler.Reserve(buffer, 2);
         headHandler.WriteHead(buffer, TarsStructType.StructEnd, options.Tag);
     }
 }
Exemple #4
0
        public void SameKeyShouldBeSingle(short version, Codec codec, int tag)
        {
            var buffer  = Unpooled.Buffer(1);
            var options = new TarsConvertOptions()
            {
                Codec   = codec,
                Version = version,
                Tag     = tag
            };

            headHandler.Reserve(buffer, 8);
            headHandler.WriteHead(buffer, TarsStructType.Map, options.Tag);
            buffer.WriteInt(2);
            for (int i = 0; i < 2; i++)
            {
                options.Tag = 0;
                sut.Serialize("str", buffer, options);
                options.Tag = 1;
                sut.Serialize("ok" + i.ToString(), buffer, options);
            }
            headHandler.ReadHead(buffer, options);
            var result = sut.Deserialize <IDictionary <string, string> >(buffer, options);

            Assert.Single(result);
            Assert.Equal("ok1", result["str"]);
            Assert.Equal(tag, options.Tag);
            Assert.Equal(TarsStructType.Map, options.TarsType);
        }
 public override void Serialize(double obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (!options.HasValue)
     {
         return;
     }
     headHandler.Reserve(buffer, 10);
     if (obj == 0)
     {
         headHandler.WriteHead(buffer, TarsStructType.Zero, options.Tag);
     }
     else
     {
         headHandler.WriteHead(buffer, TarsStructType.Double, options.Tag);
         buffer.WriteDouble(obj);
     }
 }
Exemple #6
0
        public override void Serialize(string obj, IByteBuffer buffer, TarsConvertOptions options)
        {
            if (obj == null)
            {
                return;
            }
            var count = options.Encoding.GetByteCount(obj);

            if (count > 255)
            {
                headHandler.WriteHead(buffer, TarsStructType.String4, options.Tag);
                buffer.WriteInt(count);
                buffer.WriteString(obj, options.Encoding);
            }
            else
            {
                headHandler.WriteHead(buffer, TarsStructType.String1, options.Tag);
                buffer.WriteByte(count);
                buffer.WriteString(obj, options.Encoding);
            }
        }
 public override void Serialize(IList <T> obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (obj != null)
     {
         headHandler.Reserve(buffer, 8);
         headHandler.WriteHead(buffer, TarsStructType.List, options.Tag);
         buffer.WriteInt(obj.Count);
         var op = options.Create();
         op.Tag = 0;
         foreach (var item in obj)
         {
             convert.Serialize(item, buffer, op);
         }
     }
 }
 public override void Serialize(IDictionary <K, V> obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (obj != null)
     {
         headHandler.Reserve(buffer, 8);
         headHandler.WriteHead(buffer, TarsStructType.Map, options.Tag);
         buffer.WriteInt(obj.Count);
         foreach (var kv in obj)
         {
             options.Tag = 0;
             convert.Serialize(kv.Key, buffer, options);
             options.Tag = 1;
             convert.Serialize(kv.Value, buffer, options);
         }
     }
 }
 public override void Serialize(long obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (!options.HasValue)
     {
         return;
     }
     if (obj >= int.MinValue && obj <= int.MaxValue)
     {
         convert.Serialize((int)obj, buffer, options);
     }
     else
     {
         headHandler.Reserve(buffer, 10);
         headHandler.WriteHead(buffer, TarsStructType.Long, options.Tag);
         buffer.WriteLong(obj);
     }
 }
Exemple #10
0
 public override void Serialize(int obj, IByteBuffer buffer, TarsConvertOptions options)
 {
     if (!options.HasValue)
     {
         return;
     }
     if (obj >= short.MinValue && obj <= short.MaxValue)
     {
         convert.Serialize((short)obj, buffer, options);
     }
     else
     {
         headHandler.Reserve(buffer, 6);
         headHandler.WriteHead(buffer, TarsStructType.Int, options.Tag);
         buffer.WriteInt(obj);
     }
 }