jellyfin-server/MediaBrowser.Common/Serialization/JsonSerializer.cs

75 lines
2.1 KiB
C#
Raw Normal View History

using System;
using System.IO;
2012-07-12 06:55:27 +00:00
namespace MediaBrowser.Common.Serialization
2012-07-12 06:55:27 +00:00
{
/// <summary>
/// Provides a wrapper around third party json serialization.
/// </summary>
2012-07-12 06:55:27 +00:00
public class JsonSerializer
{
public static void SerializeToStream<T>(T obj, Stream stream)
2012-07-12 06:55:27 +00:00
{
Configure();
ServiceStack.Text.JsonSerializer.SerializeToStream<T>(obj, stream);
2012-07-12 06:55:27 +00:00
}
public static void SerializeToFile<T>(T obj, string file)
2012-07-12 06:55:27 +00:00
{
Configure();
2012-08-22 17:01:05 +00:00
using (Stream stream = File.Open(file, FileMode.Create))
2012-07-12 06:55:27 +00:00
{
2012-08-22 17:01:05 +00:00
ServiceStack.Text.JsonSerializer.SerializeToStream<T>(obj, stream);
2012-07-12 06:55:27 +00:00
}
}
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)
2012-07-12 06:55:27 +00:00
{
Configure();
using (Stream stream = File.OpenRead(file))
2012-07-12 06:55:27 +00:00
{
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
2012-07-12 06:55:27 +00:00
}
}
public static T DeserializeFromStream<T>(Stream stream)
{
Configure();
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
}
2012-09-03 21:56:30 +00:00
public static object DeserializeFromStream(Stream stream, Type type)
{
Configure();
return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
}
private static bool IsConfigured = false;
private static void Configure()
{
if (!IsConfigured)
{
ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
ServiceStack.Text.JsConfig.IncludeNullValues = false;
IsConfigured = true;
}
}
2012-07-12 06:55:27 +00:00
}
}