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