229
|
1 using System;
|
|
2 using System.Collections.Generic;
|
231
|
3 using System.IO;
|
229
|
4 using System.Linq;
|
264
|
5 using System.Reflection;
|
229
|
6 using System.Text;
|
|
7 using System.Threading.Tasks;
|
|
8 using System.Xml;
|
|
9 using System.Xml.Linq;
|
264
|
10 using System.Xml.Serialization;
|
229
|
11
|
|
12 namespace Implab.Xml {
|
|
13 public static class SerializationHelpers {
|
|
14 public static string SerializeAsString<T>(T obj) {
|
|
15 return SerializersPool<T>.Instance.SerializeAsString(obj);
|
|
16 }
|
|
17
|
|
18 public static void Serialize<T>(XmlWriter writer, T obj) {
|
|
19 SerializersPool<T>.Instance.Serialize(writer, obj);
|
|
20 }
|
|
21
|
|
22 public static XmlDocument SerializeAsXmlDocument<T>(T obj) {
|
|
23 var doc = new XmlDocument();
|
|
24 using (var writer = doc.CreateNavigator().AppendChild()) {
|
|
25 SerializersPool<T>.Instance.Serialize(writer, obj);
|
|
26 }
|
|
27 return doc;
|
|
28 }
|
|
29
|
|
30 public static XDocument SerializeAsXDocument<T>(T obj) {
|
|
31 var doc = new XDocument();
|
|
32 using (var writer = doc.CreateWriter()) {
|
|
33 SerializersPool<T>.Instance.Serialize(writer, obj);
|
|
34 }
|
|
35 return doc;
|
|
36 }
|
|
37
|
231
|
38 public static void SerializeToFile<T>(string file, T obj) {
|
|
39 using (var writer = File.CreateText(file))
|
|
40 SerializersPool<T>.Instance.Serialize(writer, obj);
|
|
41 }
|
|
42
|
251
|
43 public static void SerializeToElementChild<T>(XmlElement element, T obj) {
|
|
44 using(var writer = element.CreateNavigator().AppendChild())
|
|
45 SerializersPool<T>.Instance.Serialize(writer, obj);
|
|
46 }
|
|
47
|
|
48 public static T Deserialize<T>(XmlReader reader) {
|
|
49 return SerializersPool<T>.Instance.Deserialize(reader);
|
|
50 }
|
|
51
|
|
52 public static T DeserializeFromFile<T>(string file) {
|
|
53 using(var reader = XmlReader.Create(File.OpenText(file)))
|
|
54 return Deserialize<T>(reader);
|
|
55 }
|
|
56
|
229
|
57 public static T DeserializeFromString<T>(string data) {
|
|
58 return SerializersPool<T>.Instance.DeserializeFromString(data);
|
|
59 }
|
|
60
|
|
61 public static T DeserializeFromXmlNode<T>(XmlNode node) {
|
|
62 Safe.ArgumentNotNull(node, nameof(node));
|
|
63 using (var reader = node.CreateNavigator().ReadSubtree())
|
|
64 return SerializersPool<T>.Instance.Deserialize(reader);
|
|
65 }
|
264
|
66
|
|
67 public static T DeserializeJson<T>(TextReader textReader) {
|
|
68 var options = new JsonXmlReaderOptions {
|
|
69 NamespaceUri = typeof(T).GetCustomAttribute<XmlRootAttribute>()?.Namespace,
|
|
70 RootName = typeof(T).Name,
|
|
71 FlattenArrays = true
|
|
72 };
|
|
73
|
|
74 using(var reader = JsonXmlReader.CreateJsonXmlReader(textReader, options))
|
|
75 return Deserialize<T>(reader);
|
|
76 }
|
|
77
|
|
78 public static T DeserializeJsonFromString<T>(string data) {
|
|
79 using(var reader = new StringReader(data)) {
|
|
80 return DeserializeJson<T>(reader);
|
|
81 }
|
|
82 }
|
|
83
|
|
84 public static void SerializeJson<T>(TextWriter writer, T obj) {
|
|
85 var doc = SerializeAsXmlDocument(obj);
|
|
86 XmlToJson.Default.Transform(doc, null, writer);
|
|
87 }
|
|
88
|
|
89 public static string SerializeJsonAsString<T>(T obj) {
|
|
90 using(var writer = new StringWriter()) {
|
|
91 SerializeJson(writer, obj);
|
|
92 return writer.ToString();
|
|
93 }
|
|
94 }
|
229
|
95 }
|
|
96 }
|