annotate Implab/Xml/SerializationHelpers.cs @ 281:e0916ddc9950 v3 tip

code cleanup and refactoring
author cin
date Fri, 01 Jun 2018 21:35:24 +0300
parents 6691aff01de1
children
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;
264
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
5 using System.Reflection;
229
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.Linq;
264
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
10 using System.Xml.Serialization;
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
11
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
12 namespace Implab.Xml {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
13 public static class SerializationHelpers {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
14 public static string SerializeAsString<T>(T obj) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
15 return XmlDefaultSerializer<T>.Instance.SerializeAsString(obj);
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
16 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
17
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
18 public static void Serialize<T>(XmlWriter writer, T obj) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
19 XmlDefaultSerializer<T>.Instance.Serialize(writer, obj);
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
20 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
21
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
22 public static XmlDocument SerializeAsXmlDocument<T>(T obj) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
23 var doc = new XmlDocument();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
24 using (var writer = doc.CreateNavigator().AppendChild()) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
25 XmlDefaultSerializer<T>.Instance.Serialize(writer, obj);
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
26 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
27 return doc;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
28 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
29
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
30 public static XDocument SerializeAsXDocument<T>(T obj) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
31 var doc = new XDocument();
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
32 using (var writer = doc.CreateWriter()) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
33 XmlDefaultSerializer<T>.Instance.Serialize(writer, obj);
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
34 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
35 return doc;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
36 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
37
231
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
38 public static void SerializeToFile<T>(string file, T obj) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
39 XmlDefaultSerializer<T>.Instance.SerializeToFile(obj, file);
231
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
40 }
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
41
251
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
42 public static void SerializeToElementChild<T>(XmlElement element, T obj) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
43 XmlDefaultSerializer<T>.Instance.Serialize(obj, element);
251
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) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
47 return (T)XmlDefaultSerializer<T>.Instance.Deserialize(reader);
251
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) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
51 return (T)XmlDefaultSerializer<T>.Instance.DeserializeFromFile(file);
251
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
52 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
53
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
54 public static T DeserializeFromString<T>(string data) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
55 return (T)XmlDefaultSerializer<T>.Instance.DeserializeFromString(data);
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
56 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
57
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
58 public static T DeserializeFromXmlNode<T>(XmlNode node) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
59 Safe.ArgumentNotNull(node, nameof(node));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
60 using (var reader = node.CreateNavigator().ReadSubtree())
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
61 return (T)XmlDefaultSerializer<T>.Instance.Deserialize(reader);
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
62 }
264
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
63
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
64 public static T DeserializeJson<T>(TextReader textReader) {
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
65 var options = new JsonXmlReaderOptions {
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
66 NamespaceUri = typeof(T).GetCustomAttribute<XmlRootAttribute>()?.Namespace,
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
67 RootName = typeof(T).Name,
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
68 FlattenArrays = true
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
69 };
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
70
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
71 using (var reader = JsonXmlReader.CreateJsonXmlReader(textReader, options))
264
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
72 return Deserialize<T>(reader);
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
73 }
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
74
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
75 public static T DeserializeJsonFromString<T>(string data) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
76 using (var reader = new StringReader(data)) {
264
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
77 return DeserializeJson<T>(reader);
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
78 }
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
79 }
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
80
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
81 public static void SerializeJson<T>(TextWriter writer, T obj) {
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
82 var doc = SerializeAsXmlDocument(obj);
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
83 XmlToJson.Default.Transform(doc, null, writer);
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
84 }
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
85
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
86 public static string SerializeJsonAsString<T>(T obj) {
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents: 264
diff changeset
87 using (var writer = new StringWriter()) {
264
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
88 SerializeJson(writer, obj);
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
89 return writer.ToString();
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
90 }
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
91 }
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
92 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
93 }