diff options
| author | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-24 16:53:54 -0500 |
|---|---|---|
| committer | LukePulverenti <luke.pulverenti@gmail.com> | 2013-02-24 16:53:54 -0500 |
| commit | 8ce3e74e8112a94773df22827849bf274fc88198 (patch) | |
| tree | a4ce1edf34466be697e2e432609f6be80b6c6df6 /MediaBrowser.Common/Serialization | |
| parent | 6c86721f6de2acbe68e9419064ff21111ff3a223 (diff) | |
More DI
Diffstat (limited to 'MediaBrowser.Common/Serialization')
| -rw-r--r-- | MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs | 157 | ||||
| -rw-r--r-- | MediaBrowser.Common/Serialization/JsonSerializer.cs | 259 | ||||
| -rw-r--r-- | MediaBrowser.Common/Serialization/XmlSerializer.cs | 212 |
3 files changed, 0 insertions, 628 deletions
diff --git a/MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs b/MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs deleted file mode 100644 index 359cf9da0..000000000 --- a/MediaBrowser.Common/Serialization/DynamicProtobufSerializer.cs +++ /dev/null @@ -1,157 +0,0 @@ -using ProtoBuf; -using ProtoBuf.Meta; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; - -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> - /// <returns>DynamicProtobufSerializer.</returns> - /// <exception cref="System.ArgumentNullException">assemblies</exception> - public static DynamicProtobufSerializer Create(IEnumerable<Type> types) - { - if (types == null) - { - throw new ArgumentNullException("types"); - } - - var model = TypeModel.Create(); - var attributeType = typeof(ProtoContractAttribute); - - // Find all ProtoContracts in the current assembly - foreach (var type in types.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 deleted file mode 100644 index 5b6e354a8..000000000 --- a/MediaBrowser.Common/Serialization/JsonSerializer.cs +++ /dev/null @@ -1,259 +0,0 @@ -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/XmlSerializer.cs b/MediaBrowser.Common/Serialization/XmlSerializer.cs deleted file mode 100644 index 07010a5d2..000000000 --- a/MediaBrowser.Common/Serialization/XmlSerializer.cs +++ /dev/null @@ -1,212 +0,0 @@ -using MediaBrowser.Model.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> - /// <param name="logger">The logger.</param> - /// <returns>System.Object.</returns> - public static object GetXmlConfiguration(Type type, string path, ILogger logger) - { - logger.Info("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.Info("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> - /// <param name="logger">The logger.</param> - /// <returns>``0.</returns> - public static T GetXmlConfiguration<T>(string path, ILogger logger) - where T : class - { - return GetXmlConfiguration(typeof(T), path, logger) as T; - } - } -} |
