comparison Implab/Xml/SerializersPool.cs @ 229:5f7a3e1d32b9 v2

JsonXmlReader performance tuning JsonScanner now operates strings and doesn't parses number and literals. Added SerializationHelpers to common serialize/deserialize operations
author cin
date Tue, 12 Sep 2017 19:07:42 +0300
parents
children 6691aff01de1
comparison
equal deleted inserted replaced
228:6fa235c5a760 229:5f7a3e1d32b9
1 using Implab.Components;
2 using System;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Linq;
6 using System.Text;
7 using System.Threading.Tasks;
8 using System.Xml;
9 using System.Xml.Serialization;
10
11 namespace Implab.Xml {
12 public class SerializersPool<T> : ObjectPool<XmlSerializer> {
13
14 static readonly SerializersPool<T> _instance = new SerializersPool<T>();
15
16 public static SerializersPool<T> Instance {
17 get { return _instance; }
18 }
19
20 #region implemented abstract members of ObjectPool
21 protected override XmlSerializer CreateInstance() {
22 return new XmlSerializer(typeof(T));
23 }
24 #endregion
25
26 public T DeserializeFromString(string data) {
27 using (var reader = new StringReader(data)) {
28 return Deserialize(reader);
29 }
30 }
31
32 public T Deserialize(TextReader reader) {
33 var sr = Allocate();
34 try {
35 return (T)sr.Deserialize(reader);
36 } finally {
37 Release(sr);
38 }
39 }
40
41 public T Deserialize(XmlReader reader) {
42 var sr = Allocate();
43 try {
44 return (T)sr.Deserialize(reader);
45 } finally {
46 Release(sr);
47 }
48 }
49
50 public string SerializeAsString(T data) {
51 using (var writer = new StringWriter()) {
52 Serialize(writer, data);
53 return writer.ToString();
54 }
55 }
56
57 public void Serialize(TextWriter writer, T data) {
58 var sr = Allocate();
59 try {
60 sr.Serialize(writer, data);
61 } finally {
62 Release(sr);
63 }
64 }
65
66 public void Serialize(XmlWriter writer, T data) {
67 var sr = Allocate();
68 try {
69 sr.Serialize(writer, data);
70 } finally {
71 Release(sr);
72 }
73 }
74
75 }
76 }