Beispiel #1
0
        /// <summary>
        /// Loads and deserializes the HIT from the specified reader
        /// </summary>
        /// <param name="reader">The input reader</param>
        /// <param name="format">The format of the file</param>
        public HIT DeserializeHIT(TextReader reader, MTurkSerializationFormat format)
        {
            HIT ret = null;
            if (reader == null)
            {
                throw new ArgumentNullException("reader", "No reader specified to load object from");
            }

            object o = GetFormatter(format).Parse(reader, typeof(HITData));

            if (o is HIT)
            {
                ret = (HIT)o;
            }
            else if (o is HITData)
            {
                ret = ((HITData)o).Proxy;
            }
            else
            {
                throw new InvalidDataException("Expected data of type hit. Got " + o.GetType().FullName);
            }

            return ret;
        }
Beispiel #2
0
        /// <summary>
        /// Loads and deserializes a HIT from the specified file (or content string)
        /// </summary>
        /// <param name="fileNameOrContent">The file or content containing the object in the 
        /// specified format</param>
        /// <param name="format">The format of the file</param>
        public HIT DeserializeHIT(string fileNameOrContent, MTurkSerializationFormat format)
        {
            if (string.IsNullOrEmpty(fileNameOrContent))
            {
                throw new ArgumentException("No filename or content specified to load object from", "fileNameOrContent");
            }

            if (fileNameOrContent[0] == '<' || !File.Exists(fileNameOrContent))
            {
                using (StringReader reader = new StringReader(fileNameOrContent))
                {
                    return DeserializeHIT(reader, format);
                }
            }
            else
            {
                using (StreamReader reader = new StreamReader(fileNameOrContent))
                {
                    return DeserializeHIT(reader, format);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Serializes the HIT in the specified format and writes it to the specified writer
        /// </summary>
        /// <param name="hit">HIT to serialize</param>
        /// <param name="writer">The output writer.</param>
        /// <param name="format">The format to use for object serialization</param>
        public void SerializeHIT(HIT hit, TextWriter writer, MTurkSerializationFormat format)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer", "No text writer specified to serialize object to");
            }

            GetFormatter(format).Format(writer, new HITData(hit));
        }
Beispiel #4
0
        /// <summary>
        /// Returns the formatter implementation for the specified format
        /// </summary>
        private static IModelObjectFormatter GetFormatter(MTurkSerializationFormat format)
        {
            IModelObjectFormatter formatter = null;
            if (format == MTurkSerializationFormat.Xml)
            {
                formatter = XmlFormatter.Instance;
            }
            else if (format == MTurkSerializationFormat.Property)
            {
                formatter = PropertyFormatter.Instance;
            }
            else
            {
                throw new InvalidOperationException("Not implemented. Persisting objects to file currently is only supported for XML and property files");
            }

            return formatter;
        }
Beispiel #5
0
        /// <summary>
        /// Serializes the HIT in the specified format and saves it to the specified file
        /// </summary>
        /// <param name="hit">HIT to serialize</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="format">The format to use for serialization</param>
        public void SerializeHIT(HIT hit, string fileName, MTurkSerializationFormat format)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException("No filename specified to save object to", "fileName");
            }

            using (StreamWriter writer = new StreamWriter(fileName))
            {
                SerializeHIT(hit, writer, format);
            }
        }