Пример #1
0
        /// <summary>
        /// Serialises the specified object.
        /// </summary>
        /// <param name="obj">A T that is the object to serialise.</param>
        /// <returns>An array of byte containing the serialised object.</returns>
        public static byte[] Serialise(T obj)
        {
            DataContractJsonSerializer Serialiser;

            byte[]       ReturnValue = null;
            MemoryStream TargetStream;

            // Create the serialiser
            Serialiser = CreateSerialiser();

            // Serialise the object
            TargetStream = new MemoryStream();
            Serialiser.WriteObject(TargetStream, obj);
            ReturnValue = TargetStream.GetBuffer();

            return(ReturnValue);
        }
Пример #2
0
        /// <summary>
        /// Deserialises the specified data stream.
        /// </summary>
        /// <param name="data">A Stream containing the data to deserialise.</param>
        /// <returns>A T desrialised from the data.</returns>
        public static T Deserialise(Stream data)
        {
            object SourceObject;
            DataContractJsonSerializer Serialiser;
            T ReturnValue = default(T);

            // Create the serialiser
            Serialiser = CreateSerialiser();

            // Desrialise the object and check it's valid
            SourceObject = Serialiser.ReadObject(data);
            if (SourceObject != null && SourceObject is T)
            {
                ReturnValue = (T)SourceObject;
            }

            return(ReturnValue);
        }