Click here to Skip to main content
15,885,032 members
Articles / Programming Languages / C#

AltSerializer - An Alternate Binary Serializer

Rate me:
Please Sign up or sign in to vote.
4.93/5 (27 votes)
19 Sep 20066 min read 141.4K   1.9K   69  
The AltSerializer is a replacement for the binary serializer built in to .NET.
using System;
using System.Collections.Generic;
using System.Text;

using AltSerialize;
using NUnit.Framework;
using AltSerializeTest.TestObjects;

namespace AltSerializeTest
{
    /// <summary>
    /// Tests object types.
    /// </summary>
    [TestFixture]
    public class TestClasses : SerializerTestBase
    {
        public override string Name
        {
            get { return "Class Test"; }
        }

        [Test]
        public void ClassTest()
        {
            ClassObject myObject = new ClassObject("Hello");
            object ret = SerializeObject(myObject);

            Assert.IsTrue(CompareObject(myObject, ret), "Class object didnt work.");
        }

        [Test]
        public void StructTest()
        {
            StructObject myObject = new StructObject(this);
            myObject.Value1 = 1;
            myObject.Value2 = Int32.MaxValue;
            object ret = SerializeObject(myObject);
            Assert.IsTrue(CompareObject(myObject, ret), "Struct object didnt work.");
        }

        public void ArrayTest(int arraySize)
        {
            ClassObject[] array = new ClassObject[arraySize];
            for (int i = 0; i < arraySize; i++)
            {
                array[i] = new ClassObject(Rand.Next().ToString());
            }
            object resultObject = SerializeObject(array);
            Assert.IsTrue(CompareObject(array, resultObject), "Failed on type " + array.GetType().FullName);
        }

        /// <summary>
        /// Test all value type arrays with random data.
        /// </summary>
        [Test]
        public void ArrayTest()
        {
            foreach (Type type in ValueTypes)
            {
                ArrayTest(10);
                ArrayTest(32);
            }
        }

        public void ObjectArrayTest(int arraySize)
        {
            object [] array = new object[arraySize];
            for (int i = 0; i < arraySize; i++)
            {
                array[i] = new ClassObject((string)GetRandomData(typeof(string)));
            }
            object resultObject = SerializeObject(array);
            Assert.IsTrue(CompareObject(array,  resultObject), "Failed on type " + array.GetType().FullName);
        }

        /// <summary>
        /// Test all value type arrays with random data.
        /// </summary>
        [Test]
        public void ObjectArrayTest()
        {
            foreach (Type type in ValueTypes)
            {
                ObjectArrayTest(10);
                ObjectArrayTest(32);
            }
        }

        [Test]
        public void NullTest()
        {
            object value = null;
            object result = SerializeObject(value);
            Assert.IsTrue(CompareObject(value, result));
        }

        [Test]
        public void NullArrayTest()
        {
            object[]  value = new object[10];
            object result = SerializeObject(value);

            Assert.IsTrue(CompareObject(value, result));
        }

        [Test]
        public void ComplexObjectTest()
        {
            DebugOut("Testing ComplexObject");
            ComplexObject obj = new ComplexObject(this);
            object result = SerializeObject(obj);
            Assert.IsTrue(CompareObject(obj, result));
        }

        [Test]
        public void NestedObjectTest()
        {
            DebugOut("Testing Recursive Object");
            RecursiveObject obj = new RecursiveObject();
            object result = SerializeObject(obj);
            Assert.IsTrue(CompareObject(obj, result));

            obj.Recurse = new RecursiveObject();
            result = SerializeObject(obj);
            Assert.IsTrue(CompareObject(obj, result));

            obj.Recurse = obj;
            result = SerializeObject(obj);
            Assert.IsTrue(CompareObject(obj, result));
        }
    }

    
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I've been writing code since I started learning BASIC on the Apple //c. Since then, I've learned C, C++, a few assembly languages. I went to college for Computer Science and Mathematics at the University of Texas at Dallas.

Comments and Discussions