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
120 lines (107 loc) · 3.97 KB
/
XmlSerializer.cs
File metadata and controls
120 lines (107 loc) · 3.97 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
#if !LITE
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
namespace ServiceStack.Text
{
public class XmlSerializer
{
private static readonly XmlWriterSettings XWSettings = new XmlWriterSettings();
private static readonly XmlReaderSettings XRSettings = new XmlReaderSettings();
public static XmlSerializer Instance = new XmlSerializer();
public XmlSerializer(bool omitXmlDeclaration = false, int maxCharsInDocument = 1024 * 1024)
{
XWSettings.Encoding = PclExport.Instance.GetUTF8Encoding(false);
XWSettings.OmitXmlDeclaration = omitXmlDeclaration;
XRSettings.MaxCharactersInDocument = maxCharsInDocument;
}
private static object Deserialize(string xml, Type type)
{
try
{
var stringReader = new StringReader(xml);
using (var reader = XmlReader.Create(stringReader, XRSettings))
{
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, XWSettings))
{
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
{
using (var xw = XmlWriter.Create(writer, XWSettings))
{
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 (obj == null) return;
using (var xw = XmlWriter.Create(stream, XWSettings))
{
var serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(xw, obj);
}
}
}
}
#endif