mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-07 11:57:55 +02:00
259 lines
7.9 KiB
C#
259 lines
7.9 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace MediaBrowser.Common.Serialization
|
|
{
|
|
/// <summary>
|
|
/// Provides a wrapper around third party json serialization.
|
|
/// </summary>
|
|
public class JsonSerializer
|
|
{
|
|
/// <summary>
|
|
/// Serializes to stream.
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="obj">The obj.</param>
|
|
/// <param name="stream">The stream.</param>
|
|
/// <exception cref="System.ArgumentNullException">obj</exception>
|
|
public static void SerializeToStream<T>(T obj, Stream stream)
|
|
where T : class
|
|
{
|
|
if (obj == null)
|
|
{
|
|
throw new ArgumentNullException("obj");
|
|
}
|
|
|
|
if (stream == null)
|
|
{
|
|
throw new ArgumentNullException("stream");
|
|
}
|
|
|
|
Configure();
|
|
|
|
ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), stream);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serializes to file.
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="obj">The obj.</param>
|
|
/// <param name="file">The file.</param>
|
|
/// <exception cref="System.ArgumentNullException">obj</exception>
|
|
public static void SerializeToFile<T>(T obj, string file)
|
|
where T : class
|
|
{
|
|
if (obj == null)
|
|
{
|
|
throw new ArgumentNullException("obj");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(file))
|
|
{
|
|
throw new ArgumentNullException("file");
|
|
}
|
|
|
|
Configure();
|
|
|
|
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>
|
|
public static object DeserializeFromFile(Type type, string file)
|
|
{
|
|
if (type == null)
|
|
{
|
|
throw new ArgumentNullException("type");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(file))
|
|
{
|
|
throw new ArgumentNullException("file");
|
|
}
|
|
|
|
Configure();
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
|
{
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
|
|
}
|
|
}
|
|
|
|
/// <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 static T DeserializeFromFile<T>(string file)
|
|
where T : class
|
|
{
|
|
if (string.IsNullOrEmpty(file))
|
|
{
|
|
throw new ArgumentNullException("file");
|
|
}
|
|
|
|
Configure();
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
|
{
|
|
return ServiceStack.Text.JsonSerializer.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 static T DeserializeFromStream<T>(Stream stream)
|
|
{
|
|
if (stream == null)
|
|
{
|
|
throw new ArgumentNullException("stream");
|
|
}
|
|
|
|
Configure();
|
|
|
|
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 static T DeserializeFromString<T>(string text)
|
|
{
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
throw new ArgumentNullException("text");
|
|
}
|
|
|
|
Configure();
|
|
|
|
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 static object DeserializeFromStream(Stream stream, Type type)
|
|
{
|
|
if (stream == null)
|
|
{
|
|
throw new ArgumentNullException("stream");
|
|
}
|
|
|
|
if (type == null)
|
|
{
|
|
throw new ArgumentNullException("type");
|
|
}
|
|
|
|
Configure();
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The _is configured
|
|
/// </summary>
|
|
private static bool _isConfigured;
|
|
/// <summary>
|
|
/// Configures this instance.
|
|
/// </summary>
|
|
internal static void Configure()
|
|
{
|
|
if (!_isConfigured)
|
|
{
|
|
ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.JsonDateHandler.ISO8601;
|
|
ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
|
|
ServiceStack.Text.JsConfig.IncludeNullValues = false;
|
|
_isConfigured = 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 static object DeserializeFromString(string json, Type type)
|
|
{
|
|
if (string.IsNullOrEmpty(json))
|
|
{
|
|
throw new ArgumentNullException("json");
|
|
}
|
|
|
|
if (type == null)
|
|
{
|
|
throw new ArgumentNullException("type");
|
|
}
|
|
|
|
Configure();
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serializes to string.
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="obj">The obj.</param>
|
|
/// <returns>System.String.</returns>
|
|
/// <exception cref="System.ArgumentNullException">obj</exception>
|
|
public static string SerializeToString<T>(T obj)
|
|
where T : class
|
|
{
|
|
if (obj == null)
|
|
{
|
|
throw new ArgumentNullException("obj");
|
|
}
|
|
|
|
Configure();
|
|
return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serializes to bytes.
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="obj">The obj.</param>
|
|
/// <returns>System.Byte[][].</returns>
|
|
/// <exception cref="System.ArgumentNullException">obj</exception>
|
|
public static byte[] SerializeToBytes<T>(T obj)
|
|
where T : class
|
|
{
|
|
if (obj == null)
|
|
{
|
|
throw new ArgumentNullException("obj");
|
|
}
|
|
|
|
using (var stream = new MemoryStream())
|
|
{
|
|
SerializeToStream(obj, stream);
|
|
return stream.ToArray();
|
|
}
|
|
}
|
|
}
|
|
}
|