Click here to Skip to main content
15,888,521 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.5K   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 System.IO;
using System.Threading;

namespace AltSerialize
{    
    /// <summary>
    /// Static methods for easy, thread-safe serialization.
    /// </summary>
    public static class Serializer
    {        
        #region Properties

        private static ByteSerializer _serializer = new ByteSerializer();

        /// <summary>
        /// Gets or sets the default serialization flags.
        /// This method defaults to None.
        /// </summary>
        public static SerializeFlags DefaultSerializeFlags
        {
            get { return _serializer.DefaultSerializeFlags; }
            set { _serializer.DefaultSerializeFlags = value; }
        }

        /// <summary>
        /// Gets the default string encoding to use for serialization.
        /// </summary>
        public static Encoding DefaultEncoding
        {
            get { return _serializer.DefaultEncoding; }
            set { _serializer.DefaultEncoding = value; }
        }        
        
        #endregion

        #region Public Methods

        /// <summary>
        /// Serializes an object using the default serialization flags
        /// and returns a byte array of the result.
        /// </summary>
        /// <param name="anObject">The object to serialize.</param>
        /// <returns>Returns a byte array of the serialized object.</returns>
        public static byte[] Serialize(object anObject)
        {
            return _serializer.Serialize(anObject);
        }

        /// <summary>
        /// Serializes an object and returns a byte array of the result.
        /// </summary>
        /// <param name="anObject">The object to serialize.</param>
        /// <param name="flags">Flags to control the serialization.</param>
        /// <returns>Returns a byte array of the serialized object.</returns>
        public static byte[] Serialize(object anObject, Type objectType)
        {
            return _serializer.Serialize(anObject, objectType);
        }

        /// <summary>
        /// Deserializes an object using the default serialization flags
        /// and returns the result.
        /// </summary>
        /// <param name="bytes">Array of bytes containing the serialized object.</param>
        /// <param name="objectType">The object type contained in the serialized byte array.</param>
        /// /// <param name="flags">Flags to control the deserialization.</param>
        /// <returns>Returns the deserialized object.</returns>
        public static object Deserialize(byte[] bytes, Type objectType)
        {
            return _serializer.Deserialize(bytes, objectType);
        }

        /// <summary>
        /// Deserializes an object.
        /// </summary>
        /// <param name="bytes">Array of bytes containing the serialized object.</param>
        /// <returns>Returns the deserialized object.</returns>
        public static object Deserialize(byte[] bytes)
        {
            return _serializer.Deserialize(bytes);
        }

        /// <summary>
        /// Adds an object to the serialization cache.
        /// </summary>
        /// <remarks>This method makes a permanant addition to the serialization class.
        /// Any time the serializer encounters the object, it will use the cached reference
        /// instead of serializing the entire object.</remarks>
        /// <param name="cachedObject">Object to cache.</param>
        public static void CacheObject(object cachedObject)
        {
            _serializer.CacheObject(cachedObject);
        }

        #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 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