forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlSerializer.cs
More file actions
171 lines (151 loc) · 5.36 KB
/
XmlSerializer.cs
File metadata and controls
171 lines (151 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#if !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE && !MONOTOUCH
using System.IO.Compression;
#endif
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
namespace ServiceStack.Text
{
#if !XBOX
public class XmlSerializer
{
private readonly XmlDictionaryReaderQuotas quotas;
private static readonly XmlWriterSettings XSettings = new XmlWriterSettings();
public static XmlSerializer Instance
= new XmlSerializer(
#if !SILVERLIGHT && !WINDOWS_PHONE && !MONOTOUCH
new XmlDictionaryReaderQuotas { MaxStringContentLength = 1024 * 1024, }
#endif
);
public XmlSerializer(XmlDictionaryReaderQuotas quotas=null, bool omitXmlDeclaration = false)
{
this.quotas = quotas;
XSettings.Encoding = new UTF8Encoding(false);
XSettings.OmitXmlDeclaration = omitXmlDeclaration;
}
private static object Deserialize(string xml, Type type, XmlDictionaryReaderQuotas quotas)
{
try
{
#if WINDOWS_PHONE
StringReader stringReader = new StringReader(xml);
using (var reader = XmlDictionaryReader.Create(stringReader))
{
var serializer = new DataContractSerializer(type);
return serializer.ReadObject(reader);
}
#else
var bytes = Encoding.UTF8.GetBytes(xml);
using (var reader = XmlDictionaryReader.CreateTextReader(bytes, quotas))
{
var serializer = new DataContractSerializer(type);
return serializer.ReadObject(reader);
}
#endif
}
catch (Exception ex)
{
throw new SerializationException("DeserializeDataContract: Error converting type: " + ex.Message, ex);
}
}
public static object DeserializeFromString(string xml, Type type)
{
return Deserialize(xml, type, Instance.quotas);
}
public static T DeserializeFromString<T>(string xml)
{
var type = typeof(T);
return (T)Deserialize(xml, type, Instance.quotas);
}
public static T DeserializeFromReader<T>(TextReader reader)
{
return DeserializeFromString<T>(reader.ReadToEnd());
}
public static T DeserializeFromStream<T>(Stream stream)
{
var serializer = new DataContractSerializer(typeof(T));
return (T)serializer.ReadObject(stream);
}
public static object DeserializeFromStream(Type type, Stream stream)
{
var serializer = new DataContractSerializer(type);
return serializer.ReadObject(stream);
}
public static string SerializeToString<T>(T from)
{
try
{
using (var ms = new MemoryStream())
{
using (var xw = XmlWriter.Create(ms, XSettings))
{
var serializer = new DataContractSerializer(from.GetType());
serializer.WriteObject(xw, from);
xw.Flush();
ms.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(ms);
return reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
throw new SerializationException(string.Format("Error serializing object of type {0}", from.GetType().FullName), ex);
}
}
public static void SerializeToWriter<T>(T value, TextWriter writer)
{
try
{
#if !SILVERLIGHT
using (var xw = new XmlTextWriter(writer))
#else
using (var xw = XmlWriter.Create(writer))
#endif
{
var serializer = new DataContractSerializer(value.GetType());
serializer.WriteObject(xw, value);
}
}
catch (Exception ex)
{
throw new SerializationException(string.Format("Error serializing object of type {0}", value.GetType().FullName), ex);
}
}
public static void SerializeToStream(object obj, Stream stream)
{
#if !SILVERLIGHT
using (var xw = new XmlTextWriter(stream, Encoding.UTF8))
#else
using (var xw = XmlWriter.Create(stream))
#endif
{
var serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(xw, obj);
}
}
#if !SILVERLIGHT && !MONOTOUCH
public static void CompressToStream<TXmlDto>(TXmlDto from, Stream stream)
{
using (var deflateStream = new DeflateStream(stream, CompressionMode.Compress))
using (var xw = new XmlTextWriter(deflateStream, Encoding.UTF8))
{
var serializer = new DataContractSerializer(from.GetType());
serializer.WriteObject(xw, from);
xw.Flush();
}
}
public static byte[] Compress<TXmlDto>(TXmlDto from)
{
using (var ms = new MemoryStream())
{
CompressToStream(from, ms);
return ms.ToArray();
}
}
#endif
}
#endif
}