protected override void SerializeInternal(object obj, SerializationStream stream) { BinaryConverter converter = new BinaryConverter(); KeyValuePair <object, object> objAsKeyValuePair = TypeHelper.CastFrom(obj); byte[] dataKey = converter.Serialize(objAsKeyValuePair.Key); stream.WriteWithLengthPrefix(dataKey); byte[] dataValue = converter.Serialize(objAsKeyValuePair.Value); stream.WriteWithLengthPrefix(dataValue); }
protected override void SerializeInternal(object obj, SerializationStream stream) { var objectAsCollection = (ICollection)obj; byte[] collectionSize = BitConverter.GetBytes(objectAsCollection.Count); stream.Write(collectionSize); BinaryConverter converter = new BinaryConverter(); foreach (var sourceElementValue in objectAsCollection) { if (sourceElementValue == null) { continue; } object elementValue = (sourceElementValue as IEnumerable <object>); if (elementValue == null) { elementValue = sourceElementValue; } else { List <object> collectionOfObjects = new List <object>(); foreach (var item in (IList)elementValue) { collectionOfObjects.Add(item); } elementValue = collectionOfObjects; } byte[] data = converter.Serialize(elementValue); stream.WriteWithLengthPrefix(data); } }
protected override void SerializeInternal(Enum obj, SerializationStream stream) { Type enumUnderlyingType = Enum.GetUnderlyingType(obj.GetType()); var underlyingValue = Convert.ChangeType(obj, enumUnderlyingType); BinaryConverter converter = new BinaryConverter(); byte[] data = converter.Serialize(underlyingValue); stream.WriteWithLengthPrefix(data); }
public override void Serialize(object obj, SerializationStream stream) { byte[] objectType = BitConverter.GetBytes((ushort)Type); stream.Write(objectType); if (obj != null) { if (!DestinationType.IsBaseTypeSupportedBySerializer()) { byte[] typeInfo = Encoding.UTF8.GetBytes(obj.GetType().AssemblyQualifiedName); stream.WriteWithLengthPrefix(typeInfo); } SerializeInternal((T)obj, stream); } }
public void WriteWithLengthPrefix_WritesAllElementsToTheStream() { // Arrange var stream = new MemoryStream(); var serializationStream = new SerializationStream(stream); var data = Encoding.UTF8.GetBytes("Hello world"); var expectedBytesHeader = BitConverter.GetBytes(data.Length); // Act serializationStream.WriteWithLengthPrefix(data); // Assert byte[] dataFromStream = stream.ToArray(); dataFromStream.Should().HaveCount(data.Length + sizeof(int)); byte[] lengthPrefix = dataFromStream.Take(sizeof(int)).ToArray(); expectedBytesHeader.Should().Equal(lengthPrefix); byte[] dataBytes = dataFromStream.Skip(sizeof(int)).ToArray(); data.Should().Equal(dataBytes); }
protected override void SerializeInternal(string obj, SerializationStream stream) { byte[] objBytes = Encoding.UTF8.GetBytes(obj); stream.WriteWithLengthPrefix(objBytes); }
protected override void SerializeInternal(byte[] obj, SerializationStream stream) { stream.WriteWithLengthPrefix(obj); }
protected override void SerializeInternal(BigInteger obj, SerializationStream stream) { byte[] data = obj.ToByteArray(); stream.WriteWithLengthPrefix(data); }
protected override void SerializeInternal(Uri obj, SerializationStream stream) { byte[] data = Encoding.UTF8.GetBytes(obj.AbsoluteUri); stream.WriteWithLengthPrefix(data); }