annotate Implab/Xml/SerializersPool.cs @ 281:e0916ddc9950 v3 tip

code cleanup and refactoring
author cin
date Fri, 01 Jun 2018 21:35:24 +0300
parents 6691aff01de1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
1 using Implab.Components;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
2 using System;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
3 using System.Collections.Generic;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
4 using System.IO;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
5 using System.Linq;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
6 using System.Text;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
7 using System.Threading.Tasks;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
8 using System.Xml;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
9 using System.Xml.Serialization;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
10
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
11 namespace Implab.Xml {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 229
diff changeset
12 [Obsolete("this class will be removed, use XmlDefaultSerializer")]
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
13 public class SerializersPool<T> : ObjectPool<XmlSerializer> {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
14
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
15 static readonly SerializersPool<T> _instance = new SerializersPool<T>();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
16
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
17 public static SerializersPool<T> Instance {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
18 get { return _instance; }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
19 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
20
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
21 #region implemented abstract members of ObjectPool
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
22 protected override XmlSerializer CreateInstance() {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
23 return new XmlSerializer(typeof(T));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
24 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
25 #endregion
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
26
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
27 public T DeserializeFromString(string data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
28 using (var reader = new StringReader(data)) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
29 return Deserialize(reader);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
30 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
31 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
32
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
33 public T Deserialize(TextReader reader) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
34 var sr = Allocate();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
35 try {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
36 return (T)sr.Deserialize(reader);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
37 } finally {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
38 Release(sr);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
39 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
40 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
41
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
42 public T Deserialize(XmlReader reader) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
43 var sr = Allocate();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
44 try {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
45 return (T)sr.Deserialize(reader);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
46 } finally {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
47 Release(sr);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
48 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
49 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
50
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
51 public string SerializeAsString(T data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
52 using (var writer = new StringWriter()) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
53 Serialize(writer, data);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
54 return writer.ToString();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
55 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
56 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
57
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
58 public void Serialize(TextWriter writer, T data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
59 var sr = Allocate();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
60 try {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
61 sr.Serialize(writer, data);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
62 } finally {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
63 Release(sr);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
64 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
65 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
66
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
67 public void Serialize(XmlWriter writer, T data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
68 var sr = Allocate();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
69 try {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
70 sr.Serialize(writer, data);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
71 } finally {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
72 Release(sr);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
73 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
74 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
75
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
76 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
77 }