annotate Implab/Xml/SerializationHelpers.cs @ 260:547a2fc0d93e v3 v3.0.6

minor fixes
author cin
date Fri, 13 Apr 2018 19:14:59 +0300
parents 7c7e9ad6fe4a
children 3a6e18c432be
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
251
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
41 public static void SerializeToElementChild<T>(XmlElement element, T obj) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
42 using(var writer = element.CreateNavigator().AppendChild())
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
43 SerializersPool<T>.Instance.Serialize(writer, obj);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
44 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
45
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
46 public static T Deserialize<T>(XmlReader reader) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
47 return SerializersPool<T>.Instance.Deserialize(reader);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
48 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
49
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
50 public static T DeserializeFromFile<T>(string file) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
51 using(var reader = XmlReader.Create(File.OpenText(file)))
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
52 return Deserialize<T>(reader);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
53 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
54
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
55 public static T DeserializeFromString<T>(string data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
56 return SerializersPool<T>.Instance.DeserializeFromString(data);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
57 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
58
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
59 public static T DeserializeFromXmlNode<T>(XmlNode node) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
60 Safe.ArgumentNotNull(node, nameof(node));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
61 using (var reader = node.CreateNavigator().ReadSubtree())
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
62 return SerializersPool<T>.Instance.Deserialize(reader);
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 }