aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations/Serialization
diff options
context:
space:
mode:
Diffstat (limited to 'Emby.Common.Implementations/Serialization')
-rw-r--r--Emby.Common.Implementations/Serialization/JsonSerializer.cs227
-rw-r--r--Emby.Common.Implementations/Serialization/XmlSerializer.cs130
2 files changed, 0 insertions, 357 deletions
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
-{
- /// <summary>
- /// Provides a wrapper around third party json serialization.
- /// </summary>
- public class JsonSerializer : IJsonSerializer
- {
- private readonly IFileSystem _fileSystem;
- private readonly ILogger _logger;
-
- public JsonSerializer(IFileSystem fileSystem, ILogger logger)
- {
- _fileSystem = fileSystem;
- _logger = logger;
- Configure();
- }
-
- /// <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");
- }
-
- ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), stream);
- }
-
- /// <summary>
- /// Serializes to file.
- /// </summary>
- /// <param name="obj">The obj.</param>
- /// <param name="file">The file.</param>
- /// <exception cref="System.ArgumentNullException">obj</exception>
- 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);
- }
-
- /// <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 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);
- }
- }
-
- /// <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 = OpenFile(file))
- {
- return 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 T DeserializeFromStream<T>(Stream stream)
- {
- if (stream == null)
- {
- throw new ArgumentNullException("stream");
- }
-
- 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 T DeserializeFromString<T>(string text)
- {
- if (string.IsNullOrEmpty(text))
- {
- throw new ArgumentNullException("text");
- }
-
- 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 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);
- }
-
- /// <summary>
- /// Configures this instance.
- /// </summary>
- 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;
- }
-
- /// <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 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);
- }
-
- /// <summary>
- /// Serializes to string.
- /// </summary>
- /// <param name="obj">The obj.</param>
- /// <returns>System.String.</returns>
- /// <exception cref="System.ArgumentNullException">obj</exception>
- 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
-{
- /// <summary>
- /// Provides a wrapper around third party xml serialization.
- /// </summary>
- 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<string, XmlSerializer> _serializers =
- new Dictionary<string, XmlSerializer>();
-
- 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;
- }
- }
-
- /// <summary>
- /// Serializes to writer.
- /// </summary>
- /// <param name="obj">The obj.</param>
- /// <param name="writer">The writer.</param>
- private void SerializeToWriter(object obj, XmlWriter writer)
- {
- var netSerializer = GetSerializer(obj.GetType());
- netSerializer.Serialize(writer, obj);
- }
-
- /// <summary>
- /// Deserializes from stream.
- /// </summary>
- /// <param name="type">The type.</param>
- /// <param name="stream">The stream.</param>
- /// <returns>System.Object.</returns>
- public object DeserializeFromStream(Type type, Stream stream)
- {
- using (var reader = XmlReader.Create(stream))
- {
- var netSerializer = GetSerializer(type);
- return netSerializer.Deserialize(reader);
- }
- }
-
- /// <summary>
- /// Serializes to stream.
- /// </summary>
- /// <param name="obj">The obj.</param>
- /// <param name="stream">The stream.</param>
- public void SerializeToStream(object obj, Stream stream)
- {
- using (var writer = new XmlTextWriter(stream, null))
- {
- writer.Formatting = Formatting.Indented;
- SerializeToWriter(obj, writer);
- }
- }
-
- /// <summary>
- /// Serializes to file.
- /// </summary>
- /// <param name="obj">The obj.</param>
- /// <param name="file">The file.</param>
- 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);
- }
- }
-
- /// <summary>
- /// Deserializes from file.
- /// </summary>
- /// <param name="type">The type.</param>
- /// <param name="file">The file.</param>
- /// <returns>System.Object.</returns>
- public object DeserializeFromFile(Type type, string file)
- {
- _logger.Debug("Deserializing file {0}", file);
- using (var stream = _fileSystem.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 object DeserializeFromBytes(Type type, byte[] buffer)
- {
- using (var stream = new MemoryStream(buffer))
- {
- return DeserializeFromStream(type, stream);
- }
- }
- }
-}