forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataContractSerializer.cs
More file actions
117 lines (103 loc) · 3.75 KB
/
DataContractSerializer.cs
File metadata and controls
117 lines (103 loc) · 3.75 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
#if !LITE
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using ServiceStack.Text;
namespace ServiceStack.Serialization
{
public partial class DataContractSerializer : IStringSerializer
{
private static readonly Encoding Encoding = Encoding.UTF8;// new UTF8Encoding(true);
/// <summary>
/// Default MaxStringContentLength is 8k, and throws an exception when reached
/// </summary>
#if !(SL5 || __IOS__ || XBOX || ANDROID || PCL)
private readonly XmlDictionaryReaderQuotas quotas;
#endif
public static DataContractSerializer Instance
= new DataContractSerializer(
#if !(SL5 || __IOS__ || XBOX || ANDROID || PCL || NETSTANDARD1_1)
new XmlDictionaryReaderQuotas { MaxStringContentLength = 1024 * 1024, }
#endif
);
public DataContractSerializer(
#if !(SL5 || __IOS__ || XBOX || ANDROID || PCL)
XmlDictionaryReaderQuotas quotas = null
#endif
)
{
#if !(SL5 || __IOS__ || XBOX || ANDROID || PCL)
this.quotas = quotas;
#endif
}
public string Parse<XmlDto>(XmlDto from, bool indentXml)
{
try
{
if (Equals(@from, default(XmlDto))) return null;
using (var ms = MemoryStreamFactory.GetStream())
{
var serializer = new System.Runtime.Serialization.DataContractSerializer(from.GetType());
#if !(SL5 || __IOS__ || XBOX || ANDROID || PCL || NETSTANDARD1_1)
var xw = new XmlTextWriter(ms, Encoding);
if (indentXml)
{
xw.Formatting = Formatting.Indented;
}
serializer.WriteObject(xw, from);
xw.Flush();
#else
serializer.WriteObject(ms, from);
#endif
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 string SerializeToString<XmlDto>(XmlDto from)
{
return Parse(from, false);
}
public void SerializeToStream(object obj, Stream stream)
{
#if !(SL5 || __IOS__ || XBOX || ANDROID || PCL || NETSTANDARD1_1)
using (var xw = new XmlTextWriter(stream, Encoding))
{
var serializer = new System.Runtime.Serialization.DataContractSerializer(obj.GetType());
serializer.WriteObject(xw, obj);
}
#else
var serializer = new System.Runtime.Serialization.DataContractSerializer(obj.GetType());
serializer.WriteObject(stream, obj);
#endif
}
#if !(SL5 || __IOS__ || XBOX || ANDROID || PCL || NETSTANDARD1_1)
public void CompressToStream<XmlDto>(XmlDto from, Stream stream)
{
using (var deflateStream = new System.IO.Compression.DeflateStream(stream, System.IO.Compression.CompressionMode.Compress))
using (var xw = new XmlTextWriter(deflateStream, Encoding))
{
var serializer = new System.Runtime.Serialization.DataContractSerializer(from.GetType());
serializer.WriteObject(xw, from);
xw.Flush();
}
}
public byte[] Compress<XmlDto>(XmlDto from)
{
using (var ms = new MemoryStream()) //only use MS with .NET's incompat Compression classes
{
CompressToStream(from, ms);
return ms.ToArray();
}
}
#endif
}
}
#endif