2019-01-06 13:50:43 -07:00
|
|
|
using System;
|
2013-02-20 18:33:05 -07:00
|
|
|
using System.IO;
|
2019-01-13 12:22:24 -07:00
|
|
|
using System.Threading.Tasks;
|
2016-10-25 12:02:04 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
2016-10-28 21:10:11 -07:00
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2019-02-09 02:10:33 -07:00
|
|
|
namespace Emby.Server.Implementations.Serialization
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Provides a wrapper around third party json serialization.
|
|
|
|
/// </summary>
|
2013-02-24 14:53:54 -07:00
|
|
|
public class JsonSerializer : IJsonSerializer
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2015-09-13 16:07:54 -07:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2016-06-29 11:48:26 -07:00
|
|
|
|
2019-01-17 15:55:05 -07:00
|
|
|
public JsonSerializer(
|
|
|
|
IFileSystem fileSystem)
|
2013-02-24 14:53:54 -07:00
|
|
|
{
|
2015-09-13 16:07:54 -07:00
|
|
|
_fileSystem = fileSystem;
|
2013-02-24 14:53:54 -07:00
|
|
|
Configure();
|
|
|
|
}
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Serializes to stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">The obj.</param>
|
|
|
|
/// <param name="stream">The stream.</param>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">obj</exception>
|
2013-02-25 20:43:04 -07:00
|
|
|
public void SerializeToStream(object obj, Stream stream)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
if (obj == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(obj));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stream == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(stream));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), stream);
|
|
|
|
}
|
|
|
|
|
2019-02-09 03:53:07 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Serializes to stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">The obj.</param>
|
|
|
|
/// <param name="stream">The stream.</param>
|
|
|
|
/// <exception cref="ArgumentNullException">obj</exception>
|
|
|
|
public void SerializeToStream<T>(T obj, Stream stream)
|
|
|
|
{
|
|
|
|
if (obj == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(stream));
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceStack.Text.JsonSerializer.SerializeToStream<T>(obj, stream);
|
|
|
|
}
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Serializes to file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">The obj.</param>
|
|
|
|
/// <param name="file">The file.</param>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">obj</exception>
|
2013-02-25 20:43:04 -07:00
|
|
|
public void SerializeToFile(object obj, string file)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
if (obj == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(obj));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(file))
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(file));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:37:13 -07:00
|
|
|
using (var stream = _fileSystem.GetFileStream(file, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
SerializeToStream(obj, stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static Stream OpenFile(string path)
|
2015-03-07 10:19:44 -07:00
|
|
|
{
|
2015-08-01 14:17:46 -07:00
|
|
|
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 131072);
|
2015-03-07 10:19:44 -07:00
|
|
|
}
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Deserializes from file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">type</exception>
|
2013-02-24 14:53:54 -07:00
|
|
|
public object DeserializeFromFile(Type type, string file)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
if (type == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(type));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(file))
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(file));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:37:13 -07:00
|
|
|
using (var stream = OpenFile(file))
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2013-12-04 07:52:38 -07:00
|
|
|
return DeserializeFromStream(stream, type);
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deserializes from file.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
/// <returns>``0.</returns>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">file</exception>
|
2013-02-24 14:53:54 -07:00
|
|
|
public T DeserializeFromFile<T>(string file)
|
2013-02-20 18:33:05 -07:00
|
|
|
where T : class
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(file))
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(file));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:37:13 -07:00
|
|
|
using (var stream = OpenFile(file))
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2013-12-04 07:52:38 -07:00
|
|
|
return DeserializeFromStream<T>(stream);
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deserializes from stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <param name="stream">The stream.</param>
|
|
|
|
/// <returns>``0.</returns>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">stream</exception>
|
2013-02-24 14:53:54 -07:00
|
|
|
public T DeserializeFromStream<T>(Stream stream)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
if (stream == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(stream));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
|
|
|
|
}
|
|
|
|
|
2018-12-30 16:28:23 -07:00
|
|
|
public Task<T> DeserializeFromStreamAsync<T>(Stream stream)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
|
|
|
if (stream == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(stream));
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-30 16:28:23 -07:00
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromStreamAsync<T>(stream);
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Deserializes from string.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <param name="text">The text.</param>
|
|
|
|
/// <returns>``0.</returns>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">text</exception>
|
2013-02-24 14:53:54 -07:00
|
|
|
public T DeserializeFromString<T>(string text)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(text))
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(text));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">stream</exception>
|
2013-02-24 14:53:54 -07:00
|
|
|
public object DeserializeFromStream(Stream stream, Type type)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
if (stream == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(stream));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(type));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public async Task<object> DeserializeFromStreamAsync(Stream stream, Type type)
|
|
|
|
{
|
|
|
|
if (stream == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(stream));
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(type));
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
using (var reader = new StreamReader(stream))
|
|
|
|
{
|
|
|
|
var json = await reader.ReadToEndAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Configures this instance.
|
|
|
|
/// </summary>
|
2013-02-24 14:53:54 -07:00
|
|
|
private void Configure()
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2013-12-07 08:52:38 -07:00
|
|
|
ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.DateHandler.ISO8601;
|
2013-02-24 14:53:54 -07:00
|
|
|
ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
|
|
|
|
ServiceStack.Text.JsConfig.IncludeNullValues = false;
|
2013-11-30 11:32:39 -07:00
|
|
|
ServiceStack.Text.JsConfig.AlwaysUseUtc = true;
|
|
|
|
ServiceStack.Text.JsConfig.AssumeUtc = true;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
|
|
|
ServiceStack.Text.JsConfig<Guid>.SerializeFn = SerializeGuid;
|
|
|
|
}
|
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
private static string SerializeGuid(Guid guid)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
|
|
|
if (guid.Equals(Guid.Empty))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return guid.ToString("N");
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deserializes from string.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="json">The json.</param>
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">json</exception>
|
2013-02-24 14:53:54 -07:00
|
|
|
public object DeserializeFromString(string json, Type type)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(json))
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(json));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(type));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Serializes to string.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">The obj.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">obj</exception>
|
2013-02-25 20:43:04 -07:00
|
|
|
public string SerializeToString(object obj)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
if (obj == null)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(obj));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|