You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Common/Serialization/JsonSerializer.cs

75 lines
2.2 KiB

using System;
using System.IO;
13 years ago
namespace MediaBrowser.Common.Serialization
13 years ago
{
/// <summary>
/// Provides a wrapper around third party json serialization.
/// </summary>
13 years ago
public class JsonSerializer
{
public static void SerializeToStream<T>(T obj, Stream stream)
13 years ago
{
Configure();
ServiceStack.Text.JsonSerializer.SerializeToStream(obj, stream);
13 years ago
}
public static void SerializeToFile<T>(T obj, string file)
13 years ago
{
Configure();
using (Stream stream = File.Open(file, FileMode.Create))
13 years ago
{
ServiceStack.Text.JsonSerializer.SerializeToStream(obj, stream);
13 years ago
}
}
public static object DeserializeFromFile(Type type, string file)
{
Configure();
using (Stream stream = File.OpenRead(file))
{
return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
}
}
public static T DeserializeFromFile<T>(string file)
13 years ago
{
Configure();
using (Stream stream = File.OpenRead(file))
13 years ago
{
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
13 years ago
}
}
public static T DeserializeFromStream<T>(Stream stream)
{
Configure();
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
}
public static object DeserializeFromStream(Stream stream, Type type)
{
Configure();
return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
}
private static bool _isConfigured;
private 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;
}
}
13 years ago
}
}