diff 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
line wrap: on
line diff
--- a/Implab/Xml/SerializationHelpers.cs	Sat Apr 28 18:48:09 2018 +0300
+++ b/Implab/Xml/SerializationHelpers.cs	Thu May 03 09:59:44 2018 +0300
@@ -12,17 +12,17 @@
 namespace Implab.Xml {
     public static class SerializationHelpers {
         public static string SerializeAsString<T>(T obj) {
-            return SerializersPool<T>.Instance.SerializeAsString(obj);
+            return XmlDefaultSerializer<T>.Instance.SerializeAsString(obj);
         }
 
         public static void Serialize<T>(XmlWriter writer, T obj) {
-            SerializersPool<T>.Instance.Serialize(writer, obj);
+            XmlDefaultSerializer<T>.Instance.Serialize(writer, obj);
         }
 
         public static XmlDocument SerializeAsXmlDocument<T>(T obj) {
             var doc = new XmlDocument();
             using (var writer = doc.CreateNavigator().AppendChild()) {
-                SerializersPool<T>.Instance.Serialize(writer, obj);
+                XmlDefaultSerializer<T>.Instance.Serialize(writer, obj);
             }
             return doc;
         }
@@ -30,38 +30,35 @@
         public static XDocument SerializeAsXDocument<T>(T obj) {
             var doc = new XDocument();
             using (var writer = doc.CreateWriter()) {
-                SerializersPool<T>.Instance.Serialize(writer, obj);
+                XmlDefaultSerializer<T>.Instance.Serialize(writer, obj);
             }
             return doc;
         }
 
         public static void SerializeToFile<T>(string file, T obj) {
-            using (var writer = File.CreateText(file))
-                SerializersPool<T>.Instance.Serialize(writer, obj);
+            XmlDefaultSerializer<T>.Instance.SerializeToFile(obj, file);
         }
 
         public static void SerializeToElementChild<T>(XmlElement element, T obj) {
-            using(var writer = element.CreateNavigator().AppendChild())
-                SerializersPool<T>.Instance.Serialize(writer, obj);
+            XmlDefaultSerializer<T>.Instance.Serialize(obj, element);
         }
 
         public static T Deserialize<T>(XmlReader reader) {
-            return SerializersPool<T>.Instance.Deserialize(reader);
+            return (T)XmlDefaultSerializer<T>.Instance.Deserialize(reader);
         }
 
         public static T DeserializeFromFile<T>(string file) {
-            using(var reader = XmlReader.Create(File.OpenText(file)))
-                return Deserialize<T>(reader);
+            return (T)XmlDefaultSerializer<T>.Instance.DeserializeFromFile(file);
         }
 
         public static T DeserializeFromString<T>(string data) {
-            return SerializersPool<T>.Instance.DeserializeFromString(data);
+            return (T)XmlDefaultSerializer<T>.Instance.DeserializeFromString(data);
         }
 
         public static T DeserializeFromXmlNode<T>(XmlNode node) {
             Safe.ArgumentNotNull(node, nameof(node));
             using (var reader = node.CreateNavigator().ReadSubtree())
-                return SerializersPool<T>.Instance.Deserialize(reader);
+                return (T)XmlDefaultSerializer<T>.Instance.Deserialize(reader);
         }
 
         public static T DeserializeJson<T>(TextReader textReader) {
@@ -71,12 +68,12 @@
                 FlattenArrays = true
             };
 
-            using(var reader = JsonXmlReader.CreateJsonXmlReader(textReader, options))
+            using (var reader = JsonXmlReader.CreateJsonXmlReader(textReader, options))
                 return Deserialize<T>(reader);
         }
 
         public static T DeserializeJsonFromString<T>(string data) {
-            using(var reader = new StringReader(data)) {
+            using (var reader = new StringReader(data)) {
                 return DeserializeJson<T>(reader);
             }
         }
@@ -87,7 +84,7 @@
         }
 
         public static string SerializeJsonAsString<T>(T obj) {
-            using(var writer = new StringWriter()) {
+            using (var writer = new StringWriter()) {
                 SerializeJson(writer, obj);
                 return writer.ToString();
             }