Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

Commit

Permalink
All primitive converters with serialization based on stream
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-pyrzyk committed Jul 7, 2017
1 parent 4cd3ab4 commit 7206b6e
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 64 deletions.
7 changes: 3 additions & 4 deletions src/BinaryFormatter/TypeConverter/BaseTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ public void Serialize(T obj, Stream stream)
{
if (obj == null) throw new ArgumentNullException(nameof(obj));

byte[] objectBytes = ProcessSerialize(obj);
byte[] objectType = BitConverter.GetBytes((ushort)Type);

stream.Write(objectType);
stream.Write(objectBytes);

WriteObjectToStream(obj, stream);
}

public override void Serialize(object obj, Stream stream)
Expand Down Expand Up @@ -49,7 +48,7 @@ public override object DeserializeToObject(byte[] stream, ref int offset)
}

protected abstract int GetTypeSize();
protected abstract byte[] ProcessSerialize(T obj);
protected abstract void WriteObjectToStream(T obj, Stream stream);
protected abstract T ProcessDeserialize(byte[] stream, ref int offset);

protected virtual SerializedType GetPackageType(byte[] stream, ref int offset)
Expand Down
7 changes: 5 additions & 2 deletions src/BinaryFormatter/TypeConverter/BoolConverter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class BoolConverter : BaseTypeConverter<bool>
{
protected override byte[] ProcessSerialize(bool obj)
protected override void WriteObjectToStream(bool obj, Stream stream)
{
return BitConverter.GetBytes(obj);
byte[] data = BitConverter.GetBytes(obj);
stream.Write(data);
}

protected override bool ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
11 changes: 5 additions & 6 deletions src/BinaryFormatter/TypeConverter/ByteArrayConverter.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class ByteArrayConverter : BaseTypeConverter<byte[]>
{
private int Size { get; set; }

protected override byte[] ProcessSerialize(byte[] obj)
protected override void WriteObjectToStream(byte[] obj, Stream stream)
{
Size = obj.Length;
byte[] lengthBytes = BitConverter.GetBytes(Size);

byte[] serializedStringWithSize = new byte[sizeof(int) + Size];
Array.Copy(lengthBytes, 0, serializedStringWithSize, 0, lengthBytes.Length);
Array.Copy(obj, 0, serializedStringWithSize, lengthBytes.Length, obj.Length);

return serializedStringWithSize;
stream.Write(lengthBytes);
stream.Write(obj);
}

protected override byte[] ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
7 changes: 5 additions & 2 deletions src/BinaryFormatter/TypeConverter/ByteConverter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class ByteConverter : BaseTypeConverter<byte>
{
protected override byte[] ProcessSerialize(byte obj)
protected override void WriteObjectToStream(byte obj, Stream stream)
{
return BitConverter.GetBytes(obj);
byte[] data = BitConverter.GetBytes(obj);
stream.Write(data);
}

protected override byte ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
7 changes: 5 additions & 2 deletions src/BinaryFormatter/TypeConverter/CharConverter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class CharConverter : BaseTypeConverter<char>
{
protected override byte[] ProcessSerialize(char obj)
protected override void WriteObjectToStream(char obj, Stream stream)
{
return BitConverter.GetBytes(obj);
byte[] data = BitConverter.GetBytes(obj);
stream.Write(data);
}

protected override char ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
7 changes: 5 additions & 2 deletions src/BinaryFormatter/TypeConverter/DatetimeConverter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class DatetimeConverter : BaseTypeConverter<DateTime>
{
protected override byte[] ProcessSerialize(DateTime obj)
protected override void WriteObjectToStream(DateTime obj, Stream stream)
{
return BitConverter.GetBytes(obj.Ticks);
byte[] data = BitConverter.GetBytes(obj.Ticks);
stream.Write(data);
}

protected override DateTime ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
10 changes: 3 additions & 7 deletions src/BinaryFormatter/TypeConverter/DecimalConverter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.IO;
using System.IO;
using System.Linq;
using System.Text;
using BinaryFormatter.Types;

namespace BinaryFormatter.TypeConverter
Expand All @@ -10,14 +8,12 @@ internal class DecimalConverter : BaseTypeConverter<decimal>
{
private int Size { get; set; } = 0;

protected override byte[] ProcessSerialize(decimal obj)
protected override void WriteObjectToStream(decimal obj, Stream stream)
{
string sdecimal = obj.ToString("F");
Size = sdecimal.Length;
var ms = new MemoryStream();

new StringConverter().Serialize(sdecimal, ms);
return ms.ToArray();
new StringConverter().Serialize(sdecimal, stream);
}

protected override decimal ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
7 changes: 5 additions & 2 deletions src/BinaryFormatter/TypeConverter/DoubleConverter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class DoubleConverter : BaseTypeConverter<double>
{
protected override byte[] ProcessSerialize(double obj)
protected override void WriteObjectToStream(double obj, Stream stream)
{
return BitConverter.GetBytes(obj);
byte[] data = BitConverter.GetBytes(obj);
stream.Write(data);
}

protected override double ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
7 changes: 5 additions & 2 deletions src/BinaryFormatter/TypeConverter/FloatConverter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class FloatConverter : BaseTypeConverter<float>
{
protected override byte[] ProcessSerialize(float obj)
protected override void WriteObjectToStream(float obj, Stream stream)
{
return BitConverter.GetBytes(obj);
byte[] data = BitConverter.GetBytes(obj);
stream.Write(data);
}

protected override float ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
26 changes: 14 additions & 12 deletions src/BinaryFormatter/TypeConverter/IEnumerableConverter.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
using System;
using BinaryFormatter.Types;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Reflection;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class IEnumerableConverter : BaseTypeConverter<object>
{
private int Size { get; set; }

protected override byte[] ProcessSerialize(object obj)
protected override void WriteObjectToStream(object obj, Stream stream)
{
byte[] collectionAsByteArray = null;
var objAsIEnumerable = (obj as IEnumerable<object>);
var objAsIEnumerable = obj as IEnumerable<object>;
if (objAsIEnumerable != null)
{
BinaryConverter converter = new BinaryConverter();
BinaryConverter converter = new BinaryConverter();
List<byte> listAsArray = new List<byte>();

foreach (var sourceElementValue in objAsIEnumerable)
Expand All @@ -25,17 +26,19 @@ protected override byte[] ProcessSerialize(object obj)
continue;

object elementValue = (sourceElementValue as IEnumerable<object>);
if(elementValue == null)
if (elementValue == null)
{
elementValue = sourceElementValue;
} else
}
else
{
List<object> collectionOfObjects = new List<object>();
foreach (var item in (elementValue as IEnumerable<object>))
foreach (var item in (IEnumerable<object>)elementValue)
{
collectionOfObjects.Add(item);
}
elementValue = collectionOfObjects as IEnumerable<object>;

elementValue = collectionOfObjects;
}

Type elementType = elementValue.GetType();
Expand All @@ -59,11 +62,10 @@ protected override byte[] ProcessSerialize(object obj)
listAsArray.AddRange(elementAsBytes);
}

collectionAsByteArray = listAsArray.ToArray();
byte[] collectionAsByteArray = listAsArray.ToArray();
Size = collectionAsByteArray.Length;
stream.Write(collectionAsByteArray);
}

return collectionAsByteArray;
}

protected override object ProcessDeserialize(byte[] stream, ref int offset)
Expand All @@ -79,7 +81,7 @@ protected override object ProcessDeserialize(byte[] stream, ref int offset)
{
int sizeTypeInfo = BitConverter.ToInt32(stream, offset);
offset += sizeof(int);
if(sizeTypeInfo == 0)
if (sizeTypeInfo == 0)
{
continue;
}
Expand Down
7 changes: 5 additions & 2 deletions src/BinaryFormatter/TypeConverter/IntConverter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class IntConverter : BaseTypeConverter<int>
{
protected override byte[] ProcessSerialize(int obj)
protected override void WriteObjectToStream(int obj, Stream stream)
{
return BitConverter.GetBytes(obj);
byte[] data = BitConverter.GetBytes(obj);
stream.Write(data);
}

protected override int ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
7 changes: 5 additions & 2 deletions src/BinaryFormatter/TypeConverter/LongConverter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class LongConverter : BaseTypeConverter<long>
{
protected override byte[] ProcessSerialize(long obj)
protected override void WriteObjectToStream(long obj, Stream stream)
{
return BitConverter.GetBytes(obj);
byte[] data = BitConverter.GetBytes(obj);
stream.Write(data);
}

protected override long ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
7 changes: 5 additions & 2 deletions src/BinaryFormatter/TypeConverter/SByteConverter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class SByteConverter : BaseTypeConverter<sbyte>
{
protected override byte[] ProcessSerialize(sbyte obj)
protected override void WriteObjectToStream(sbyte obj, Stream stream)
{
return BitConverter.GetBytes(obj);
byte[] data = BitConverter.GetBytes(obj);
stream.Write(data);
}

protected override sbyte ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
7 changes: 5 additions & 2 deletions src/BinaryFormatter/TypeConverter/ShortConverter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class ShortConverter : BaseTypeConverter<short>
{
protected override byte[] ProcessSerialize(short obj)
protected override void WriteObjectToStream(short obj, Stream stream)
{
return BitConverter.GetBytes(obj);
byte[] data = BitConverter.GetBytes(obj);
stream.Write(data);
}

protected override short ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
12 changes: 5 additions & 7 deletions src/BinaryFormatter/TypeConverter/StringConverter.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
using System;
using System.IO;
using System.Text;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class StringConverter : BaseTypeConverter<string>
{
private int Size { get; set; }

protected override byte[] ProcessSerialize(string obj)
protected override void WriteObjectToStream(string obj, Stream stream)
{
byte[] objBytes = Encoding.UTF8.GetBytes(obj);
byte[] sizeBytes = BitConverter.GetBytes(objBytes.Length);
Size = objBytes.Length;

byte[] serializedStringWithSize = new byte[sizeof(int) + objBytes.Length];

Array.ConstrainedCopy(sizeBytes, 0, serializedStringWithSize, 0, sizeBytes.Length);
Array.ConstrainedCopy(objBytes, 0, serializedStringWithSize, sizeBytes.Length, objBytes.Length);

return serializedStringWithSize;
stream.Write(sizeBytes);
stream.Write(objBytes);
}

protected override string ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
7 changes: 5 additions & 2 deletions src/BinaryFormatter/TypeConverter/UIntConverter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.IO;
using BinaryFormatter.Types;
using BinaryFormatter.Utils;

namespace BinaryFormatter.TypeConverter
{
internal class UIntConverter : BaseTypeConverter<uint>
{
protected override byte[] ProcessSerialize(uint obj)
protected override void WriteObjectToStream(uint obj, Stream stream)
{
return BitConverter.GetBytes(obj);
byte[] data = BitConverter.GetBytes(obj);
stream.Write(data);
}

protected override uint ProcessDeserialize(byte[] stream, ref int offset)
Expand Down
Loading

0 comments on commit 7206b6e

Please sign in to comment.