/// <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)); } }
/// <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)); }
/// <summary> /// Converts a memory stream containing bytes previously serialized and processed using data processors to an object of provided type /// </summary> /// <typeparam name="T">Type of object to deserialize to</typeparam> /// <param name="receivedObjectStream">Byte array containing serialized and compressed object</param> /// <param name="dataProcessors">Data processors to apply to serialised data. These will be run in reverse order i.e. high index to low</param> /// <param name="options">Options dictionary for serialisation/data processing</param> /// <returns>The deserialized object</returns> public T DeserialiseDataObject <T>(MemoryStream receivedObjectStream, List <DataProcessor> dataProcessors, Dictionary <string, string> options) { if (receivedObjectStream == null) { throw new ArgumentNullException("receivedObjectStream"); } //Ensure the stream is at the beginning receivedObjectStream.Seek(0, SeekOrigin.Begin); //Try to deserialise using the array helper. If the result is a primitive array this call will return an object object baseRes = null; baseRes = ArraySerializer.DeserialiseArrayObject(receivedObjectStream, typeof(T), dataProcessors, options); if (baseRes != null) { return((T)baseRes); } else { return(DeserialiseGeneralObject <T>(receivedObjectStream, dataProcessors, options)); } }