Click here to Skip to main content
15,886,734 members
Articles / Programming Languages / C#

A Fast/Compact Serialization Framework

Rate me:
Please Sign up or sign in to vote.
4.85/5 (37 votes)
13 Oct 2010GPL35 min read 282K   1.2K   175  
A framework for object serializiation/deserialization that is many times faster and yields a compact output.
/*

OpenNxSerialization Framework
Copyright (C) 2006 - 2007 "NeXtreme Innovations"
[The Next Xtreme Innovations]

This program is free software, distributed under the terms of
the GNU General Public License Version 2. See the "License.txt" file
at the top of the source tree.

*/
using System;
using System.Collections;
using System.IO;
using System.Runtime.InteropServices;

using NeXtreme.OpenNxSerialization;
using NeXtreme.OpenNxSerialization.IO;
using NeXtreme.OpenNxSerialization.Surrogates;

namespace NeXtreme.Benchmark.SampleData
{
    enum SampleEnumeration : uint
    {
        None,
        One,
        Two,
        Three,
        Four
    }

    #region /       How to use complex graphs with shared and circular references        /

    /// <summary>
    /// Demonstrates a custom type that has nested <see cref="INxSerializable"/> members.
    /// </summary>
    [Serializable]
    public class SampleParentClass : INxSerializable
    {
        public SampleChildClass mNested1;
        public SampleChildClass mNested2;
        public int val;

        public SampleParentClass()
        {
            mNested1 = mNested2 = new SampleChildClass(this);
        }

        public SampleChildClass Nested1
        {
            get { return mNested1; }
        }
        public SampleChildClass Nested2
        {
            get { return mNested2; }
        }

        /// <summary>
        /// Implementation of INxSerializable.Serialize
        /// </summary>
        public virtual void Serialize(NxBinaryWriter w)
        {
            w.WriteObject(mNested1);
            w.WriteObject(mNested2);
            w.WriteObject(val);
        }

        /// <summary>
        /// Implementation of INxSerializable.Deserialize
        /// </summary>
        public virtual void Deserialize(NxBinaryReader r)
        {
            mNested1 = (SampleChildClass)r.ReadObject();
            mNested2 = (SampleChildClass)r.ReadObject();
            val = (int)r.ReadObject();
        }
    }

    /// <summary>
    /// Demomstrates sample class that holds reference to <see cref="INxSerializable"/>.
    /// </summary>
    [Serializable]
    public class SampleChildClass : INxSerializable
    {
        public SampleParentClass mParent;

        public SampleChildClass(SampleParentClass parent)
        {
            this.mParent = parent;
        }

        public SampleParentClass Parent
        {
            get { return mParent; }
        }

        /// <summary>
        /// Implementation of INxSerializable.Serialize
        /// </summary>
        public virtual void Serialize(NxBinaryWriter w)
        {
            w.WriteObject(mParent);
        }

        /// <summary>
        /// Implementation of INxSerializable.Deserialize
        /// </summary>
        public virtual void Deserialize(NxBinaryReader r)
        {
            mParent = (SampleParentClass)r.ReadObject();
        }
    }

    #endregion

    #region /       How to write a custom surrogate        /

    [StructLayout(LayoutKind.Sequential), Serializable()]
    public struct DataPoint
    {
        internal double mdX;
        internal double mdY;
        internal double mdZ;
        internal short msX;
        internal short msY;
        internal short msZ;
        internal float mfX;
        internal float mfY;
        internal float mfZ;
    }

    class DataPointSurrogate : NxSerializationSurrogate
    {
        public const short FIXED_ID = 0xCee;

        public DataPointSurrogate()
            : base(typeof(DataPoint))
        {
        }

        public override object Read(NxBinaryReader r)
        {
            DataPoint obj = new DataPoint();
            obj.mdX = r.ReadDouble();
            obj.mdY = r.ReadDouble();
            obj.mdZ = r.ReadDouble();
            obj.msX = r.ReadInt16();
            obj.msY = r.ReadInt16();
            obj.msZ = r.ReadInt16();
            obj.mfX = r.ReadSingle();
            obj.mfY = r.ReadSingle();
            obj.mfZ = r.ReadSingle();
            return obj;
        }

        public override void Write(NxBinaryWriter w, object graph)
        {
            DataPoint obj = (DataPoint)graph;
            w.Write(obj.mdX);
            w.Write(obj.mdY);
            w.Write(obj.mdZ);
            w.Write(obj.msX);
            w.Write(obj.msY);
            w.Write(obj.msZ);
            w.Write(obj.mfX);
            w.Write(obj.mfY);
            w.Write(obj.mfZ);
        }
    }


    /// <summary>
    /// Demostrates a surrogate for a class <see cref="SampleSurrogatedClass"/> that is not compact
    /// serializable otherwise.
    /// </summary>
    class SampleSurrogate : NxSerializationSurrogate
    {
        public SampleSurrogate()
            : base(typeof(SampleSurrogatedClass))
        {
        }

        /// <summary>
        /// Read an object of type <see cref="ActualType"/> from the stream reader
        /// </summary>
        public override object Read(NxBinaryReader r)
        {
            SampleSurrogatedClass obj = new SampleSurrogatedClass();
            obj.title = r.ReadString();
            obj.title2 = r.ReadString();
            obj.title3 = r.ReadString();
            obj.num1 = r.ReadInt32();
            obj.num2 = r.ReadInt32();
            return obj;
        }

        /// <summary>
        /// Write an object of type <see cref="ActualType"/> to the stream writer
        /// </summary>
        public override void Write(NxBinaryWriter w, object graph)
        {
            SampleSurrogatedClass obj = (SampleSurrogatedClass)graph;
            w.Write(obj.title);
            w.Write(obj.title2);
            w.Write(obj.title3);
            w.Write(obj.num1);
            w.Write(obj.num2);
        }
    }

    /// <summary>
    /// Demonstrates a custom type that does not implement <see cref="INxSerializable"/>.
    /// </summary>
    [Serializable]
    class SampleSurrogatedClass
    {
        public const short FIXED_ID = 0xBee;

        internal String title = "C1";
        internal String title2 = "C2";
        internal String title3 = "C3";
        internal int num1;
        internal int num2;

        public SampleSurrogatedClass()
        {
        }
    }

    #endregion

    #region /       How to use an INxSerializable        /

    /// <summary>
    /// Demomstrates a custom type that implements <see cref="INxSerializable"/>.
    /// </summary>
    [Serializable]
    class SampleINxSerializableClass : INxSerializable
    {
        public const short FIXED_ID = 0xFee;

        private string id;
        private SampleSurrogatedClass nested = new SampleSurrogatedClass();
        private Hashtable table = new Hashtable(11);

        /// <summary> Default constructor is not mandatory for types that implement INxSerializable.</summary>
        protected SampleINxSerializableClass() { }

        public SampleINxSerializableClass(String title)
        {
            id = title;
            table[1] = new SampleSurrogatedClass();
            table[2] = new SampleSurrogatedClass();
            table[3] = new SampleSurrogatedClass();
            table[4] = new SampleSurrogatedClass();
        }

        /// <summary>
        /// Implementation of INxSerializable.Serialize
        /// </summary>
        public virtual void Serialize(NxBinaryWriter w)
        {
            w.Write(id);
            w.WriteObject(nested);
            w.WriteObject(table);
        }

        /// <summary>
        /// Implementation of INxSerializable.Deserialize
        /// </summary>
        public virtual void Deserialize(NxBinaryReader r)
        {
            id = r.ReadString();
            nested = (SampleSurrogatedClass)r.ReadObject();
            table = (Hashtable)r.ReadObject();
        }
    }

    #endregion
}

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 GNU General Public License (GPLv3)


Written By
Architect
Pakistan Pakistan
Let a = b ....... (1)
a - b = a - b
a^2 - ab = a^2 - ab
a^2 - ab = a^2 - b^2 (from 1)
a (a - b) = (a + b) (a - b)
a = (a + b) ...... (2)

if a = 1
1 = (1 + 1) (from 1 & 2)
1 = 2 !!

Comments and Discussions