annotate Implab/Xml/SerializationHelpers.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 3eaa9372c754
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
1 using System;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
2 using System.Collections.Generic;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
3 using System.Linq;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
4 using System.Text;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
5 using System.Threading.Tasks;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
6 using System.Xml;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
7 using System.Xml.Linq;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
8
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
9 namespace Implab.Xml {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
10 public static class SerializationHelpers {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
11 public static string SerializeAsString<T>(T obj) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
12 return SerializersPool<T>.Instance.SerializeAsString(obj);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
13 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
14
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
15 public static void Serialize<T>(XmlWriter writer, T obj) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
16 SerializersPool<T>.Instance.Serialize(writer, obj);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
17 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
18
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
19 public static XmlDocument SerializeAsXmlDocument<T>(T obj) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
20 var doc = new XmlDocument();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
21 using (var writer = doc.CreateNavigator().AppendChild()) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
22 SerializersPool<T>.Instance.Serialize(writer, obj);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
23 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
24 return doc;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
25 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
26
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
27 public static XDocument SerializeAsXDocument<T>(T obj) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
28 var doc = new XDocument();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
29 using (var writer = doc.CreateWriter()) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
30 SerializersPool<T>.Instance.Serialize(writer, obj);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
31 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
32 return doc;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
33 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
34
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
35 public static T DeserializeFromString<T>(string data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
36 return SerializersPool<T>.Instance.DeserializeFromString(data);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
37 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
38
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
39 public static T DeserializeFromXmlNode<T>(XmlNode node) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
40 Safe.ArgumentNotNull(node, nameof(node));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
41 using (var reader = node.CreateNavigator().ReadSubtree())
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
42 return SerializersPool<T>.Instance.Deserialize(reader);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
43 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
44 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
45 }