wasSharpNET – Blame information for rev 16

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