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
123 lines (109 loc) · 4.17 KB
/
XmlSerializer.cs
File metadata and controls
123 lines (109 loc) · 4.17 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
#if !LITE
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
namespace ServiceStack.Text
{
public class XmlSerializer
{
public static readonly XmlWriterSettings XmlWriterSettings = new XmlWriterSettings();
public static readonly XmlReaderSettings XmlReaderSettings = new XmlReaderSettings();
public static XmlSerializer Instance = new XmlSerializer();
public XmlSerializer(bool omitXmlDeclaration = false, int maxCharsInDocument = 1024 * 1024)
{
XmlWriterSettings.Encoding = PclExport.Instance.GetUTF8Encoding(false);
XmlWriterSettings.OmitXmlDeclaration = omitXmlDeclaration;
XmlReaderSettings.MaxCharactersInDocument = maxCharsInDocument;
//Prevent XML bombs by default: https://msdn.microsoft.com/en-us/magazine/ee335713.aspx
XmlReaderSettings.DtdProcessing = DtdProcessing.Prohibit;
}
private static object Deserialize(string xml, Type type)
{
try
{
var stringReader = new StringReader(xml);
using (var reader = XmlReader.Create(stringReader, XmlReaderSettings))
{
var serializer = new 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);
}
public static T DeserializeFromString<T>(string xml)
{
var type = typeof(T);
return (T)Deserialize(xml, type);
}
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 = MemoryStreamFactory.GetStream())
{
using (var xw = XmlWriter.Create(ms, XmlWriterSettings))
{
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($"Error serializing object of type {@from.GetType().FullName}", ex);
}
}
public static void SerializeToWriter<T>(T value, TextWriter writer)
{
try
{
using (var xw = XmlWriter.Create(writer, XmlWriterSettings))
{
var serializer = new DataContractSerializer(value.GetType());
serializer.WriteObject(xw, value);
}
}
catch (Exception ex)
{
throw new SerializationException($"Error serializing object of type {value.GetType().FullName}", ex);
}
}
public static void SerializeToStream(object obj, Stream stream)
{
if (obj == null) return;
using (var xw = XmlWriter.Create(stream, XmlWriterSettings))
{
var serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(xw, obj);
}
}
}
}
#endif