diff options
Diffstat (limited to 'Emby.Server.Implementations/Serialization')
| -rw-r--r-- | Emby.Server.Implementations/Serialization/JsonSerializer.cs | 74 | ||||
| -rw-r--r-- | Emby.Server.Implementations/Serialization/XmlSerializer.cs | 13 |
2 files changed, 43 insertions, 44 deletions
diff --git a/Emby.Server.Implementations/Serialization/JsonSerializer.cs b/Emby.Server.Implementations/Serialization/JsonSerializer.cs index e28acd769..53ef5d60c 100644 --- a/Emby.Server.Implementations/Serialization/JsonSerializer.cs +++ b/Emby.Server.Implementations/Serialization/JsonSerializer.cs @@ -1,9 +1,9 @@ -using System; +using System; using System.IO; +using System.Threading.Tasks; using MediaBrowser.Model.IO; -using Microsoft.Extensions.Logging; using MediaBrowser.Model.Serialization; -using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace Emby.Common.Implementations.Serialization { @@ -13,12 +13,11 @@ namespace Emby.Common.Implementations.Serialization public class JsonSerializer : IJsonSerializer { private readonly IFileSystem _fileSystem; - private readonly ILogger _logger; - public JsonSerializer(IFileSystem fileSystem, ILogger logger) + public JsonSerializer( + IFileSystem fileSystem) { _fileSystem = fileSystem; - _logger = logger; Configure(); } @@ -27,17 +26,17 @@ namespace Emby.Common.Implementations.Serialization /// </summary> /// <param name="obj">The obj.</param> /// <param name="stream">The stream.</param> - /// <exception cref="System.ArgumentNullException">obj</exception> + /// <exception cref="ArgumentNullException">obj</exception> public void SerializeToStream(object obj, Stream stream) { if (obj == null) { - throw new ArgumentNullException("obj"); + throw new ArgumentNullException(nameof(obj)); } if (stream == null) { - throw new ArgumentNullException("stream"); + throw new ArgumentNullException(nameof(stream)); } ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), stream); @@ -48,28 +47,27 @@ namespace Emby.Common.Implementations.Serialization /// </summary> /// <param name="obj">The obj.</param> /// <param name="file">The file.</param> - /// <exception cref="System.ArgumentNullException">obj</exception> + /// <exception cref="ArgumentNullException">obj</exception> public void SerializeToFile(object obj, string file) { if (obj == null) { - throw new ArgumentNullException("obj"); + throw new ArgumentNullException(nameof(obj)); } if (string.IsNullOrEmpty(file)) { - throw new ArgumentNullException("file"); + throw new ArgumentNullException(nameof(file)); } - using (Stream stream = _fileSystem.GetFileStream(file, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read)) + using (var stream = _fileSystem.GetFileStream(file, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read)) { SerializeToStream(obj, stream); } } - private Stream OpenFile(string path) + private static Stream OpenFile(string path) { - //_logger.LogDebug("Deserializing file {0}", path); return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 131072); } @@ -79,20 +77,20 @@ namespace Emby.Common.Implementations.Serialization /// <param name="type">The type.</param> /// <param name="file">The file.</param> /// <returns>System.Object.</returns> - /// <exception cref="System.ArgumentNullException">type</exception> + /// <exception cref="ArgumentNullException">type</exception> public object DeserializeFromFile(Type type, string file) { if (type == null) { - throw new ArgumentNullException("type"); + throw new ArgumentNullException(nameof(type)); } if (string.IsNullOrEmpty(file)) { - throw new ArgumentNullException("file"); + throw new ArgumentNullException(nameof(file)); } - using (Stream stream = OpenFile(file)) + using (var stream = OpenFile(file)) { return DeserializeFromStream(stream, type); } @@ -104,16 +102,16 @@ namespace Emby.Common.Implementations.Serialization /// <typeparam name="T"></typeparam> /// <param name="file">The file.</param> /// <returns>``0.</returns> - /// <exception cref="System.ArgumentNullException">file</exception> + /// <exception cref="ArgumentNullException">file</exception> public T DeserializeFromFile<T>(string file) where T : class { if (string.IsNullOrEmpty(file)) { - throw new ArgumentNullException("file"); + throw new ArgumentNullException(nameof(file)); } - using (Stream stream = OpenFile(file)) + using (var stream = OpenFile(file)) { return DeserializeFromStream<T>(stream); } @@ -125,12 +123,12 @@ namespace Emby.Common.Implementations.Serialization /// <typeparam name="T"></typeparam> /// <param name="stream">The stream.</param> /// <returns>``0.</returns> - /// <exception cref="System.ArgumentNullException">stream</exception> + /// <exception cref="ArgumentNullException">stream</exception> public T DeserializeFromStream<T>(Stream stream) { if (stream == null) { - throw new ArgumentNullException("stream"); + throw new ArgumentNullException(nameof(stream)); } return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream); @@ -140,7 +138,7 @@ namespace Emby.Common.Implementations.Serialization { if (stream == null) { - throw new ArgumentNullException("stream"); + throw new ArgumentNullException(nameof(stream)); } @@ -153,12 +151,12 @@ namespace Emby.Common.Implementations.Serialization /// <typeparam name="T"></typeparam> /// <param name="text">The text.</param> /// <returns>``0.</returns> - /// <exception cref="System.ArgumentNullException">text</exception> + /// <exception cref="ArgumentNullException">text</exception> public T DeserializeFromString<T>(string text) { if (string.IsNullOrEmpty(text)) { - throw new ArgumentNullException("text"); + throw new ArgumentNullException(nameof(text)); } return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(text); @@ -170,17 +168,17 @@ namespace Emby.Common.Implementations.Serialization /// <param name="stream">The stream.</param> /// <param name="type">The type.</param> /// <returns>System.Object.</returns> - /// <exception cref="System.ArgumentNullException">stream</exception> + /// <exception cref="ArgumentNullException">stream</exception> public object DeserializeFromStream(Stream stream, Type type) { if (stream == null) { - throw new ArgumentNullException("stream"); + throw new ArgumentNullException(nameof(stream)); } if (type == null) { - throw new ArgumentNullException("type"); + throw new ArgumentNullException(nameof(type)); } return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream); @@ -190,12 +188,12 @@ namespace Emby.Common.Implementations.Serialization { if (stream == null) { - throw new ArgumentNullException("stream"); + throw new ArgumentNullException(nameof(stream)); } if (type == null) { - throw new ArgumentNullException("type"); + throw new ArgumentNullException(nameof(type)); } using (var reader = new StreamReader(stream)) @@ -220,7 +218,7 @@ namespace Emby.Common.Implementations.Serialization ServiceStack.Text.JsConfig<Guid>.SerializeFn = SerializeGuid; } - private string SerializeGuid(Guid guid) + private static string SerializeGuid(Guid guid) { if (guid.Equals(Guid.Empty)) { @@ -236,17 +234,17 @@ namespace Emby.Common.Implementations.Serialization /// <param name="json">The json.</param> /// <param name="type">The type.</param> /// <returns>System.Object.</returns> - /// <exception cref="System.ArgumentNullException">json</exception> + /// <exception cref="ArgumentNullException">json</exception> public object DeserializeFromString(string json, Type type) { if (string.IsNullOrEmpty(json)) { - throw new ArgumentNullException("json"); + throw new ArgumentNullException(nameof(json)); } if (type == null) { - throw new ArgumentNullException("type"); + throw new ArgumentNullException(nameof(type)); } return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type); @@ -257,12 +255,12 @@ namespace Emby.Common.Implementations.Serialization /// </summary> /// <param name="obj">The obj.</param> /// <returns>System.String.</returns> - /// <exception cref="System.ArgumentNullException">obj</exception> + /// <exception cref="ArgumentNullException">obj</exception> public string SerializeToString(object obj) { if (obj == null) { - throw new ArgumentNullException("obj"); + throw new ArgumentNullException(nameof(obj)); } return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType()); diff --git a/Emby.Server.Implementations/Serialization/XmlSerializer.cs b/Emby.Server.Implementations/Serialization/XmlSerializer.cs index dfc324919..f7428eff7 100644 --- a/Emby.Server.Implementations/Serialization/XmlSerializer.cs +++ b/Emby.Server.Implementations/Serialization/XmlSerializer.cs @@ -1,11 +1,11 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Xml; using System.Xml.Serialization; using MediaBrowser.Model.IO; -using Microsoft.Extensions.Logging; using MediaBrowser.Model.Serialization; +using Microsoft.Extensions.Logging; namespace Emby.Server.Implementations.Serialization { @@ -17,10 +17,12 @@ namespace Emby.Server.Implementations.Serialization private readonly IFileSystem _fileSystem; private readonly ILogger _logger; - public MyXmlSerializer(IFileSystem fileSystem, ILogger logger) + public MyXmlSerializer( + IFileSystem fileSystem, + ILoggerFactory loggerFactory) { _fileSystem = fileSystem; - _logger = logger; + _logger = loggerFactory.CreateLogger("XmlSerializer"); } // Need to cache these @@ -33,8 +35,7 @@ namespace Emby.Server.Implementations.Serialization var key = type.FullName; lock (_serializers) { - XmlSerializer serializer; - if (!_serializers.TryGetValue(key, out serializer)) + if (!_serializers.TryGetValue(key, out var serializer)) { serializer = new XmlSerializer(type); _serializers[key] = serializer; |
