From bfcd1b520fd79b893e721ba916ae5e1656407d2f Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 16 Aug 2017 02:43:41 -0400 Subject: merge common implementations and server implementations --- .../Serialization/JsonSerializer.cs | 227 --------------------- .../Serialization/XmlSerializer.cs | 130 ------------ 2 files changed, 357 deletions(-) delete mode 100644 Emby.Common.Implementations/Serialization/JsonSerializer.cs delete mode 100644 Emby.Common.Implementations/Serialization/XmlSerializer.cs (limited to 'Emby.Common.Implementations/Serialization') diff --git a/Emby.Common.Implementations/Serialization/JsonSerializer.cs b/Emby.Common.Implementations/Serialization/JsonSerializer.cs deleted file mode 100644 index c9db33689..000000000 --- a/Emby.Common.Implementations/Serialization/JsonSerializer.cs +++ /dev/null @@ -1,227 +0,0 @@ -using System; -using System.IO; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Serialization; - -namespace Emby.Common.Implementations.Serialization -{ - /// - /// Provides a wrapper around third party json serialization. - /// - public class JsonSerializer : IJsonSerializer - { - private readonly IFileSystem _fileSystem; - private readonly ILogger _logger; - - public JsonSerializer(IFileSystem fileSystem, ILogger logger) - { - _fileSystem = fileSystem; - _logger = logger; - Configure(); - } - - /// - /// Serializes to stream. - /// - /// The obj. - /// The stream. - /// obj - public void SerializeToStream(object obj, Stream stream) - { - if (obj == null) - { - throw new ArgumentNullException("obj"); - } - - if (stream == null) - { - throw new ArgumentNullException("stream"); - } - - ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), stream); - } - - /// - /// Serializes to file. - /// - /// The obj. - /// The file. - /// obj - public void SerializeToFile(object obj, string file) - { - if (obj == null) - { - throw new ArgumentNullException("obj"); - } - - if (string.IsNullOrEmpty(file)) - { - throw new ArgumentNullException("file"); - } - - using (Stream stream = _fileSystem.GetFileStream(file, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read)) - { - SerializeToStream(obj, stream); - } - } - - private Stream OpenFile(string path) - { - _logger.Debug("Deserializing file {0}", path); - return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 131072); - } - - /// - /// Deserializes from file. - /// - /// The type. - /// The file. - /// System.Object. - /// type - public object DeserializeFromFile(Type type, string file) - { - if (type == null) - { - throw new ArgumentNullException("type"); - } - - if (string.IsNullOrEmpty(file)) - { - throw new ArgumentNullException("file"); - } - - using (Stream stream = OpenFile(file)) - { - return DeserializeFromStream(stream, type); - } - } - - /// - /// Deserializes from file. - /// - /// - /// The file. - /// ``0. - /// file - public T DeserializeFromFile(string file) - where T : class - { - if (string.IsNullOrEmpty(file)) - { - throw new ArgumentNullException("file"); - } - - using (Stream stream = OpenFile(file)) - { - return DeserializeFromStream(stream); - } - } - - /// - /// Deserializes from stream. - /// - /// - /// The stream. - /// ``0. - /// stream - public T DeserializeFromStream(Stream stream) - { - if (stream == null) - { - throw new ArgumentNullException("stream"); - } - - return ServiceStack.Text.JsonSerializer.DeserializeFromStream(stream); - } - - /// - /// Deserializes from string. - /// - /// - /// The text. - /// ``0. - /// text - public T DeserializeFromString(string text) - { - if (string.IsNullOrEmpty(text)) - { - throw new ArgumentNullException("text"); - } - - return ServiceStack.Text.JsonSerializer.DeserializeFromString(text); - } - - /// - /// Deserializes from stream. - /// - /// The stream. - /// The type. - /// System.Object. - /// stream - public object DeserializeFromStream(Stream stream, Type type) - { - if (stream == null) - { - throw new ArgumentNullException("stream"); - } - - if (type == null) - { - throw new ArgumentNullException("type"); - } - - return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream); - } - - /// - /// Configures this instance. - /// - private void Configure() - { - ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.DateHandler.ISO8601; - ServiceStack.Text.JsConfig.ExcludeTypeInfo = true; - ServiceStack.Text.JsConfig.IncludeNullValues = false; - ServiceStack.Text.JsConfig.AlwaysUseUtc = true; - ServiceStack.Text.JsConfig.AssumeUtc = true; - } - - /// - /// Deserializes from string. - /// - /// The json. - /// The type. - /// System.Object. - /// json - public object DeserializeFromString(string json, Type type) - { - if (string.IsNullOrEmpty(json)) - { - throw new ArgumentNullException("json"); - } - - if (type == null) - { - throw new ArgumentNullException("type"); - } - - return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type); - } - - /// - /// Serializes to string. - /// - /// The obj. - /// System.String. - /// obj - public string SerializeToString(object obj) - { - if (obj == null) - { - throw new ArgumentNullException("obj"); - } - - return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType()); - } - } -} diff --git a/Emby.Common.Implementations/Serialization/XmlSerializer.cs b/Emby.Common.Implementations/Serialization/XmlSerializer.cs deleted file mode 100644 index b5896e6b0..000000000 --- a/Emby.Common.Implementations/Serialization/XmlSerializer.cs +++ /dev/null @@ -1,130 +0,0 @@ -using MediaBrowser.Model.Serialization; -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.IO; -using System.Xml; -using System.Xml.Serialization; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.Logging; - -namespace Emby.Common.Implementations.Serialization -{ - /// - /// Provides a wrapper around third party xml serialization. - /// - public class MyXmlSerializer : IXmlSerializer - { - private readonly IFileSystem _fileSystem; - private readonly ILogger _logger; - - public MyXmlSerializer(IFileSystem fileSystem, ILogger logger) - { - _fileSystem = fileSystem; - _logger = logger; - } - - // Need to cache these - // http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html - private readonly Dictionary _serializers = - new Dictionary(); - - private XmlSerializer GetSerializer(Type type) - { - var key = type.FullName; - lock (_serializers) - { - XmlSerializer serializer; - if (!_serializers.TryGetValue(key, out serializer)) - { - serializer = new XmlSerializer(type); - _serializers[key] = serializer; - } - return serializer; - } - } - - /// - /// Serializes to writer. - /// - /// The obj. - /// The writer. - private void SerializeToWriter(object obj, XmlWriter writer) - { - var netSerializer = GetSerializer(obj.GetType()); - netSerializer.Serialize(writer, obj); - } - - /// - /// Deserializes from stream. - /// - /// The type. - /// The stream. - /// System.Object. - public object DeserializeFromStream(Type type, Stream stream) - { - using (var reader = XmlReader.Create(stream)) - { - var netSerializer = GetSerializer(type); - return netSerializer.Deserialize(reader); - } - } - - /// - /// Serializes to stream. - /// - /// The obj. - /// The stream. - public void SerializeToStream(object obj, Stream stream) - { - using (var writer = new XmlTextWriter(stream, null)) - { - writer.Formatting = Formatting.Indented; - SerializeToWriter(obj, writer); - } - } - - /// - /// Serializes to file. - /// - /// The obj. - /// The file. - public void SerializeToFile(object obj, string file) - { - _logger.Debug("Serializing to file {0}", file); - using (var stream = new FileStream(file, FileMode.Create)) - { - SerializeToStream(obj, stream); - } - } - - /// - /// Deserializes from file. - /// - /// The type. - /// The file. - /// System.Object. - public object DeserializeFromFile(Type type, string file) - { - _logger.Debug("Deserializing file {0}", file); - using (var stream = _fileSystem.OpenRead(file)) - { - return DeserializeFromStream(type, stream); - } - } - - /// - /// Deserializes from bytes. - /// - /// The type. - /// The buffer. - /// System.Object. - public object DeserializeFromBytes(Type type, byte[] buffer) - { - using (var stream = new MemoryStream(buffer)) - { - return DeserializeFromStream(type, stream); - } - } - } -} -- cgit v1.2.3