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

OpenNxSerialization Framework
Copyright (C) 2006 - 2008 "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.Diagnostics;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;

using NeXtreme.OpenNxSerialization.IO;
using NeXtreme.OpenNxSerialization.Formatters;

namespace NeXtreme.OpenNxSerialization.Surrogates
{
    /// <summary>
    /// Surrogate for <see cref="System.Runtime.Serialization.ISerializable"/> type.
    /// </summary>
    sealed class NxObjRefSerializationSurrogate : NxSerializationSurrogateBase, INxSerializationSurrogate, INxBuiltinSerializationSurrogate
    {
        /// <summary> The constructor used to create instance of surrogated type </summary>
        private ConstructorInfo mConstructor;

        public NxObjRefSerializationSurrogate() : base(typeof(ObjRef))
        {
            mConstructor = ActualType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                  null,
                                                  new Type[] { typeof(SerializationInfo), typeof(StreamingContext) },
                                                  null);
            if (mConstructor == null)
            {
                throw new ArgumentException(String.Format(Resources.Surrogates_NoMatchCons, ActualType.Name));
            }
        }

        public object Read(INxBinaryReader reader)
        {
            SerializationInfo info = (SerializationInfo)reader.ReadObjectAs(typeof(SerializationInfo));
            return mConstructor.Invoke(new object[] { info, reader.Context.StreamingContext });
        }

        public void Write(INxBinaryWriter writer, object graph)
        {
            SerializationInfo info = new SerializationInfo(ActualType, writer.Context.FormatConverter);
            ((ObjRef)graph).GetObjectData(info, writer.Context.StreamingContext);
            writer.WriteObjectAs(info, typeof(SerializationInfo));
        }
    }

    /// <summary>
    /// Surrogate for <see cref="MarshalByRefObject"/> type.
    /// </summary>
    sealed class NxMarshalByRefObjectSerializationSurrogate : NxSerializationSurrogateBase, INxSerializationSurrogate, INxBuiltinSerializationSurrogate
    {
        /// <summary> The constructor used to create instance of surrogated type </summary>
        private ConstructorInfo mConstructor;

        public NxMarshalByRefObjectSerializationSurrogate(Type type) : base(type)
        {
            mConstructor = typeof(ObjRef).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                null,
                                                new Type[] { typeof(SerializationInfo), typeof(StreamingContext) },
                                                null);
            if (mConstructor == null)
            {
                throw new ArgumentException(String.Format(Resources.Surrogates_NoMatchCons, ActualType.Name));
            }
        }

        public object Read(INxBinaryReader reader)
        {
            SerializationInfo info = (SerializationInfo)reader.ReadObjectAs(typeof(SerializationInfo));
            ObjRef refObj = (ObjRef)mConstructor.Invoke(new object[] { info, reader.Context.StreamingContext });
            if (refObj != null)
            {
                return RemotingServices.Unmarshal(refObj);
            }
            return null;
        }

        public void Write(INxBinaryWriter writer, object graph)
        {
            SerializationInfo info = new SerializationInfo(ActualType, writer.Context.FormatConverter);
            if (RemotingServices.IsTransparentProxy(graph))
            {
                RemotingServices.GetRealProxy(graph).GetObjectData(info, writer.Context.StreamingContext);
            }
            else
            {
                RemotingServices.GetObjectData(graph, info, writer.Context.StreamingContext);
            }
            writer.WriteObjectAs(info, typeof(SerializationInfo));
        }
    }

    /// <summary>
    /// Surrogate for <see cref="Header"/> type.
    /// </summary>
    sealed class NxHeaderSerializationSurrogate : NxSerializationSurrogate, INxBuiltinSerializationSurrogate
    {
        public NxHeaderSerializationSurrogate() : base(typeof(Header))
        {
        }

        public override object Instantiate(INxBinaryReader reader)
        {
            return new Header(null, null);
        }

        public override object ReadDirect(INxBinaryReader reader, object graph)
        {
            Header header = (Header)graph;
            header.Name = reader.ReadString();
            header.HeaderNamespace = reader.ReadString();
            header.MustUnderstand = reader.ReadBoolean();
            header.Value = reader.ReadObject();
            return graph;
        }

        public override void WriteDirect(INxBinaryWriter writer, object graph)
        {
            Header header = (Header)graph;
            writer.Write(header.Name);
            writer.Write(header.HeaderNamespace);
            writer.Write(header.MustUnderstand);
            writer.WriteObject(header.Value);
        }
    }

    /// <summary>
    /// Surrogate for <see cref="Header"/>[] type.
    /// </summary>
    sealed class NxHeaderArraySerializationSurrogate : NxSerializationSurrogate, INxBuiltinSerializationSurrogate
    {
        public NxHeaderArraySerializationSurrogate() : base(typeof(Header[])) { }

        public override object Instantiate(INxBinaryReader reader)
        {
            int length = reader.ReadInt32();
            return new Header[length];
        }

        public override object ReadDirect(INxBinaryReader reader, object graph)
        {
            Header[] array = (Header[])graph;
            for (int i = 0; i < array.Length; i++)
                array[i] = (Header) reader.ReadObject();
            return array;
        }

        public override void WriteDirect(INxBinaryWriter writer, object graph)
        {
            Header[] array = (Header[])graph;
            writer.Write(array.Length);
            for (int i = 0; i < array.Length; i++)
                writer.WriteObject(array[i]);
        }
    }
}

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