wasSharpNET – Blame information for rev 28

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