Mercurial > pub > ImplabNet
diff Implab/Xml/SerializersPool.cs @ 229:5f7a3e1d32b9 v2
JsonXmlReader performance tuning
JsonScanner now operates strings and doesn't
parses number and literals.
Added SerializationHelpers to common serialize/deserialize operations
author | cin |
---|---|
date | Tue, 12 Sep 2017 19:07:42 +0300 |
parents | |
children | 6691aff01de1 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/Xml/SerializersPool.cs Tue Sep 12 19:07:42 2017 +0300 @@ -0,0 +1,76 @@ +using Implab.Components; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using System.Xml.Serialization; + +namespace Implab.Xml { + public class SerializersPool<T> : ObjectPool<XmlSerializer> { + + static readonly SerializersPool<T> _instance = new SerializersPool<T>(); + + public static SerializersPool<T> Instance { + get { return _instance; } + } + + #region implemented abstract members of ObjectPool + protected override XmlSerializer CreateInstance() { + return new XmlSerializer(typeof(T)); + } + #endregion + + public T DeserializeFromString(string data) { + using (var reader = new StringReader(data)) { + return Deserialize(reader); + } + } + + public T Deserialize(TextReader reader) { + var sr = Allocate(); + try { + return (T)sr.Deserialize(reader); + } finally { + Release(sr); + } + } + + public T Deserialize(XmlReader reader) { + var sr = Allocate(); + try { + return (T)sr.Deserialize(reader); + } finally { + Release(sr); + } + } + + public string SerializeAsString(T data) { + using (var writer = new StringWriter()) { + Serialize(writer, data); + return writer.ToString(); + } + } + + public void Serialize(TextWriter writer, T data) { + var sr = Allocate(); + try { + sr.Serialize(writer, data); + } finally { + Release(sr); + } + } + + public void Serialize(XmlWriter writer, T data) { + var sr = Allocate(); + try { + sr.Serialize(writer, data); + } finally { + Release(sr); + } + } + + } +}