2012-07-29 11:19:25 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2012-07-12 02:55:27 -04:00
|
|
|
|
|
2012-07-31 12:29:07 -04:00
|
|
|
|
namespace MediaBrowser.Common.Serialization
|
2012-07-12 02:55:27 -04:00
|
|
|
|
{
|
2012-07-31 12:29:07 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides a wrapper around third party json serialization.
|
|
|
|
|
|
/// </summary>
|
2012-07-12 02:55:27 -04:00
|
|
|
|
public class JsonSerializer
|
|
|
|
|
|
{
|
2012-07-19 22:22:44 -04:00
|
|
|
|
public static void SerializeToStream<T>(T obj, Stream stream)
|
2012-07-12 02:55:27 -04:00
|
|
|
|
{
|
2012-07-31 12:29:07 -04:00
|
|
|
|
Configure();
|
|
|
|
|
|
|
2012-07-19 22:22:44 -04:00
|
|
|
|
ServiceStack.Text.JsonSerializer.SerializeToStream<T>(obj, stream);
|
2012-07-12 02:55:27 -04:00
|
|
|
|
}
|
2012-07-12 23:50:50 -04:00
|
|
|
|
|
2012-07-19 22:22:44 -04:00
|
|
|
|
public static void SerializeToFile<T>(T obj, string file)
|
2012-07-12 02:55:27 -04:00
|
|
|
|
{
|
2012-07-31 12:29:07 -04:00
|
|
|
|
Configure();
|
|
|
|
|
|
|
2012-08-22 13:01:05 -04:00
|
|
|
|
using (Stream stream = File.Open(file, FileMode.Create))
|
2012-07-12 02:55:27 -04:00
|
|
|
|
{
|
2012-08-22 13:01:05 -04:00
|
|
|
|
ServiceStack.Text.JsonSerializer.SerializeToStream<T>(obj, stream);
|
2012-07-12 02:55:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-29 11:19:25 -04:00
|
|
|
|
public static object DeserializeFromFile(Type type, string file)
|
|
|
|
|
|
{
|
2012-07-31 12:29:07 -04:00
|
|
|
|
Configure();
|
|
|
|
|
|
|
2012-07-29 11:19:25 -04:00
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-15 16:27:07 -04:00
|
|
|
|
public static T DeserializeFromFile<T>(string file)
|
2012-07-12 02:55:27 -04:00
|
|
|
|
{
|
2012-07-31 12:29:07 -04:00
|
|
|
|
Configure();
|
|
|
|
|
|
|
2012-07-15 16:27:07 -04:00
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
2012-07-12 02:55:27 -04:00
|
|
|
|
{
|
2012-07-15 16:27:07 -04:00
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
|
2012-07-12 02:55:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-07-15 16:27:07 -04:00
|
|
|
|
|
|
|
|
|
|
public static T DeserializeFromStream<T>(Stream stream)
|
|
|
|
|
|
{
|
2012-07-31 12:29:07 -04:00
|
|
|
|
Configure();
|
|
|
|
|
|
|
2012-07-15 16:27:07 -04:00
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-09-03 17:56:30 -04:00
|
|
|
|
public static object DeserializeFromStream(Stream stream, Type type)
|
|
|
|
|
|
{
|
|
|
|
|
|
Configure();
|
|
|
|
|
|
|
|
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-07-31 12:29:07 -04:00
|
|
|
|
private static bool IsConfigured = false;
|
|
|
|
|
|
private static void Configure()
|
2012-07-15 16:27:07 -04:00
|
|
|
|
{
|
2012-07-31 12:29:07 -04:00
|
|
|
|
if (!IsConfigured)
|
|
|
|
|
|
{
|
|
|
|
|
|
ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
|
|
|
|
|
|
ServiceStack.Text.JsConfig.IncludeNullValues = false;
|
|
|
|
|
|
|
|
|
|
|
|
IsConfigured = true;
|
|
|
|
|
|
}
|
2012-07-15 16:27:07 -04:00
|
|
|
|
}
|
2012-07-12 02:55:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|