annotate Implab/Xml/SerializationHelpers.cs @ 244:eee3e49dd1ff v3

working on promises
author cin
date Thu, 25 Jan 2018 19:09:16 +0300
parents 3eaa9372c754
children 7c7e9ad6fe4a
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;
231
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
3 using System.IO;
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
4 using System.Linq;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
5 using System.Text;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
6 using System.Threading.Tasks;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
7 using System.Xml;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
8 using System.Xml.Linq;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
9
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
10 namespace Implab.Xml {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
11 public static class SerializationHelpers {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
12 public static string SerializeAsString<T>(T obj) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
13 return SerializersPool<T>.Instance.SerializeAsString(obj);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
14 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
15
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
16 public static void Serialize<T>(XmlWriter writer, T obj) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
17 SerializersPool<T>.Instance.Serialize(writer, obj);
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 public static XmlDocument SerializeAsXmlDocument<T>(T obj) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
21 var doc = new XmlDocument();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
22 using (var writer = doc.CreateNavigator().AppendChild()) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
23 SerializersPool<T>.Instance.Serialize(writer, obj);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
24 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
25 return doc;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
26 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
27
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
28 public static XDocument SerializeAsXDocument<T>(T obj) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
29 var doc = new XDocument();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
30 using (var writer = doc.CreateWriter()) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
31 SerializersPool<T>.Instance.Serialize(writer, obj);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
32 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
33 return doc;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
34 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
35
231
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
36 public static void SerializeToFile<T>(string file, T obj) {
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
37 using (var writer = File.CreateText(file))
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
38 SerializersPool<T>.Instance.Serialize(writer, obj);
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
39 }
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
40
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
41 public static T DeserializeFromString<T>(string data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
42 return SerializersPool<T>.Instance.DeserializeFromString(data);
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 public static T DeserializeFromXmlNode<T>(XmlNode node) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
46 Safe.ArgumentNotNull(node, nameof(node));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
47 using (var reader = node.CreateNavigator().ReadSubtree())
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
48 return SerializersPool<T>.Instance.Deserialize(reader);
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 }