wasSharpNET – Diff between revs 22 and 27
?pathlinks?
Rev 22 | Rev 27 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | // Based on: Danilow @ https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer/ |
6 | // Based on: Danilow @ https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer/ |
|
7 | |
7 | |
|
8 | using System; |
8 | using System; |
|
9 | using System.Collections.Generic; |
9 | using System.Collections.Generic; |
|
10 | using System.IO; |
10 | using System.IO; |
|
11 | using System.Linq; |
11 | using System.Linq; |
|
12 | using System.Threading; |
12 | using System.Threading; |
|
13 | using System.Xml; |
13 | using System.Xml; |
|
14 | using System.Xml.Linq; |
14 | using System.Xml.Linq; |
|
15 | using System.Xml.Serialization; |
15 | using System.Xml.Serialization; |
|
16 | |
16 | |
|
17 | namespace wasSharpNET.Serialization |
17 | namespace wasSharpNET.Serialization |
|
18 | { |
18 | { |
|
19 | public static class XmlSerializerCache |
19 | public static class XmlSerializerCache |
|
20 | { |
20 | { |
|
- | 21 | private static readonly ReaderWriterLockSlim SerializerCacheLock = |
||
21 | private static readonly ReaderWriterLockSlim SerializerCacheLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
22 | new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); |
|
- | 23 | |
||
22 | private static readonly Dictionary<string, XmlSerializer> SerializerCache = new Dictionary<string, XmlSerializer>(); |
24 | private static readonly Dictionary<string, XmlSerializer> SerializerCache = |
|
- | 25 | new Dictionary<string, XmlSerializer>(); |
||
23 | |
26 | |
|
24 | public static XmlSerializer GetSerializer<T>() |
27 | public static XmlSerializer GetSerializer<T>() |
|
25 | { |
28 | { |
|
26 | return GetSerializer<T>(null); |
29 | return GetSerializer<T>(null); |
|
27 | } |
30 | } |
|
28 | |
31 | |
|
29 | public static XmlSerializer GetSerializer<T>(Type[] ExtraTypes) |
32 | public static XmlSerializer GetSerializer<T>(Type[] ExtraTypes) |
|
30 | { |
33 | { |
|
31 | return GetSerializer(typeof(T), ExtraTypes); |
34 | return GetSerializer(typeof(T), ExtraTypes); |
|
32 | } |
35 | } |
|
33 | |
36 | |
|
34 | public static XmlSerializer GetSerializer(Type MainTypeForSerialization) |
37 | public static XmlSerializer GetSerializer(Type MainTypeForSerialization) |
|
35 | { |
38 | { |
|
36 | return GetSerializer(MainTypeForSerialization, null); |
39 | return GetSerializer(MainTypeForSerialization, null); |
|
37 | } |
40 | } |
|
38 | |
41 | |
|
39 | public static XmlSerializer GetSerializer(Type MainTypeForSerialization, Type[] ExtraTypes) |
42 | public static XmlSerializer GetSerializer(Type MainTypeForSerialization, Type[] ExtraTypes) |
|
40 | { |
43 | { |
|
41 | var Signature = MainTypeForSerialization.FullName; |
44 | var Signature = MainTypeForSerialization.FullName; |
|
42 | if (ExtraTypes != null) |
45 | if (ExtraTypes != null) |
|
43 | { |
- | ||
44 | Signature = ExtraTypes.Aggregate(Signature, (current, tp) => current + ("-" + tp.FullName)); |
46 | Signature = ExtraTypes.Aggregate(Signature, (current, tp) => current + "-" + tp.FullName); |
|
45 | } |
- | ||
46 | |
47 | |
|
47 | SerializerCacheLock.EnterReadLock(); |
48 | SerializerCacheLock.EnterReadLock(); |
|
48 | XmlSerializer XmlEventSerializer; |
49 | XmlSerializer XmlEventSerializer; |
|
49 | if (SerializerCache.TryGetValue(Signature, out XmlEventSerializer)) |
50 | if (SerializerCache.TryGetValue(Signature, out XmlEventSerializer)) |
|
50 | { |
51 | { |
|
51 | SerializerCacheLock.ExitReadLock(); |
52 | SerializerCacheLock.ExitReadLock(); |
|
52 | return XmlEventSerializer; |
53 | return XmlEventSerializer; |
|
53 | } |
54 | } |
|
54 | SerializerCacheLock.ExitReadLock(); |
55 | SerializerCacheLock.ExitReadLock(); |
|
55 | |
56 | |
|
56 | if (ExtraTypes == null) |
57 | if (ExtraTypes == null) |
|
57 | return new XmlSerializer(MainTypeForSerialization); |
58 | return new XmlSerializer(MainTypeForSerialization); |
|
58 | |
59 | |
|
59 | XmlEventSerializer = new XmlSerializer(MainTypeForSerialization, ExtraTypes); |
60 | XmlEventSerializer = new XmlSerializer(MainTypeForSerialization, ExtraTypes); |
|
60 | SerializerCacheLock.EnterWriteLock(); |
61 | SerializerCacheLock.EnterWriteLock(); |
|
61 | SerializerCache.Add(Signature, XmlEventSerializer); |
62 | SerializerCache.Add(Signature, XmlEventSerializer); |
|
62 | SerializerCacheLock.ExitWriteLock(); |
63 | SerializerCacheLock.ExitWriteLock(); |
|
63 | |
64 | |
|
64 | return XmlEventSerializer; |
65 | return XmlEventSerializer; |
|
65 | } |
66 | } |
|
66 | |
67 | |
|
67 | public static T Deserialize<T>(XDocument XmlData) |
68 | public static T Deserialize<T>(XDocument XmlData) |
|
68 | { |
69 | { |
|
69 | return Deserialize<T>(XmlData, null); |
70 | return Deserialize<T>(XmlData, null); |
|
70 | } |
71 | } |
|
71 | |
72 | |
|
72 | public static T Deserialize<T>(XDocument XmlData, Type[] ExtraTypes) |
73 | public static T Deserialize<T>(XDocument XmlData, Type[] ExtraTypes) |
|
73 | { |
74 | { |
|
74 | try |
75 | try |
|
75 | { |
76 | { |
|
76 | using (var XmlReader = XmlData.Root.CreateReader()) |
77 | using (var XmlReader = XmlData.Root.CreateReader()) |
|
77 | { |
78 | { |
|
78 | return (T) GetSerializer<T>(ExtraTypes).Deserialize(XmlReader); |
79 | return (T) GetSerializer<T>(ExtraTypes).Deserialize(XmlReader); |
|
79 | } |
80 | } |
|
80 | } |
81 | } |
|
81 | catch (Exception ex) |
82 | catch (Exception ex) |
|
82 | { |
83 | { |
|
83 | throw new Exception("Could not deserialize to " + typeof(T).Name, ex); |
84 | throw new Exception("Could not deserialize to " + typeof(T).Name, ex); |
|
84 | } |
85 | } |
|
85 | } |
86 | } |
|
86 | |
87 | |
|
87 | public static T Deserialize<T>(string XmlData) |
88 | public static T Deserialize<T>(string XmlData) |
|
88 | { |
89 | { |
|
89 | return Deserialize<T>(XmlData, null); |
90 | return Deserialize<T>(XmlData, null); |
|
90 | } |
91 | } |
|
91 | |
92 | |
|
92 | public static T Deserialize<T>(string XmlData, Type[] ExtraTypes) |
93 | public static T Deserialize<T>(string XmlData, Type[] ExtraTypes) |
|
93 | { |
94 | { |
|
94 | try |
95 | try |
|
95 | { |
96 | { |
|
96 | using (MemoryStream memoryStream = new MemoryStream()) |
97 | using (var memoryStream = new MemoryStream()) |
|
97 | { |
98 | { |
|
98 | using (StreamWriter streamWriter = new StreamWriter(memoryStream)) |
99 | using (var streamWriter = new StreamWriter(memoryStream)) |
|
99 | { |
100 | { |
|
100 | streamWriter.Write(XmlData); |
101 | streamWriter.Write(XmlData); |
|
101 | streamWriter.Flush(); |
102 | streamWriter.Flush(); |
|
102 | memoryStream.Position = 0; |
103 | memoryStream.Position = 0; |
|
103 | return (T)GetSerializer<T>(ExtraTypes).Deserialize(memoryStream); |
104 | return (T) GetSerializer<T>(ExtraTypes).Deserialize(memoryStream); |
|
104 | } |
105 | } |
|
105 | } |
106 | } |
|
106 | } |
107 | } |
|
107 | catch (Exception ex) |
108 | catch (Exception ex) |
|
108 | { |
109 | { |
|
109 | throw new Exception("Could not deserialize to " + typeof(T).Name, ex); |
110 | throw new Exception("Could not deserialize to " + typeof(T).Name, ex); |
|
110 | } |
111 | } |
|
111 | } |
112 | } |
|
112 | |
113 | |
|
113 | public static XDocument Serialize<T>(T Object, Type[] ExtraTypes) |
114 | public static XDocument Serialize<T>(T Object, Type[] ExtraTypes) |
|
114 | { |
115 | { |
|
115 | try |
116 | try |
|
116 | { |
117 | { |
|
117 | using (MemoryStream memoryStream = new MemoryStream()) |
118 | using (var memoryStream = new MemoryStream()) |
|
118 | { |
119 | { |
|
119 | using (StreamReader streamReader = new StreamReader(memoryStream)) |
120 | using (var streamReader = new StreamReader(memoryStream)) |
|
120 | { |
121 | { |
|
121 | using (var xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings { Indent = true })) |
122 | using (var xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings {Indent = true})) |
|
122 | { |
123 | { |
|
123 | var ns = new XmlSerializerNamespaces(); |
124 | var ns = new XmlSerializerNamespaces(); |
|
124 | ns.Add(string.Empty, string.Empty); |
125 | ns.Add(string.Empty, string.Empty); |
|
125 | GetSerializer<T>(ExtraTypes).Serialize(xmlWriter, Object, ns); |
126 | GetSerializer<T>(ExtraTypes).Serialize(xmlWriter, Object, ns); |
|
126 | xmlWriter.Flush(); |
127 | xmlWriter.Flush(); |
|
127 | memoryStream.Position = 0L; |
128 | memoryStream.Position = 0L; |
|
128 | return XDocument.Load(streamReader, LoadOptions.None); |
129 | return XDocument.Load(streamReader, LoadOptions.None); |
|
129 | } |
130 | } |
|
130 | } |
131 | } |
|
131 | } |
132 | } |
|
132 | } |
133 | } |
|
133 | catch (Exception ex) |
134 | catch (Exception ex) |
|
134 | { |
135 | { |
|
135 | throw new Exception("Could not serialize from " + typeof(T).Name + " to xml string", ex); |
136 | throw new Exception("Could not serialize from " + typeof(T).Name + " to xml string", ex); |
|
136 | } |
137 | } |
|
137 | } |
138 | } |
|
138 | |
139 | |
|
139 | public static XDocument Serialize<T>(T Object) |
140 | public static XDocument Serialize<T>(T Object) |
|
140 | { |
141 | { |
|
141 | return Serialize(Object, null); |
142 | return Serialize(Object, null); |
|
142 | } |
143 | } |
|
143 | |
144 | |
|
144 | public static T Deserialize<T>(StreamReader streamReader) |
145 | public static T Deserialize<T>(StreamReader streamReader) |
|
145 | { |
146 | { |
|
146 | return Deserialize<T>(streamReader.ReadToEnd(), null); |
147 | return Deserialize<T>(streamReader.ReadToEnd(), null); |
|
147 | } |
148 | } |
|
148 | |
149 | |
|
149 | public static T Deserialize<T>(Stream stream) |
150 | public static T Deserialize<T>(Stream stream) |
|
150 | { |
151 | { |
|
151 | using (var memoryStream = new MemoryStream()) |
152 | using (var memoryStream = new MemoryStream()) |
|
152 | { |
153 | { |
|
153 | stream.CopyTo(memoryStream); |
154 | stream.CopyTo(memoryStream); |
|
154 | memoryStream.Position = 0L; |
155 | memoryStream.Position = 0L; |
|
155 | using (var streamReader = new StreamReader(memoryStream)) |
156 | using (var streamReader = new StreamReader(memoryStream)) |
|
156 | { |
157 | { |
|
157 | return Deserialize<T>(streamReader.ReadToEnd(), null); |
158 | return Deserialize<T>(streamReader.ReadToEnd(), null); |
|
158 | } |
159 | } |
|
159 | } |
160 | } |
|
160 | } |
161 | } |
|
161 | |
162 | |
|
162 | public static void Serialize<T>(Stream stream, T value) |
163 | public static void Serialize<T>(Stream stream, T value) |
|
163 | { |
164 | { |
|
164 | Serialize(value, null).Save(stream); |
165 | Serialize(value, null).Save(stream); |
|
165 | } |
166 | } |
|
166 | |
167 | |
|
167 | public static void Serialize<T>(StreamWriter streamWriter, T value) |
168 | public static void Serialize<T>(StreamWriter streamWriter, T value) |
|
168 | { |
169 | { |
|
169 | Serialize(value, null).Save(streamWriter); |
170 | Serialize(value, null).Save(streamWriter); |
|
170 | } |
171 | } |
|
171 | |
172 | |
|
172 | public static void Serialize<T>(StringWriter stringWriter, T value) |
173 | public static void Serialize<T>(StringWriter stringWriter, T value) |
|
173 | { |
174 | { |
|
174 | Serialize(value, null).Save(stringWriter); |
175 | Serialize(value, null).Save(stringWriter); |
|
175 | } |
176 | } |
|
176 | |
177 | |
|
177 | public static T Deserialize<T>(TextReader reader) |
178 | public static T Deserialize<T>(TextReader reader) |
|
178 | { |
179 | { |
|
179 | return Deserialize<T>(reader.ReadToEnd(), null); |
180 | return Deserialize<T>(reader.ReadToEnd(), null); |
|
180 | } |
181 | } |
|
181 | } |
182 | } |
|
182 | } |
183 | } |
|
183 | |
184 | |
|
- | 185 |
|
||
- | 186 | |
||
- | 187 | |
||
- | 188 | |