コード例 #1
0
        public override void Serialize(ValueStream stream, ushort data)
        {
            var union = new UnifiedUnion(data);

            stream.WriteByte(union.Byte1);
            stream.WriteByte(union.Byte2);
        }
コード例 #2
0
        public override void Serialize(ValueStream stream, string data)
        {
            var union = new UnifiedUnion(data.Length);

            stream.WriteByte(union.Byte1);
            stream.WriteByte(union.Byte2);
            stream.WriteByte(union.Byte3);
            stream.WriteByte(union.Byte4);

            var stringBuffer = Encoding.UTF8.GetBytes(data);

            stream.Write(stringBuffer, 0, stringBuffer.Length);
        }
コード例 #3
0
        public override void Serialize(ValueStream stream, ICollection elements)
        {
            var elementType = elements.GetType().GetGenericArguments()[0];

            var union = new UnifiedUnion(elements.Count);

            stream.WriteByte(union.Byte1);
            stream.WriteByte(union.Byte2);
            stream.WriteByte(union.Byte3);
            stream.WriteByte(union.Byte4);

            foreach (var element in elements)
            {
                serializer.Serialize(stream, element, elementType);
            }
        }
コード例 #4
0
        public override void Serialize(ValueStream stream, Array elements)
        {
            var elementType = elements.GetType().GetElementType();

            var union = new UnifiedUnion(elements.Length);

            stream.WriteByte(union.Byte1);
            stream.WriteByte(union.Byte2);
            stream.WriteByte(union.Byte3);
            stream.WriteByte(union.Byte4);

            for (var i = 0; i < elements.Length; i++)
            {
                serializer.Serialize(stream, elements.GetValue(i), elementType);
            }
        }
コード例 #5
0
        public override void Serialize(ValueStream stream, IList elements)
        {
            var elementType = elements.GetType().GetGenericArguments()[0];

            var union = new UnifiedUnion(elements.Count);

            stream.WriteByte(union.Byte1);
            stream.WriteByte(union.Byte2);
            stream.WriteByte(union.Byte3);
            stream.WriteByte(union.Byte4);

            for (var i = 0; i < elements.Count; i++)
            {
                serializer.Serialize(stream, elements[i], elementType);
            }
        }
コード例 #6
0
        public override void Serialize(ValueStream stream, IEnumerable data)
        {
            var elements    = data.Cast <object>().ToList();
            var elementType = data.GetType().GetGenericArguments()[0];

            var union = new UnifiedUnion(elements.Count);

            stream.WriteByte(union.Byte1);
            stream.WriteByte(union.Byte2);
            stream.WriteByte(union.Byte3);
            stream.WriteByte(union.Byte4);

            foreach (var element in elements)
            {
                serializer.Serialize(stream, element, elementType);
            }
        }
コード例 #7
0
        public void ReadByteWriteByteWorks()
        {
            using (var stream = new ValueStream())
            {
                stream.WriteByte(1);
                stream.WriteByte(2);
                stream.WriteByte(3);
                stream.WriteByte(4);
                stream.WriteByte(5);

                stream.Seek(0);

                Assert.Equal(1, stream.ReadByte());
                Assert.Equal(2, stream.ReadByte());
                Assert.Equal(3, stream.ReadByte());
                Assert.Equal(4, stream.ReadByte());
                Assert.Equal(5, stream.ReadByte());
            }
        }
コード例 #8
0
        public override void Serialize(ValueStream stream, long data)
        {
            var union = new UnifiedUnion(data);

            stream.WriteByte(union.Byte1);
            stream.WriteByte(union.Byte2);
            stream.WriteByte(union.Byte3);
            stream.WriteByte(union.Byte4);
            stream.WriteByte(union.Byte5);
            stream.WriteByte(union.Byte6);
            stream.WriteByte(union.Byte7);
            stream.WriteByte(union.Byte8);
        }
コード例 #9
0
        public override void Serialize(ValueStream stream, DateTime data)
        {
            var binary = data.ToBinary();

            var union = new UnifiedUnion(binary);

            stream.WriteByte(union.Byte1);
            stream.WriteByte(union.Byte2);
            stream.WriteByte(union.Byte3);
            stream.WriteByte(union.Byte4);
            stream.WriteByte(union.Byte5);
            stream.WriteByte(union.Byte6);
            stream.WriteByte(union.Byte7);
            stream.WriteByte(union.Byte8);
        }
コード例 #10
0
 public override void Serialize(ValueStream stream, byte data)
 {
     stream.WriteByte(data);
 }
コード例 #11
0
 public override void Serialize(ValueStream stream, bool data)
 {
     stream.WriteByte((byte)(data ? 1 : 0));
 }
コード例 #12
0
        public void Serialize(ValueStream stream, object data, Type dataTypeOverride)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(nameof(Serializer));
            }

            if (dataTypeOverride == null)
            {
                throw new ArgumentNullException(nameof(dataTypeOverride));
            }

            if (data == null)
            {
                stream.WriteByte(0);
                return;
            }

            var dataType = data.GetType();

            if (!dataType.IsValueType || Nullable.GetUnderlyingType(dataType) != null)
            {
                stream.WriteByte(1);
            }

            var dto = converterContainer.TryGetDto(dataType);

            if (dto == null)
            {
                dataType = dataTypeOverride;
                dto      = converterContainer.TryGetDto(dataTypeOverride);
            }
            else
            {
                if (dto.Converter != null)
                {
                    dto.Converter.SerializeObject(stream, data);
                    return;
                }
            }

            if (dto == null)
            {
                stream.WriteByte(0);
            }
            else
            {
                stream.WriteByte(1);

                var union = new UnifiedUnion(dto.EntityAlias);
                stream.WriteByte(union.Byte1);
                stream.WriteByte(union.Byte2);
                stream.WriteByte(union.Byte3);
                stream.WriteByte(union.Byte4);
            }

            foreach (var property in EnsureProperties(dataType))
            {
                Serialize(stream, property.GetValue(data), property.PropertyType);
            }
        }