forked from ServiceStackV3/ServiceStack.Contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlSerializer.cs
More file actions
147 lines (129 loc) · 3.88 KB
/
XmlSerializer.cs
File metadata and controls
147 lines (129 loc) · 3.88 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
using System;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
namespace ServiceStack.Text
{
public class XmlSerializer
{
private readonly XmlDictionaryReaderQuotas quotas;
public static XmlSerializer Instance
= new XmlSerializer(new XmlDictionaryReaderQuotas {
MaxStringContentLength = 1024 * 1024,
});
public XmlSerializer(XmlDictionaryReaderQuotas quotas)
{
this.quotas = quotas;
}
private static object Deserialize(string xml, Type type, XmlDictionaryReaderQuotas quotas)
{
try
{
var bytes = Encoding.UTF8.GetBytes(xml);
using (var reader = XmlDictionaryReader.CreateTextReader(bytes, quotas))
{
var serializer = new System.Runtime.Serialization.DataContractSerializer(type);
return serializer.ReadObject(reader);
}
}
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 System.Runtime.Serialization.DataContractSerializer(typeof(T));
return (T)serializer.ReadObject(stream);
}
public static object DeserializeFromStream(Type type, Stream stream)
{
var serializer = new System.Runtime.Serialization.DataContractSerializer(type);
return serializer.ReadObject(stream);
}
public static string SerializeToString<T>(T from)
{
try
{
using (var ms = new MemoryStream())
{
using (var xw = new XmlTextWriter(ms, Encoding.UTF8))
{
// if (false)
// {
// xw.Formatting = Formatting.Indented;
// }
var serializer = new System.Runtime.Serialization.DataContractSerializer(from.GetType());
serializer.WriteObject(xw, from);
xw.Flush();
ms.Seek(0, SeekOrigin.Begin);
using (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
{
using (var xw = new XmlTextWriter(writer))
{
var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
serializer.WriteObject(xw, value);
}
}
catch (Exception ex)
{
throw new SerializationException(string.Format("Error serializing object of type {0}", typeof(T).FullName), ex);
}
}
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 System.Runtime.Serialization.DataContractSerializer(from.GetType());
serializer.WriteObject(xw, from);
xw.Flush();
}
}
public static void SerializeToStream(object obj, Stream stream)
{
using (var xw = new XmlTextWriter(stream, Encoding.UTF8))
{
var serializer = new System.Runtime.Serialization.DataContractSerializer(obj.GetType());
serializer.WriteObject(xw, obj);
}
}
public static byte[] Compress<TXmlDto>(TXmlDto from)
{
using (var ms = new MemoryStream())
{
CompressToStream(from, ms);
return ms.ToArray();
}
}
}
}