~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/ServiceModel/Serialization/XmlSerializableDeserializer.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
 
2
using System;
 
3
using System.IO;
 
4
using System.Text;
 
5
using System.Xml;
 
6
using System.Runtime.Serialization;
 
7
using ServiceStack.DesignPatterns.Serialization;
 
8
 
 
9
namespace ServiceStack.ServiceModel.Serialization
 
10
{
 
11
    public class XmlSerializableDeserializer : IStringDeserializer
 
12
    {
 
13
        public static XmlSerializableDeserializer Instance = new XmlSerializableDeserializer();
 
14
 
 
15
        public To Parse<To>(string xml)
 
16
        {
 
17
            var type = typeof(To);
 
18
            return (To)Parse(xml, type);
 
19
        }
 
20
 
 
21
        public object Parse(string xml, Type type)
 
22
        {
 
23
            try
 
24
            {
 
25
                var bytes = Encoding.UTF8.GetBytes(xml);
 
26
                using (var reader = XmlDictionaryReader.CreateTextReader(bytes, new XmlDictionaryReaderQuotas()))
 
27
                {
 
28
                    var serializer = new System.Xml.Serialization.XmlSerializer(type);
 
29
                    return serializer.Deserialize(reader);
 
30
                }
 
31
            }
 
32
            catch (Exception ex)
 
33
            {
 
34
                throw new SerializationException(string.Format("Error serializing object of type {0}", type.FullName), ex);
 
35
            }
 
36
        }
 
37
 
 
38
        public To Parse<To>(TextReader from)
 
39
        {
 
40
            var type = typeof(To);
 
41
            try
 
42
            {
 
43
                using (from)
 
44
                {
 
45
                    var serializer = new System.Xml.Serialization.XmlSerializer(type);
 
46
                    return (To)serializer.Deserialize(from);
 
47
                }
 
48
            }
 
49
            catch (Exception ex)
 
50
            {
 
51
                throw new SerializationException(string.Format("Error serializing object of type {0}", type.FullName), ex);
 
52
            }
 
53
        }
 
54
 
 
55
        public To Parse<To>(Stream from)
 
56
        {
 
57
            var type = typeof(To);
 
58
            try
 
59
            {
 
60
                using (var reader = XmlDictionaryReader.CreateTextReader(from, new XmlDictionaryReaderQuotas()))
 
61
                {
 
62
                    var serializer = new System.Xml.Serialization.XmlSerializer(type);
 
63
                    return (To)serializer.Deserialize(reader);
 
64
                }
 
65
            }
 
66
            catch (Exception ex)
 
67
            {
 
68
                throw new SerializationException(string.Format("Error serializing object of type {0}", type.FullName), ex);
 
69
            }
 
70
        }
 
71
    }
 
72
}
 
73
#endif