2013-02-24 16:53:54 -05:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using System;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
2013-02-24 16:53:54 -05:00
|
|
|
|
namespace MediaBrowser.Common.Implementations.Serialization
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides a wrapper around third party json serialization.
|
|
|
|
|
/// </summary>
|
2013-02-24 16:53:54 -05:00
|
|
|
|
public class JsonSerializer : IJsonSerializer
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2013-02-24 16:53:54 -05:00
|
|
|
|
public JsonSerializer()
|
|
|
|
|
{
|
|
|
|
|
Configure();
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializes to stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="obj">The obj.</param>
|
|
|
|
|
/// <param name="stream">The stream.</param>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">obj</exception>
|
2013-02-25 22:43:04 -05:00
|
|
|
|
public void SerializeToStream(object obj, Stream stream)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
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>
|
2013-02-25 22:43:04 -05:00
|
|
|
|
public void SerializeToFile(object obj, string file)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("obj");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(file))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("file");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (Stream stream = File.Open(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>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">type</exception>
|
2013-02-24 16:53:54 -05:00
|
|
|
|
public object DeserializeFromFile(Type type, string file)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
if (type == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("type");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(file))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("file");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
|
|
|
|
{
|
2013-12-04 09:52:38 -05:00
|
|
|
|
return DeserializeFromStream(stream, type);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deserializes from file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
|
/// <returns>``0.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">file</exception>
|
2013-02-24 16:53:54 -05:00
|
|
|
|
public T DeserializeFromFile<T>(string file)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(file))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("file");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
|
|
|
|
{
|
2013-12-04 09:52:38 -05:00
|
|
|
|
return DeserializeFromStream<T>(stream);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deserializes from stream.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="stream">The stream.</param>
|
|
|
|
|
/// <returns>``0.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">stream</exception>
|
2013-02-24 16:53:54 -05:00
|
|
|
|
public T DeserializeFromStream<T>(Stream stream)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
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>
|
2013-02-24 16:53:54 -05:00
|
|
|
|
public T DeserializeFromString<T>(string text)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
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>
|
2013-02-24 16:53:54 -05:00
|
|
|
|
public object DeserializeFromStream(Stream stream, Type type)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
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>
|
2013-02-24 16:53:54 -05:00
|
|
|
|
private void Configure()
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2013-02-24 16:53:54 -05:00
|
|
|
|
ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.JsonDateHandler.ISO8601;
|
|
|
|
|
ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
|
|
|
|
|
ServiceStack.Text.JsConfig.IncludeNullValues = false;
|
2013-11-30 13:32:39 -05:00
|
|
|
|
ServiceStack.Text.JsConfig.AlwaysUseUtc = true;
|
|
|
|
|
ServiceStack.Text.JsConfig.AssumeUtc = true;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2013-02-24 16:53:54 -05:00
|
|
|
|
public object DeserializeFromString(string json, Type type)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
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>
|
2013-02-25 22:43:04 -05:00
|
|
|
|
public string SerializeToString(object obj)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("obj");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializes to bytes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="obj">The obj.</param>
|
|
|
|
|
/// <returns>System.Byte[][].</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">obj</exception>
|
2013-02-25 22:43:04 -05:00
|
|
|
|
public byte[] SerializeToBytes(object obj)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("obj");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using (var stream = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
SerializeToStream(obj, stream);
|
|
|
|
|
return stream.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|