Click here to Skip to main content
15,892,298 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 283.4K   1.2K   175  
A framework for object serializiation/deserialization that is many times faster and yields a compact output.
/*

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

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

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

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

namespace NeXtreme.CommonTypes
{
    /*       Complex graphs with shared and circular references        */

    [CLSCompliant(false)]
    public enum SampleEnumeration : uint
    {
        None,
        One,
        Two,
        Three,
        Four
    }

    /// <summary>
    /// Demonstrates a type that contains a variety of members.
    /// </summary>
    [Serializable]
    public class OneBigType
    {
        public char mChar;
        public byte mByte;
        public short mShort;
        public int mInt;
        public double mDbl;
        public DateTime mDt;
        public Guid mGuid;
        public String mString;
        public byte[] mBytes;          // A byte array member
        public DataPoint mDp;          // A value type member
        public OneBigType mSelf;       // A reference type member

        public OneBigType()
        {
            mString = mGuid.ToString();
            mBytes = mGuid.ToByteArray();
            mSelf = this;
        }
   }

    [Serializable]
    public class CustomSerializable : ISerializable
    {
        Hashtable hashtable = new Hashtable();

        public CustomSerializable()
        {
            hashtable[0] = new Guid[32];
            hashtable[1] = hashtable[0];
            hashtable[2] = hashtable[1];
        }

        private CustomSerializable(SerializationInfo info, StreamingContext context)
        {
            info.GetInt32("val1");
            hashtable = (Hashtable)info.GetValue("hashtable", typeof(Hashtable));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("val1", 1);
            info.AddValue("hashtable", hashtable);
        }
    }

    /// <summary>
    /// Demonstrates a custom type that has nested <see cref="INxSerializable"/> members.
    /// </summary>
    [Serializable]
    public class CustomNxSerializable : INxSerializable
    {
        public CustomNxSerializableChild mChild1;
        public CustomNxSerializableChild mChild2;
        public int val;

        public CustomNxSerializable()
        {
            mChild1 = mChild2 = new CustomNxSerializableChild(this);
        }

        public CustomNxSerializableChild Child1
        {
            get { return mChild1; }
        }
        public CustomNxSerializableChild Child2
        {
            get { return mChild2; }
        }

        /// <summary>
        /// Implementation of INxSerializable.Serialize
        /// </summary>
        void INxSerializable.Serialize(INxBinaryWriter w)
        {
            w.WriteObject(mChild1);
            w.WriteObject(mChild2);
            w.WriteObject(val);
        }

        /// <summary>
        /// Implementation of INxSerializable.Deserialize
        /// </summary>
        void INxSerializable.Deserialize(INxBinaryReader r)
        {
            mChild1 = (CustomNxSerializableChild)r.ReadObject();
            mChild2 = (CustomNxSerializableChild)r.ReadObject();
            val = (int)r.ReadObject();
        }
    }

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

        internal CustomNxSerializableChild()
        {
            this.mParent = null;
        }
        public CustomNxSerializableChild(CustomNxSerializable parent)
        {
            this.mParent = parent;
        }

        public CustomNxSerializable Parent
        {
            get { return mParent; }
        }

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

        /// <summary>
        /// Implementation of INxSerializable.Deserialize
        /// </summary>
        void INxSerializable.Deserialize(INxBinaryReader r)
        {
            mParent = (CustomNxSerializable)r.ReadObject();
        }
    }
}

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