Click here to Skip to main content
15,884,836 members
Articles / Programming Languages / C#

A Raw Serializer

Rate me:
Please Sign up or sign in to vote.
4.81/5 (32 votes)
8 Jan 2006CPOL14 min read 126.9K   1.5K   110  
Replace the BinaryFormatter with this class for compact (nullable) value type serialization.
using System;
using System.IO;
using System.Runtime.InteropServices;

using Clifton.Tools.Data;

using Vts.UnitTest;

// An interesting read: http://x5.tsiokos.com/

namespace UnitTests
{
	[TestFixture]
	public class NullableTypeTests
	{
		MemoryStream ms;
		RawSerializer rs;
		RawDeserializer rd;

		[SetUp]
		public void Setup()
		{
			ms = new MemoryStream();
			rs = new RawSerializer(ms);
			rd = new RawDeserializer(ms);
		}

		[Test]
		public void BoxedNullable()
		{
			object anInt = 5;
			object aNullInt = null;
			rs.SerializeNullable(anInt);
			rs.SerializeNullable(aNullInt);
			rs.Flush();
			ms.Position = 0;
			anInt = rd.DeserializeNullable(typeof(int));
			aNullInt = rd.DeserializeNullable(typeof(int));
			Assertion.Assert((int)anInt == 5, "non-null nullable failed.");
			Assertion.Assert(aNullInt == null, "null nullable failed.");
		}
	
		[Test]
		public void Bool()
		{
			bool? b1 = true;
			bool? b2 = null;
			rs.Serialize(b1);
			rs.Serialize(b2);
			rs.Flush();
			ms.Position = 0;
			b1 = rd.DeserializeNBool();
			b2 = rd.DeserializeNBool();

			// must compare b1 to true, as it can also be null.
			Assertion.Assert(b1==true, "non-null nullable bool failed");
			Assertion.Assert(b2 == null, "null nullable bool failed");
		}

		[Test]
		public void Byte()
		{
			byte? val1=byte.MaxValue;
			byte? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNByte();
			val2=rd.DeserializeNByte();
			Assertion.Assert(val1==byte.MaxValue, "non-null nullable byte failed");
			Assertion.Assert(val2==null, "null nullable byte failed");
		}

		[Test]
		public void Char()
		{
			char? val1=char.MaxValue;
			char? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNChar();
			val2=rd.DeserializeNChar();
			Assertion.Assert(val1==char.MaxValue, "non-null nullable char failed");
			Assertion.Assert(val2==null, "null nullable char failed");
		}

		[Test]
		public void Decimal()
		{
			decimal? val1=decimal.MaxValue;
			decimal? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNDecimal();
			val2=rd.DeserializeNDecimal();
			Assertion.Assert(val1==decimal.MaxValue, "non-null nullable decimal failed");
			Assertion.Assert(val2==null, "null nullable decimal failed");
		}

		[Test]
		public void Double()
		{
			double? val1=double.MaxValue;
			double? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNDouble();
			val2=rd.DeserializeNDouble();
			Assertion.Assert(val1==double.MaxValue, "non-null nullable double failed");
			Assertion.Assert(val2==null, "null nullable double failed");
		}

		[Test]
		public void Short()
		{
			short? val1=short.MaxValue;
			short? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNShort();
			val2=rd.DeserializeNShort();
			Assertion.Assert(val1==short.MaxValue, "non-null nullable short failed");
			Assertion.Assert(val2==null, "null nullable short failed");
		}

		[Test]
		public void Int()
		{
			int? val1=int.MaxValue;
			int? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNInt();
			val2=rd.DeserializeNInt();
			Assertion.Assert(val1==int.MaxValue, "non-null nullable int failed");
			Assertion.Assert(val2==null, "null nullable int failed");
		}

		[Test]
		public void IntObject()
		{
			int? val1 = int.MaxValue;
			int? val2 = null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position = 0;
			object obj1 = rd.DeserializeNullable(typeof(int));
			object obj2 = rd.DeserializeNullable(typeof(int));
			Assertion.Assert((int)val1 == int.MaxValue, "non-null nullable int failed");
			Assertion.Assert(val2 == null, "null nullable int failed");
		}

		[Test]
		public void Long()
		{
			long? val1=long.MaxValue;
			long? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNLong();
			val2=rd.DeserializeNLong();
			Assertion.Assert(val1==long.MaxValue, "non-null nullable long failed");
			Assertion.Assert(val2==null, "null nullable long failed");
		}

		[Test]
		public void SByte()
		{
			sbyte? val1=sbyte.MaxValue;
			sbyte? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNSByte();
			val2=rd.DeserializeNSByte();
			Assertion.Assert(val1==sbyte.MaxValue, "non-null nullable sByte failed");
			Assertion.Assert(val2==null, "null nullable sByte failed");
		}

		[Test]
		public void Float()
		{
			float? val1=float.MaxValue;
			float? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNFloat();
			val2=rd.DeserializeNFloat();
			Assertion.Assert(val1==float.MaxValue, "non-null nullable float failed");
			Assertion.Assert(val2==null, "null nullable float failed");
		}

		[Test]
		public void UShort()
		{
			ushort? val1=ushort.MaxValue;
			ushort? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNUShort();
			val2=rd.DeserializeNUShort();
			Assertion.Assert(val1==ushort.MaxValue, "non-null nullable uShort failed");
			Assertion.Assert(val2==null, "null nullable uShort failed");
		}

		[Test]
		public void UInt()
		{
			uint? val1=uint.MaxValue;
			uint? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNUInt();
			val2=rd.DeserializeNUInt();
			Assertion.Assert(val1==uint.MaxValue, "non-null nullable uInt failed");
			Assertion.Assert(val2==null, "null nullable uInt failed");
		}

		[Test]
		public void ULong()
		{
			ulong? val1=ulong.MaxValue;
			ulong? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNULong();
			val2=rd.DeserializeNULong();
			Assertion.Assert(val1==ulong.MaxValue, "non-null nullable uLong failed");
			Assertion.Assert(val2==null, "null nullable uLong failed");
		}

		[Test]
		public void GuidStruct()
		{
			Guid? val1 = Guid.NewGuid();
			Guid saveGuid = (Guid)val1;
			Guid? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNGuid();
			val2=rd.DeserializeNGuid();
			Assertion.Assert(val1==saveGuid, "non-null nullable Guid failed");
			Assertion.Assert(val2==null, "null nullable Guid failed");
		}

		[Test]
		public void DateTimeStruct()
		{
			DateTime? val1=DateTime.Now;
			DateTime saveDateTime = (DateTime)val1;
			DateTime? val2=null;
			rs.Serialize(val1);
			rs.Serialize(val2);
			rs.Flush();
			ms.Position=0;
			val1=rd.DeserializeNDateTime();
			val2=rd.DeserializeNDateTime();
			Assertion.Assert(val1==saveDateTime, "non-null nullable DateTime failed");
			Assertion.Assert(val2==null, "null nullable DateTime failed");
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions