Mercurial > pub > ImplabNet
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 |
rev | line source |
---|---|
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) { | |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
264
diff
changeset
|
15 return XmlDefaultSerializer<T>.Instance.SerializeAsString(obj); |
229 | 16 } |
17 | |
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 | 20 } |
21 | |
22 public static XmlDocument SerializeAsXmlDocument<T>(T obj) { | |
23 var doc = new XmlDocument(); | |
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 | 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()) { | |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
264
diff
changeset
|
33 XmlDefaultSerializer<T>.Instance.Serialize(writer, obj); |
229 | 34 } |
35 return doc; | |
36 } | |
37 | |
231 | 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 | 40 } |
41 | |
251 | 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 | 44 } |
45 | |
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 | 48 } |
49 | |
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 | 52 } |
53 | |
229 | 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 | 56 } |
57 | |
58 public static T DeserializeFromXmlNode<T>(XmlNode node) { | |
59 Safe.ArgumentNotNull(node, nameof(node)); | |
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 | 62 } |
264 | 63 |
64 public static T DeserializeJson<T>(TextReader textReader) { | |
65 var options = new JsonXmlReaderOptions { | |
66 NamespaceUri = typeof(T).GetCustomAttribute<XmlRootAttribute>()?.Namespace, | |
67 RootName = typeof(T).Name, | |
68 FlattenArrays = true | |
69 }; | |
70 | |
278
6691aff01de1
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
264
diff
changeset
|
71 using (var reader = JsonXmlReader.CreateJsonXmlReader(textReader, options)) |
264 | 72 return Deserialize<T>(reader); |
73 } | |
74 | |
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 | 77 return DeserializeJson<T>(reader); |
78 } | |
79 } | |
80 | |
81 public static void SerializeJson<T>(TextWriter writer, T obj) { | |
82 var doc = SerializeAsXmlDocument(obj); | |
83 XmlToJson.Default.Transform(doc, null, writer); | |
84 } | |
85 | |
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 | 88 SerializeJson(writer, obj); |
89 return writer.ToString(); | |
90 } | |
91 } | |
229 | 92 } |
93 } |