Click here to Skip to main content
15,885,216 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 281.9K   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.Configuration;

using NeXtreme.OpenNxSerialization.Surrogates;
using NeXtreme.OpenNxSerialization.Configuration;

namespace NeXtreme.OpenNxSerialization
{
    /// <summary>
    /// Class that helps configuration of the serialization infrastructure using a configuration file
    /// </summary>
    public class NxConfigurator
    {
        /// <summary>
        /// Reads the configuration file and configures the serialization infrastructure
        /// </summary>
        /// <param name="userLevel">Use <paramref name="userLevel"/> to specify which configuration file 
        /// is to be represented by the <see cref="System.Configuration.Configuration"/> object returned by 
        /// <see cref="ConfigurationManager"/> </param>
        /// <param name="sectionName">The section name within the configuration file</param>
        /// <returns>A configured <see cref="NxTypeSurrogateSelector"/> object</returns>
        public static NxTypeSurrogateSelector Configure(ConfigurationUserLevel userLevel, string sectionName)
        {
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(userLevel);
            NxConfigurationSection configSection = config.GetSection(sectionName) as NxConfigurationSection;
            if (configSection == null)
            {
                return null;
            }

            return Configure(configSection);
        }

        /// <summary>
        /// Reads the configuration file and configures the serialization infrastructure
        /// </summary>
        /// <param name="fileName">confgiuration file name</param>
        /// <param name="sectionName">The section name within the configuration file</param>
        /// <returns>A configured <see cref="NxTypeSurrogateSelector"/> object</returns>
        public static NxTypeSurrogateSelector Configure(string fileName, string sectionName)
        {
            if (fileName == null)
                throw new ArgumentNullException("fileName");
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(fileName);
            NxConfigurationSection configSection = config.GetSection(sectionName) as NxConfigurationSection;
            if (configSection == null)
            {
                return null;
            }

            return Configure(configSection);
        }

        /// <summary>
        /// Reads the configuration file and configures the serialization infrastructure
        /// </summary>
        /// <param name="config">A <see cref="System.Configuration.Configuration"/> object</param>
        /// <param name="sectionName">The section name within the <see cref="System.Configuration.Configuration"/> 
        /// object</param>
        /// <returns>A configured <see cref="NxTypeSurrogateSelector"/> object</returns>
        public static NxTypeSurrogateSelector Configure(System.Configuration.Configuration config, string sectionName)
        {
            if (config == null)
                throw new ArgumentNullException("config");
            if (sectionName == null)
                throw new ArgumentNullException("sectionName");

            NxConfigurationSection configSection = config.GetSection(sectionName) as NxConfigurationSection;
            if (configSection == null)
            {
                return null;
            }

            return Configure(configSection);
        }

        /// <summary>
        /// Reads the configuration file and configures the serialization infrastructure
        /// </summary>
        /// <param name="configSection"></param>
        /// <returns>A configured <see cref="NxTypeSurrogateSelector"/> object</returns>
        public static NxTypeSurrogateSelector Configure(NxConfigurationSection configSection)
        {
            if (configSection == null)
                throw new ArgumentNullException("configSection");

            TypeNameConverter convertor = new TypeNameConverter();
            NxTypeSurrogateSelector selector = new NxTypeSurrogateSelector(configSection.UseBuiltinSurrogates);
            for (int i = 0; i < configSection.KnownSurrogates.Count; i++)
            {
                NxKnownSurrogateElement surrogateConfig = configSection.KnownSurrogates[i];
                Type surrogateType = (Type)convertor.ConvertFrom(surrogateConfig.TypeName);
                INxSerializationSurrogate surrogate = 
                    (INxSerializationSurrogate) Activator.CreateInstance(surrogateType, true);

                if (surrogateConfig.TypeHandle != 0)
                {
                    selector.RegisterTypeSurrogate(surrogate, (short)surrogateConfig.TypeHandle);
                }
                else
                {
                    selector.RegisterTypeSurrogate(surrogate);
                }
            }

            NxFormatterServices typeRegistrar = new NxFormatterServices(selector);
            for (int i = 0; i < configSection.KnownTypes.Count; i++)
            {
                NxKnownTypeElement typeConfig = configSection.KnownTypes[i];
                Type type = (Type)convertor.ConvertFrom(typeConfig.TypeName);

                if (typeConfig.TypeHandle != 0)
                {
                    typeRegistrar.RegisterCompactType(type, (short)typeConfig.TypeHandle);
                }
                else
                {
                    typeRegistrar.RegisterCompactType(type);
                }
            }

            return selector;
        }
    }
}

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