Mercurial > pub > ImplabNet
comparison Implab/Xml/SerializationHelpers.cs @ 278:6691aff01de1 v3
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
Implab.ServiceHost: rewritten TypeReference (added support for nested types), stable API
author | cin |
---|---|
date | Thu, 03 May 2018 09:59:44 +0300 |
parents | 3a6e18c432be |
children |
comparison
equal
deleted
inserted
replaced
277:963b17c275be | 278:6691aff01de1 |
---|---|
10 using System.Xml.Serialization; | 10 using System.Xml.Serialization; |
11 | 11 |
12 namespace Implab.Xml { | 12 namespace Implab.Xml { |
13 public static class SerializationHelpers { | 13 public static class SerializationHelpers { |
14 public static string SerializeAsString<T>(T obj) { | 14 public static string SerializeAsString<T>(T obj) { |
15 return SerializersPool<T>.Instance.SerializeAsString(obj); | 15 return XmlDefaultSerializer<T>.Instance.SerializeAsString(obj); |
16 } | 16 } |
17 | 17 |
18 public static void Serialize<T>(XmlWriter writer, T obj) { | 18 public static void Serialize<T>(XmlWriter writer, T obj) { |
19 SerializersPool<T>.Instance.Serialize(writer, obj); | 19 XmlDefaultSerializer<T>.Instance.Serialize(writer, obj); |
20 } | 20 } |
21 | 21 |
22 public static XmlDocument SerializeAsXmlDocument<T>(T obj) { | 22 public static XmlDocument SerializeAsXmlDocument<T>(T obj) { |
23 var doc = new XmlDocument(); | 23 var doc = new XmlDocument(); |
24 using (var writer = doc.CreateNavigator().AppendChild()) { | 24 using (var writer = doc.CreateNavigator().AppendChild()) { |
25 SerializersPool<T>.Instance.Serialize(writer, obj); | 25 XmlDefaultSerializer<T>.Instance.Serialize(writer, obj); |
26 } | 26 } |
27 return doc; | 27 return doc; |
28 } | 28 } |
29 | 29 |
30 public static XDocument SerializeAsXDocument<T>(T obj) { | 30 public static XDocument SerializeAsXDocument<T>(T obj) { |
31 var doc = new XDocument(); | 31 var doc = new XDocument(); |
32 using (var writer = doc.CreateWriter()) { | 32 using (var writer = doc.CreateWriter()) { |
33 SerializersPool<T>.Instance.Serialize(writer, obj); | 33 XmlDefaultSerializer<T>.Instance.Serialize(writer, obj); |
34 } | 34 } |
35 return doc; | 35 return doc; |
36 } | 36 } |
37 | 37 |
38 public static void SerializeToFile<T>(string file, T obj) { | 38 public static void SerializeToFile<T>(string file, T obj) { |
39 using (var writer = File.CreateText(file)) | 39 XmlDefaultSerializer<T>.Instance.SerializeToFile(obj, file); |
40 SerializersPool<T>.Instance.Serialize(writer, obj); | |
41 } | 40 } |
42 | 41 |
43 public static void SerializeToElementChild<T>(XmlElement element, T obj) { | 42 public static void SerializeToElementChild<T>(XmlElement element, T obj) { |
44 using(var writer = element.CreateNavigator().AppendChild()) | 43 XmlDefaultSerializer<T>.Instance.Serialize(obj, element); |
45 SerializersPool<T>.Instance.Serialize(writer, obj); | |
46 } | 44 } |
47 | 45 |
48 public static T Deserialize<T>(XmlReader reader) { | 46 public static T Deserialize<T>(XmlReader reader) { |
49 return SerializersPool<T>.Instance.Deserialize(reader); | 47 return (T)XmlDefaultSerializer<T>.Instance.Deserialize(reader); |
50 } | 48 } |
51 | 49 |
52 public static T DeserializeFromFile<T>(string file) { | 50 public static T DeserializeFromFile<T>(string file) { |
53 using(var reader = XmlReader.Create(File.OpenText(file))) | 51 return (T)XmlDefaultSerializer<T>.Instance.DeserializeFromFile(file); |
54 return Deserialize<T>(reader); | |
55 } | 52 } |
56 | 53 |
57 public static T DeserializeFromString<T>(string data) { | 54 public static T DeserializeFromString<T>(string data) { |
58 return SerializersPool<T>.Instance.DeserializeFromString(data); | 55 return (T)XmlDefaultSerializer<T>.Instance.DeserializeFromString(data); |
59 } | 56 } |
60 | 57 |
61 public static T DeserializeFromXmlNode<T>(XmlNode node) { | 58 public static T DeserializeFromXmlNode<T>(XmlNode node) { |
62 Safe.ArgumentNotNull(node, nameof(node)); | 59 Safe.ArgumentNotNull(node, nameof(node)); |
63 using (var reader = node.CreateNavigator().ReadSubtree()) | 60 using (var reader = node.CreateNavigator().ReadSubtree()) |
64 return SerializersPool<T>.Instance.Deserialize(reader); | 61 return (T)XmlDefaultSerializer<T>.Instance.Deserialize(reader); |
65 } | 62 } |
66 | 63 |
67 public static T DeserializeJson<T>(TextReader textReader) { | 64 public static T DeserializeJson<T>(TextReader textReader) { |
68 var options = new JsonXmlReaderOptions { | 65 var options = new JsonXmlReaderOptions { |
69 NamespaceUri = typeof(T).GetCustomAttribute<XmlRootAttribute>()?.Namespace, | 66 NamespaceUri = typeof(T).GetCustomAttribute<XmlRootAttribute>()?.Namespace, |
70 RootName = typeof(T).Name, | 67 RootName = typeof(T).Name, |
71 FlattenArrays = true | 68 FlattenArrays = true |
72 }; | 69 }; |
73 | 70 |
74 using(var reader = JsonXmlReader.CreateJsonXmlReader(textReader, options)) | 71 using (var reader = JsonXmlReader.CreateJsonXmlReader(textReader, options)) |
75 return Deserialize<T>(reader); | 72 return Deserialize<T>(reader); |
76 } | 73 } |
77 | 74 |
78 public static T DeserializeJsonFromString<T>(string data) { | 75 public static T DeserializeJsonFromString<T>(string data) { |
79 using(var reader = new StringReader(data)) { | 76 using (var reader = new StringReader(data)) { |
80 return DeserializeJson<T>(reader); | 77 return DeserializeJson<T>(reader); |
81 } | 78 } |
82 } | 79 } |
83 | 80 |
84 public static void SerializeJson<T>(TextWriter writer, T obj) { | 81 public static void SerializeJson<T>(TextWriter writer, T obj) { |
85 var doc = SerializeAsXmlDocument(obj); | 82 var doc = SerializeAsXmlDocument(obj); |
86 XmlToJson.Default.Transform(doc, null, writer); | 83 XmlToJson.Default.Transform(doc, null, writer); |
87 } | 84 } |
88 | 85 |
89 public static string SerializeJsonAsString<T>(T obj) { | 86 public static string SerializeJsonAsString<T>(T obj) { |
90 using(var writer = new StringWriter()) { | 87 using (var writer = new StringWriter()) { |
91 SerializeJson(writer, obj); | 88 SerializeJson(writer, obj); |
92 return writer.ToString(); | 89 return writer.ToString(); |
93 } | 90 } |
94 } | 91 } |
95 } | 92 } |