forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataContractDeserializer.cs
More file actions
78 lines (66 loc) · 2.31 KB
/
DataContractDeserializer.cs
File metadata and controls
78 lines (66 loc) · 2.31 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
using System;
using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Xml;
using ServiceStack.DesignPatterns.Serialization;
namespace ServiceStack.ServiceModel.Serialization
{
public class DataContractDeserializer : IStringDeserializer
{
/// <summary>
/// Default MaxStringContentLength is 8k, and throws an exception when reached
/// </summary>
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
private readonly XmlDictionaryReaderQuotas quotas;
#endif
public static DataContractDeserializer Instance
= new DataContractDeserializer(
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
new XmlDictionaryReaderQuotas { MaxStringContentLength = 1024 * 1024, }
#endif
);
public DataContractDeserializer(
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
XmlDictionaryReaderQuotas quotas=null
#endif
)
{
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
this.quotas = quotas;
#endif
}
public object Parse(string xml, Type type)
{
try
{
var bytes = Encoding.UTF8.GetBytes(xml);
#if MONOTOUCH
using (var reader = XmlDictionaryReader.CreateTextReader(bytes, null))
#elif SILVERLIGHT
using (var reader = XmlDictionaryReader.CreateTextReader(bytes, XmlDictionaryReaderQuotas.Max))
#else
using (var reader = XmlDictionaryReader.CreateTextReader(bytes, this.quotas))
#endif
{
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 T Parse<T>(string xml)
{
var type = typeof(T);
return (T)Parse(xml, type);
}
public T DeserializeFromStream<T>(Stream stream)
{
var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T));
return (T)serializer.ReadObject(stream);
}
}
}