wasSharpNET – Blame information for rev 27

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