annotate Implab/Xml/SerializersPool.cs @ 237:f2150c16b476 v2

missing files
author cin
date Wed, 22 Nov 2017 16:54:58 +0300
parents 5f7a3e1d32b9
children 6691aff01de1
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 {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
12 public class SerializersPool<T> : ObjectPool<XmlSerializer> {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
13
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
14 static readonly SerializersPool<T> _instance = new SerializersPool<T>();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
15
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
16 public static SerializersPool<T> Instance {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
17 get { return _instance; }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
18 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
19
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
20 #region implemented abstract members of ObjectPool
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
21 protected override XmlSerializer CreateInstance() {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
22 return new XmlSerializer(typeof(T));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
23 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
24 #endregion
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
25
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
26 public T DeserializeFromString(string data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
27 using (var reader = new StringReader(data)) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
28 return Deserialize(reader);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
29 }
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 public T Deserialize(TextReader reader) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
33 var sr = Allocate();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
34 try {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
35 return (T)sr.Deserialize(reader);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
36 } finally {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
37 Release(sr);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
38 }
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 public T Deserialize(XmlReader reader) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
42 var sr = Allocate();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
43 try {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
44 return (T)sr.Deserialize(reader);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
45 } finally {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
46 Release(sr);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
47 }
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 public string SerializeAsString(T data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
51 using (var writer = new StringWriter()) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
52 Serialize(writer, data);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
53 return writer.ToString();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
54 }
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 public void Serialize(TextWriter writer, T data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
58 var sr = Allocate();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
59 try {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
60 sr.Serialize(writer, data);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
61 } finally {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
62 Release(sr);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
63 }
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 public void Serialize(XmlWriter writer, T data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
67 var sr = Allocate();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
68 try {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
69 sr.Serialize(writer, data);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
70 } finally {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
71 Release(sr);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
72 }
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 }