annotate Implab/Xml/SerializationHelpers.cs @ 267:6b3e5c48131b v3

Working on Unity xml configuration
author cin
date Fri, 20 Apr 2018 19:05:12 +0300
parents 3a6e18c432be
children 6691aff01de1
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) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
15 return SerializersPool<T>.Instance.SerializeAsString(obj);
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) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
19 SerializersPool<T>.Instance.Serialize(writer, obj);
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()) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
25 SerializersPool<T>.Instance.Serialize(writer, obj);
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()) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
33 SerializersPool<T>.Instance.Serialize(writer, obj);
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) {
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
39 using (var writer = File.CreateText(file))
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
40 SerializersPool<T>.Instance.Serialize(writer, obj);
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
41 }
3eaa9372c754 Added SerializationHelpers.SerializeToFile method
cin
parents: 229
diff changeset
42
251
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
43 public static void SerializeToElementChild<T>(XmlElement element, T obj) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
44 using(var writer = element.CreateNavigator().AppendChild())
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
45 SerializersPool<T>.Instance.Serialize(writer, obj);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
46 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
47
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
48 public static T Deserialize<T>(XmlReader reader) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
49 return SerializersPool<T>.Instance.Deserialize(reader);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
50 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
51
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
52 public static T DeserializeFromFile<T>(string file) {
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
53 using(var reader = XmlReader.Create(File.OpenText(file)))
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
54 return Deserialize<T>(reader);
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
55 }
7c7e9ad6fe4a Prerelease version of RunnableComponent
cin
parents: 231
diff changeset
56
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
57 public static T DeserializeFromString<T>(string data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
58 return SerializersPool<T>.Instance.DeserializeFromString(data);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
59 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
60
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
61 public static T DeserializeFromXmlNode<T>(XmlNode node) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
62 Safe.ArgumentNotNull(node, nameof(node));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
63 using (var reader = node.CreateNavigator().ReadSubtree())
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
64 return SerializersPool<T>.Instance.Deserialize(reader);
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
65 }
264
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
66
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
67 public static T DeserializeJson<T>(TextReader textReader) {
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
68 var options = new JsonXmlReaderOptions {
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
69 NamespaceUri = typeof(T).GetCustomAttribute<XmlRootAttribute>()?.Namespace,
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
70 RootName = typeof(T).Name,
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
71 FlattenArrays = true
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
72 };
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
73
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
74 using(var reader = JsonXmlReader.CreateJsonXmlReader(textReader, options))
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
75 return Deserialize<T>(reader);
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
76 }
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
77
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
78 public static T DeserializeJsonFromString<T>(string data) {
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
79 using(var reader = new StringReader(data)) {
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
80 return DeserializeJson<T>(reader);
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
81 }
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
82 }
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
83
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
84 public static void SerializeJson<T>(TextWriter writer, T obj) {
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
85 var doc = SerializeAsXmlDocument(obj);
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
86 XmlToJson.Default.Transform(doc, null, writer);
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
87 }
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
88
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
89 public static string SerializeJsonAsString<T>(T obj) {
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
90 using(var writer = new StringWriter()) {
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
91 SerializeJson(writer, obj);
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
92 return writer.ToString();
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
93 }
3a6e18c432be Added XmlToJson xsl transformation.
cin
parents: 251
diff changeset
94 }
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
95 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents:
diff changeset
96 }