using System;
using System.IO;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
using MediaBrowser.Model.Serialization;
using System.Threading.Tasks;
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.LogDebug("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);
}
public async Task DeserializeFromStreamAsync(Stream stream)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
using (var reader = new StreamReader(stream))
{
var json = await reader.ReadToEndAsync().ConfigureAwait(false);
return ServiceStack.Text.JsonSerializer.DeserializeFromString(json);
}
}
///
/// 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);
}
public async Task