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

56 lines
1.9 KiB
C#
Raw Normal View History

2012-07-12 06:55:27 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace MediaBrowser.Common.Json
{
public class JsonSerializer
{
public static void Serialize<T>(T o, Stream stream)
{
using (StreamWriter streamWriter = new StreamWriter(stream))
{
using (Newtonsoft.Json.JsonTextWriter writer = new Newtonsoft.Json.JsonTextWriter(streamWriter))
{
var settings = new Newtonsoft.Json.JsonSerializerSettings()
{
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
};
Newtonsoft.Json.JsonSerializer.Create(settings).Serialize(writer, o);
}
}
}
2012-07-12 06:55:27 +00:00
public static void Serialize<T>(T o, string file)
{
using (StreamWriter streamWriter = new StreamWriter(file))
{
using (Newtonsoft.Json.JsonTextWriter writer = new Newtonsoft.Json.JsonTextWriter(streamWriter))
{
var settings = new Newtonsoft.Json.JsonSerializerSettings()
{
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
};
Newtonsoft.Json.JsonSerializer.Create(settings).Serialize(writer, o);
}
}
}
public static T Deserialize<T>(string file)
{
using (StreamReader streamReader = new StreamReader(file))
{
using (Newtonsoft.Json.JsonTextReader reader = new Newtonsoft.Json.JsonTextReader(streamReader))
{
return Newtonsoft.Json.JsonSerializer.Create(new Newtonsoft.Json.JsonSerializerSettings() { }).Deserialize<T>(reader);
}
}
}
}
}