예제 #1
0
        /// <summary>
        /// Converts objectToSerialize to an array of bytes using the data processors and options provided.
        /// </summary>
        /// <typeparam name="T">Type of object to serialize</typeparam>
        /// <param name="objectToSerialise">Object to serialize</param>
        /// <param name="dataProcessors">Data processors to apply to serialised data.  These will be run in index order i.e. low index to high</param>
        /// <param name="options">Options dictionary for serialisation/data processing</param>
        /// <returns>Serialized array of bytes</returns>
        public StreamSendWrapper SerialiseDataObject <T>(T objectToSerialise, List <DataProcessor> dataProcessors, Dictionary <string, string> options)
        {
            if (objectToSerialise == null)
            {
                throw new ArgumentNullException("objectToSerialise");
            }

            Type objectToSerialiseType = objectToSerialise.GetType();

            if (objectToSerialiseType == typeof(Stream))
            {
                throw new ArgumentException("Parameter should not be of type Stream. Consider using StreamSendWrapper as send object instead.", "objectToSerialise");
            }
            else if (objectToSerialiseType == typeof(StreamSendWrapper))
            {
                return(StreamSendWrapperSerializer.SerialiseStreamSendWrapper(objectToSerialise as StreamSendWrapper, dataProcessors, options));
            }

            StreamSendWrapper baseRes = null;

            baseRes = ArraySerializer.SerialiseArrayObject(objectToSerialise, dataProcessors, options);

            //if the object was an array baseres will != null
            if (baseRes != null)
            {
                return(baseRes);
            }
            else
            {
                return(SerialiseGeneralObject <T>(objectToSerialise, dataProcessors, options));
            }
        }
예제 #2
0
        /// <summary>
        /// Serializes StreamSendWrapper to a StreamSendWrapper possibly using provided data processors.  If there are no data processor streamSendWrapperToSerialize will be returned.
        /// </summary>
        /// <param name="streamSendWrapperToSerialize">StreamSendWrapper to serialize</param>
        /// <param name="dataProcessors">The compression provider to use</param>
        /// <param name="options">Options to be used during serialization and processing of data</param>
        /// <returns>The serialized and compressed bytes of objectToSerialize</returns>
        public static StreamSendWrapper SerialiseStreamSendWrapper(StreamSendWrapper streamSendWrapperToSerialize, List <DataProcessor> dataProcessors, Dictionary <string, string> options)
        {
            //If we have no data processing to do we can simply return the serialised bytes
            if (dataProcessors == null || dataProcessors.Count == 0)
            {
                return(streamSendWrapperToSerialize);
            }

            var array = new byte[streamSendWrapperToSerialize.Length];

            using (MemoryStream tempStream = new MemoryStream(array))
                streamSendWrapperToSerialize.ThreadSafeStream.CopyTo(tempStream, streamSendWrapperToSerialize.Start, streamSendWrapperToSerialize.Length, 8000);

            return(ArraySerializer.SerialiseArrayObject(array, dataProcessors, options));
        }