aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Serialization
diff options
context:
space:
mode:
authorLukePulverenti <luke.pulverenti@gmail.com>2013-02-20 20:33:05 -0500
committerLukePulverenti <luke.pulverenti@gmail.com>2013-02-20 20:33:05 -0500
commit767cdc1f6f6a63ce997fc9476911e2c361f9d402 (patch)
tree49add55976f895441167c66cfa95e5c7688d18ce /MediaBrowser.Common/Serialization
parent845554722efaed872948a9e0f7202e3ef52f1b6e (diff)
Pushing missing changes
Diffstat (limited to 'MediaBrowser.Common/Serialization')
-rw-r--r--MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs160
-rw-r--r--MediaBrowser.Common/Serialization/JsonSerializer.cs333
-rw-r--r--MediaBrowser.Common/Serialization/JsvSerializer.cs44
-rw-r--r--MediaBrowser.Common/Serialization/ProtobufSerializer.cs53
-rw-r--r--MediaBrowser.Common/Serialization/XmlSerializer.cs268
5 files changed, 629 insertions, 229 deletions
diff --git a/MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs b/MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs
new file mode 100644
index 000000000..f83b31322
--- /dev/null
+++ b/MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs
@@ -0,0 +1,160 @@
+using MediaBrowser.Common.Mef;
+using ProtoBuf;
+using ProtoBuf.Meta;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+
+namespace MediaBrowser.Common.Serialization
+{
+ /// <summary>
+ /// Creates a compiled protobuf serializer based on a set of assemblies
+ /// </summary>
+ public class DynamicProtobufSerializer
+ {
+ /// <summary>
+ /// Gets or sets the type model.
+ /// </summary>
+ /// <value>The type model.</value>
+ public TypeModel TypeModel { get; set; }
+
+ /// <summary>
+ /// Serializes to stream.
+ /// </summary>
+ /// <param name="obj">The obj.</param>
+ /// <param name="stream">The stream.</param>
+ /// <exception cref="System.ArgumentNullException">obj</exception>
+ public void SerializeToStream(object obj, Stream stream)
+ {
+ if (obj == null)
+ {
+ throw new ArgumentNullException("obj");
+ }
+ if (stream == null)
+ {
+ throw new ArgumentNullException("stream");
+ }
+
+ TypeModel.Serialize(stream, obj);
+ }
+
+ /// <summary>
+ /// Deserializes from stream.
+ /// </summary>
+ /// <param name="stream">The stream.</param>
+ /// <param name="type">The type.</param>
+ /// <returns>System.Object.</returns>
+ /// <exception cref="System.ArgumentNullException">stream</exception>
+ public object DeserializeFromStream(Stream stream, Type type)
+ {
+ if (stream == null)
+ {
+ throw new ArgumentNullException("stream");
+ }
+
+ return TypeModel.Deserialize(stream, null, type);
+ }
+
+ /// <summary>
+ /// Deserializes from stream.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="stream">The stream.</param>
+ /// <returns>``0.</returns>
+ public T DeserializeFromStream<T>(Stream stream)
+ where T : class
+ {
+ return DeserializeFromStream(stream, typeof(T)) as T;
+ }
+
+ /// <summary>
+ /// Serializes to file.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="obj">The obj.</param>
+ /// <param name="file">The file.</param>
+ /// <exception cref="System.ArgumentNullException">file</exception>
+ public void SerializeToFile<T>(T obj, string file)
+ {
+ if (string.IsNullOrEmpty(file))
+ {
+ throw new ArgumentNullException("file");
+ }
+
+ using (Stream stream = File.Open(file, FileMode.Create))
+ {
+ SerializeToStream(obj, stream);
+ }
+ }
+
+ /// <summary>
+ /// Deserializes from file.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="file">The file.</param>
+ /// <returns>``0.</returns>
+ /// <exception cref="System.ArgumentNullException">file</exception>
+ public T DeserializeFromFile<T>(string file)
+ where T : class
+ {
+ if (string.IsNullOrEmpty(file))
+ {
+ throw new ArgumentNullException("file");
+ }
+
+ using (Stream stream = File.OpenRead(file))
+ {
+ return DeserializeFromStream<T>(stream);
+ }
+ }
+
+ /// <summary>
+ /// Serializes to bytes.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="obj">The obj.</param>
+ /// <returns>System.Byte[][].</returns>
+ /// <exception cref="System.ArgumentNullException">obj</exception>
+ public byte[] SerializeToBytes<T>(T obj)
+ where T : class
+ {
+ if (obj == null)
+ {
+ throw new ArgumentNullException("obj");
+ }
+
+ using (var stream = new MemoryStream())
+ {
+ SerializeToStream(obj, stream);
+ return stream.ToArray();
+ }
+ }
+
+ /// <summary>
+ /// Creates the specified assemblies.
+ /// </summary>
+ /// <param name="assemblies">The assemblies.</param>
+ /// <returns>DynamicProtobufSerializer.</returns>
+ /// <exception cref="System.ArgumentNullException">assemblies</exception>
+ public static DynamicProtobufSerializer Create(IEnumerable<Assembly> assemblies)
+ {
+ if (assemblies == null)
+ {
+ throw new ArgumentNullException("assemblies");
+ }
+
+ var model = TypeModel.Create();
+ var attributeType = typeof(ProtoContractAttribute);
+
+ // Find all ProtoContracts in the current assembly
+ foreach (var type in assemblies.SelectMany(a => MefUtils.GetTypes(a).Where(t => Attribute.IsDefined(t, attributeType))))
+ {
+ model.Add(type, true);
+ }
+
+ return new DynamicProtobufSerializer { TypeModel = model.Compile() };
+ }
+ }
+}
diff --git a/MediaBrowser.Common/Serialization/JsonSerializer.cs b/MediaBrowser.Common/Serialization/JsonSerializer.cs
index f5d2abe33..5b6e354a8 100644
--- a/MediaBrowser.Common/Serialization/JsonSerializer.cs
+++ b/MediaBrowser.Common/Serialization/JsonSerializer.cs
@@ -1,74 +1,259 @@
-using System;
-using System.IO;
-
-namespace MediaBrowser.Common.Serialization
-{
- /// <summary>
- /// Provides a wrapper around third party json serialization.
- /// </summary>
- public class JsonSerializer
- {
- public static void SerializeToStream<T>(T obj, Stream stream)
- {
- Configure();
-
- ServiceStack.Text.JsonSerializer.SerializeToStream(obj, stream);
- }
-
- public static void SerializeToFile<T>(T obj, string file)
- {
- Configure();
-
- using (Stream stream = File.Open(file, FileMode.Create))
- {
- ServiceStack.Text.JsonSerializer.SerializeToStream(obj, stream);
- }
- }
-
- public static object DeserializeFromFile(Type type, string file)
- {
- Configure();
-
- using (Stream stream = File.OpenRead(file))
- {
- return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
- }
- }
-
- public static T DeserializeFromFile<T>(string file)
- {
- Configure();
-
- using (Stream stream = File.OpenRead(file))
- {
- return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
- }
- }
-
- public static T DeserializeFromStream<T>(Stream stream)
- {
- Configure();
-
- return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
- }
-
- public static object DeserializeFromStream(Stream stream, Type type)
- {
- Configure();
-
- return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
- }
-
- private static bool _isConfigured;
- private static void Configure()
- {
- if (!_isConfigured)
- {
- ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.JsonDateHandler.ISO8601;
- ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
- ServiceStack.Text.JsConfig.IncludeNullValues = false;
- _isConfigured = true;
- }
- }
- }
-}
+using System;
+using System.IO;
+
+namespace MediaBrowser.Common.Serialization
+{
+ /// <summary>
+ /// Provides a wrapper around third party json serialization.
+ /// </summary>
+ public class JsonSerializer
+ {
+ /// <summary>
+ /// Serializes to stream.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="obj">The obj.</param>
+ /// <param name="stream">The stream.</param>
+ /// <exception cref="System.ArgumentNullException">obj</exception>
+ public static void SerializeToStream<T>(T obj, Stream stream)
+ where T : class
+ {
+ if (obj == null)
+ {
+ throw new ArgumentNullException("obj");
+ }
+
+ if (stream == null)
+ {
+ throw new ArgumentNullException("stream");
+ }
+
+ Configure();
+
+ ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), stream);
+ }
+
+ /// <summary>
+ /// Serializes to file.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="obj">The obj.</param>
+ /// <param name="file">The file.</param>
+ /// <exception cref="System.ArgumentNullException">obj</exception>
+ public static void SerializeToFile<T>(T obj, string file)
+ where T : class
+ {
+ if (obj == null)
+ {
+ throw new ArgumentNullException("obj");
+ }
+
+ if (string.IsNullOrEmpty(file))
+ {
+ throw new ArgumentNullException("file");
+ }
+
+ Configure();
+
+ using (Stream stream = File.Open(file, FileMode.Create))
+ {
+ SerializeToStream(obj, stream);
+ }
+ }
+
+ /// <summary>
+ /// Deserializes from file.
+ /// </summary>
+ /// <param name="type">The type.</param>
+ /// <param name="file">The file.</param>
+ /// <returns>System.Object.</returns>
+ /// <exception cref="System.ArgumentNullException">type</exception>
+ public static object DeserializeFromFile(Type type, string file)
+ {
+ if (type == null)
+ {
+ throw new ArgumentNullException("type");
+ }
+
+ if (string.IsNullOrEmpty(file))
+ {
+ throw new ArgumentNullException("file");
+ }
+
+ Configure();
+
+ using (Stream stream = File.OpenRead(file))
+ {
+ return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
+ }
+ }
+
+ /// <summary>
+ /// Deserializes from file.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="file">The file.</param>
+ /// <returns>``0.</returns>
+ /// <exception cref="System.ArgumentNullException">file</exception>
+ public static T DeserializeFromFile<T>(string file)
+ where T : class
+ {
+ if (string.IsNullOrEmpty(file))
+ {
+ throw new ArgumentNullException("file");
+ }
+
+ Configure();
+
+ using (Stream stream = File.OpenRead(file))
+ {
+ return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
+ }
+ }
+
+ /// <summary>
+ /// Deserializes from stream.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="stream">The stream.</param>
+ /// <returns>``0.</returns>
+ /// <exception cref="System.ArgumentNullException">stream</exception>
+ public static T DeserializeFromStream<T>(Stream stream)
+ {
+ if (stream == null)
+ {
+ throw new ArgumentNullException("stream");
+ }
+
+ Configure();
+
+ return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
+ }
+
+ /// <summary>
+ /// Deserializes from string.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="text">The text.</param>
+ /// <returns>``0.</returns>
+ /// <exception cref="System.ArgumentNullException">text</exception>
+ public static T DeserializeFromString<T>(string text)
+ {
+ if (string.IsNullOrEmpty(text))
+ {
+ throw new ArgumentNullException("text");
+ }
+
+ Configure();
+
+ return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(text);
+ }
+
+ /// <summary>
+ /// Deserializes from stream.
+ /// </summary>
+ /// <param name="stream">The stream.</param>
+ /// <param name="type">The type.</param>
+ /// <returns>System.Object.</returns>
+ /// <exception cref="System.ArgumentNullException">stream</exception>
+ public static object DeserializeFromStream(Stream stream, Type type)
+ {
+ if (stream == null)
+ {
+ throw new ArgumentNullException("stream");
+ }
+
+ if (type == null)
+ {
+ throw new ArgumentNullException("type");
+ }
+
+ Configure();
+
+ return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
+ }
+
+ /// <summary>
+ /// The _is configured
+ /// </summary>
+ private static bool _isConfigured;
+ /// <summary>
+ /// Configures this instance.
+ /// </summary>
+ internal static void Configure()
+ {
+ if (!_isConfigured)
+ {
+ ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.JsonDateHandler.ISO8601;
+ ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
+ ServiceStack.Text.JsConfig.IncludeNullValues = false;
+ _isConfigured = true;
+ }
+ }
+
+ /// <summary>
+ /// Deserializes from string.
+ /// </summary>
+ /// <param name="json">The json.</param>
+ /// <param name="type">The type.</param>
+ /// <returns>System.Object.</returns>
+ /// <exception cref="System.ArgumentNullException">json</exception>
+ public static object DeserializeFromString(string json, Type type)
+ {
+ if (string.IsNullOrEmpty(json))
+ {
+ throw new ArgumentNullException("json");
+ }
+
+ if (type == null)
+ {
+ throw new ArgumentNullException("type");
+ }
+
+ Configure();
+
+ return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
+ }
+
+ /// <summary>
+ /// Serializes to string.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="obj">The obj.</param>
+ /// <returns>System.String.</returns>
+ /// <exception cref="System.ArgumentNullException">obj</exception>
+ public static string SerializeToString<T>(T obj)
+ where T : class
+ {
+ if (obj == null)
+ {
+ throw new ArgumentNullException("obj");
+ }
+
+ Configure();
+ return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType());
+ }
+
+ /// <summary>
+ /// Serializes to bytes.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="obj">The obj.</param>
+ /// <returns>System.Byte[][].</returns>
+ /// <exception cref="System.ArgumentNullException">obj</exception>
+ public static byte[] SerializeToBytes<T>(T obj)
+ where T : class
+ {
+ if (obj == null)
+ {
+ throw new ArgumentNullException("obj");
+ }
+
+ using (var stream = new MemoryStream())
+ {
+ SerializeToStream(obj, stream);
+ return stream.ToArray();
+ }
+ }
+ }
+}
diff --git a/MediaBrowser.Common/Serialization/JsvSerializer.cs b/MediaBrowser.Common/Serialization/JsvSerializer.cs
deleted file mode 100644
index 41e5ea800..000000000
--- a/MediaBrowser.Common/Serialization/JsvSerializer.cs
+++ /dev/null
@@ -1,44 +0,0 @@
-using System;
-using System.IO;
-
-namespace MediaBrowser.Common.Serialization
-{
- /// <summary>
- /// This adds support for ServiceStack's proprietary JSV output format.
- /// It's a hybrid of Json and Csv but the serializer performs about 25% faster and output runs about 10% smaller
- /// http://www.servicestack.net/benchmarks/NorthwindDatabaseRowsSerialization.100000-times.2010-08-17.html
- /// </summary>
- public static class JsvSerializer
- {
- public static void SerializeToStream<T>(T obj, Stream stream)
- {
- ServiceStack.Text.TypeSerializer.SerializeToStream(obj, stream);
- }
-
- public static T DeserializeFromStream<T>(Stream stream)
- {
- return ServiceStack.Text.TypeSerializer.DeserializeFromStream<T>(stream);
- }
-
- public static object DeserializeFromStream(Stream stream, Type type)
- {
- return ServiceStack.Text.TypeSerializer.DeserializeFromStream(type, stream);
- }
-
- public static void SerializeToFile<T>(T obj, string file)
- {
- using (Stream stream = File.Open(file, FileMode.Create))
- {
- SerializeToStream(obj, stream);
- }
- }
-
- public static T DeserializeFromFile<T>(string file)
- {
- using (Stream stream = File.OpenRead(file))
- {
- return DeserializeFromStream<T>(stream);
- }
- }
- }
-}
diff --git a/MediaBrowser.Common/Serialization/ProtobufSerializer.cs b/MediaBrowser.Common/Serialization/ProtobufSerializer.cs
deleted file mode 100644
index 1c79a272d..000000000
--- a/MediaBrowser.Common/Serialization/ProtobufSerializer.cs
+++ /dev/null
@@ -1,53 +0,0 @@
-using System;
-using System.IO;
-
-namespace MediaBrowser.Common.Serialization
-{
- /// <summary>
- /// Protocol buffers is google's binary serialization format. This is a .NET implementation of it.
- /// You have to tag your classes with some annoying attributes, but in return you get the fastest serialization around with the smallest possible output.
- /// </summary>
- public static class ProtobufSerializer
- {
- /// <summary>
- /// This is an auto-generated Protobuf Serialization assembly for best performance.
- /// It is created during the Model project's post-build event.
- /// This means that this class can currently only handle types within the Model project.
- /// If we need to, we can always add a param indicating whether or not the model serializer should be used.
- /// </summary>
- private static readonly ProtobufModelSerializer ProtobufModelSerializer = new ProtobufModelSerializer();
-
- public static void SerializeToStream<T>(T obj, Stream stream)
- {
- ProtobufModelSerializer.Serialize(stream, obj);
- }
-
- public static T DeserializeFromStream<T>(Stream stream)
- where T : class
- {
- return ProtobufModelSerializer.Deserialize(stream, null, typeof(T)) as T;
- }
-
- public static object DeserializeFromStream(Stream stream, Type type)
- {
- return ProtobufModelSerializer.Deserialize(stream, null, type);
- }
-
- public static void SerializeToFile<T>(T obj, string file)
- {
- using (Stream stream = File.Open(file, FileMode.Create))
- {
- SerializeToStream(obj, stream);
- }
- }
-
- public static T DeserializeFromFile<T>(string file)
- where T : class
- {
- using (Stream stream = File.OpenRead(file))
- {
- return DeserializeFromStream<T>(stream);
- }
- }
- }
-}
diff --git a/MediaBrowser.Common/Serialization/XmlSerializer.cs b/MediaBrowser.Common/Serialization/XmlSerializer.cs
index 11ef17c3d..8737aa818 100644
--- a/MediaBrowser.Common/Serialization/XmlSerializer.cs
+++ b/MediaBrowser.Common/Serialization/XmlSerializer.cs
@@ -1,58 +1,210 @@
-using System;
-using System.IO;
-
-namespace MediaBrowser.Common.Serialization
-{
- /// <summary>
- /// Provides a wrapper around third party xml serialization.
- /// </summary>
- public class XmlSerializer
- {
- public static void SerializeToStream<T>(T obj, Stream stream)
- {
- ServiceStack.Text.XmlSerializer.SerializeToStream(obj, stream);
- }
-
- public static T DeserializeFromStream<T>(Stream stream)
- {
- return ServiceStack.Text.XmlSerializer.DeserializeFromStream<T>(stream);
- }
-
- public static object DeserializeFromStream(Type type, Stream stream)
- {
- return ServiceStack.Text.XmlSerializer.DeserializeFromStream(type, stream);
- }
-
- public static void SerializeToFile<T>(T obj, string file)
- {
- using (var stream = new FileStream(file, FileMode.Create))
- {
- SerializeToStream(obj, stream);
- }
- }
-
- public static T DeserializeFromFile<T>(string file)
- {
- using (Stream stream = File.OpenRead(file))
- {
- return DeserializeFromStream<T>(stream);
- }
- }
-
- public static void SerializeToFile(object obj, string file)
- {
- using (var stream = new FileStream(file, FileMode.Create))
- {
- ServiceStack.Text.XmlSerializer.SerializeToStream(obj, stream);
- }
- }
-
- public static object DeserializeFromFile(Type type, string file)
- {
- using (Stream stream = File.OpenRead(file))
- {
- return DeserializeFromStream(type, stream);
- }
- }
- }
-}
+using MediaBrowser.Common.Logging;
+using System;
+using System.IO;
+using System.Linq;
+using System.Xml;
+
+namespace MediaBrowser.Common.Serialization
+{
+ /// <summary>
+ /// Provides a wrapper around third party xml serialization.
+ /// </summary>
+ public class XmlSerializer
+ {
+ /// <summary>
+ /// Serializes to writer.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="obj">The obj.</param>
+ /// <param name="writer">The writer.</param>
+ public static void SerializeToWriter<T>(T obj, XmlTextWriter writer)
+ {
+ writer.Formatting = Formatting.Indented;
+ var netSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
+ netSerializer.Serialize(writer, obj);
+ }
+
+ /// <summary>
+ /// Serializes to writer.
+ /// </summary>
+ /// <param name="obj">The obj.</param>
+ /// <param name="writer">The writer.</param>
+ public static void SerializeToWriter(object obj, XmlTextWriter writer)
+ {
+ writer.Formatting = Formatting.Indented;
+ var netSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
+ netSerializer.Serialize(writer, obj);
+ }
+
+ /// <summary>
+ /// Deserializes from stream.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="stream">The stream.</param>
+ /// <returns>``0.</returns>
+ public static T DeserializeFromStream<T>(Stream stream)
+ {
+ using (var reader = new XmlTextReader(stream))
+ {
+ var netSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
+
+ return (T)netSerializer.Deserialize(reader);
+ }
+ }
+
+ /// <summary>
+ /// Deserializes from stream.
+ /// </summary>
+ /// <param name="type">The type.</param>
+ /// <param name="stream">The stream.</param>
+ /// <returns>System.Object.</returns>
+ public static object DeserializeFromStream(Type type, Stream stream)
+ {
+ using (var reader = new XmlTextReader(stream))
+ {
+ var netSerializer = new System.Xml.Serialization.XmlSerializer(type);
+
+ return netSerializer.Deserialize(reader);
+ }
+ }
+
+ /// <summary>
+ /// Serializes to stream.
+ /// </summary>
+ /// <param name="obj">The obj.</param>
+ /// <param name="stream">The stream.</param>
+ public static void SerializeToStream(object obj, Stream stream)
+ {
+ using (var writer = new XmlTextWriter(stream, null))
+ {
+ SerializeToWriter(obj, writer);
+ }
+ }
+
+ /// <summary>
+ /// Deserializes from file.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="file">The file.</param>
+ /// <returns>``0.</returns>
+ public static T DeserializeFromFile<T>(string file)
+ {
+ using (var stream = File.OpenRead(file))
+ {
+ return DeserializeFromStream<T>(stream);
+ }
+ }
+
+ /// <summary>
+ /// Serializes to file.
+ /// </summary>
+ /// <param name="obj">The obj.</param>
+ /// <param name="file">The file.</param>
+ public static void SerializeToFile(object obj, string file)
+ {
+ using (var stream = new FileStream(file, FileMode.Create))
+ {
+ SerializeToStream(obj, stream);
+ }
+ }
+
+ /// <summary>
+ /// Deserializes from file.
+ /// </summary>
+ /// <param name="type">The type.</param>
+ /// <param name="file">The file.</param>
+ /// <returns>System.Object.</returns>
+ public static object DeserializeFromFile(Type type, string file)
+ {
+ using (var stream = File.OpenRead(file))
+ {
+ return DeserializeFromStream(type, stream);
+ }
+ }
+
+ /// <summary>
+ /// Deserializes from bytes.
+ /// </summary>
+ /// <param name="type">The type.</param>
+ /// <param name="buffer">The buffer.</param>
+ /// <returns>System.Object.</returns>
+ public static object DeserializeFromBytes(Type type, byte[] buffer)
+ {
+ using (var stream = new MemoryStream(buffer))
+ {
+ return DeserializeFromStream(type, stream);
+ }
+ }
+
+ /// <summary>
+ /// Serializes to bytes.
+ /// </summary>
+ /// <param name="obj">The obj.</param>
+ /// <returns>System.Byte[][].</returns>
+ public static byte[] SerializeToBytes(object obj)
+ {
+ using (var stream = new MemoryStream())
+ {
+ SerializeToStream(obj, stream);
+
+ return stream.ToArray();
+ }
+ }
+
+ /// <summary>
+ /// Reads an xml configuration file from the file system
+ /// It will immediately re-serialize and save if new serialization data is available due to property changes
+ /// </summary>
+ /// <param name="type">The type.</param>
+ /// <param name="path">The path.</param>
+ /// <returns>System.Object.</returns>
+ public static object GetXmlConfiguration(Type type, string path)
+ {
+ Logger.LogInfo("Loading {0} at {1}", type.Name, path);
+
+ object configuration;
+
+ byte[] buffer = null;
+
+ // Use try/catch to avoid the extra file system lookup using File.Exists
+ try
+ {
+ buffer = File.ReadAllBytes(path);
+
+ configuration = DeserializeFromBytes(type, buffer);
+ }
+ catch (FileNotFoundException)
+ {
+ configuration = Activator.CreateInstance(type);
+ }
+
+ // Take the object we just got and serialize it back to bytes
+ var newBytes = SerializeToBytes(configuration);
+
+ // If the file didn't exist before, or if something has changed, re-save
+ if (buffer == null || !buffer.SequenceEqual(newBytes))
+ {
+ Logger.LogInfo("Saving {0} to {1}", type.Name, path);
+
+ // Save it after load in case we got new items
+ File.WriteAllBytes(path, newBytes);
+ }
+
+ return configuration;
+ }
+
+ /// <summary>
+ /// Reads an xml configuration file from the file system
+ /// It will immediately save the configuration after loading it, just
+ /// in case there are new serializable properties
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <param name="path">The path.</param>
+ /// <returns>``0.</returns>
+ public static T GetXmlConfiguration<T>(string path)
+ where T : class
+ {
+ return GetXmlConfiguration(typeof(T), path) as T;
+ }
+ }
+}