2012-07-15 22:27:07 +02:00
|
|
|
|
using System.IO;
|
2012-07-20 04:22:44 +02:00
|
|
|
|
using System;
|
2012-07-12 08:55:27 +02:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Json
|
|
|
|
|
{
|
|
|
|
|
public class JsonSerializer
|
|
|
|
|
{
|
2012-07-20 04:22:44 +02:00
|
|
|
|
public static void SerializeToStream<T>(T obj, Stream stream)
|
2012-07-12 08:55:27 +02:00
|
|
|
|
{
|
2012-07-15 22:27:07 +02:00
|
|
|
|
Configure();
|
|
|
|
|
|
2012-07-20 04:22:44 +02:00
|
|
|
|
ServiceStack.Text.JsonSerializer.SerializeToStream<T>(obj, stream);
|
2012-07-12 08:55:27 +02:00
|
|
|
|
}
|
2012-07-13 05:50:50 +02:00
|
|
|
|
|
2012-07-20 04:22:44 +02:00
|
|
|
|
public static void SerializeToFile<T>(T obj, string file)
|
2012-07-12 08:55:27 +02:00
|
|
|
|
{
|
2012-07-15 22:27:07 +02:00
|
|
|
|
Configure();
|
|
|
|
|
|
2012-07-12 08:55:27 +02:00
|
|
|
|
using (StreamWriter streamWriter = new StreamWriter(file))
|
|
|
|
|
{
|
2012-07-20 04:22:44 +02:00
|
|
|
|
ServiceStack.Text.JsonSerializer.SerializeToWriter<T>(obj, streamWriter);
|
2012-07-12 08:55:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-15 22:27:07 +02:00
|
|
|
|
public static T DeserializeFromFile<T>(string file)
|
2012-07-12 08:55:27 +02:00
|
|
|
|
{
|
2012-07-15 22:27:07 +02:00
|
|
|
|
Configure();
|
|
|
|
|
|
|
|
|
|
using (Stream stream = File.OpenRead(file))
|
2012-07-12 08:55:27 +02:00
|
|
|
|
{
|
2012-07-15 22:27:07 +02:00
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
|
2012-07-12 08:55:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-15 22:27:07 +02:00
|
|
|
|
|
|
|
|
|
public static T DeserializeFromStream<T>(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
Configure();
|
|
|
|
|
|
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromStream<T>(stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static T DeserializeFromString<T>(string data)
|
|
|
|
|
{
|
|
|
|
|
Configure();
|
|
|
|
|
|
|
|
|
|
return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(data);
|
|
|
|
|
}
|
2012-07-20 04:22:44 +02:00
|
|
|
|
|
2012-07-15 22:27:07 +02:00
|
|
|
|
private static void Configure()
|
|
|
|
|
{
|
|
|
|
|
ServiceStack.Text.JsConfig.ExcludeTypeInfo = true;
|
|
|
|
|
ServiceStack.Text.JsConfig.IncludeNullValues = false;
|
|
|
|
|
}
|
2012-07-12 08:55:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|