jellyfin/MediaBrowser.ApiInteraction.Metro/DataSerializer.cs

79 lines
2.8 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
using System;
2012-09-06 07:05:35 -07:00
using System.IO;
namespace MediaBrowser.ApiInteraction
{
public static class DataSerializer
{
/// <summary>
/// This is an auto-generated Protobuf Serialization assembly for best performance.
/// It is created during the Model project's post-build event.
/// This means that this class can currently only handle types within the Model project.
/// If we need to, we can always add a param indicating whether or not the model serializer should be used.
/// </summary>
private static ProtobufModelSerializer ProtobufModelSerializer = new ProtobufModelSerializer();
2012-09-06 07:05:35 -07:00
public static T DeserializeFromStream<T>(Stream stream, SerializationFormats format)
where T : class
2012-09-06 07:05:35 -07:00
{
if (format == ApiInteraction.SerializationFormats.Protobuf)
{
return ProtobufModelSerializer.Deserialize(stream, null, typeof(T)) as T;
2012-09-06 07:05:35 -07:00
}
2012-09-06 11:38:29 -07:00
else if (format == ApiInteraction.SerializationFormats.Jsv)
2012-09-06 07:05:35 -07:00
{
throw new NotImplementedException();
2012-09-06 07:05:35 -07:00
}
2012-09-06 11:38:29 -07:00
else if (format == ApiInteraction.SerializationFormats.Json)
{
2012-09-06 11:38:29 -07:00
using (StreamReader streamReader = new StreamReader(stream))
{
2012-09-06 11:38:29 -07:00
using (JsonReader jsonReader = new JsonTextReader(streamReader))
{
return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize<T>(jsonReader);
}
}
}
2012-09-06 11:38:29 -07:00
throw new NotImplementedException();
2012-09-06 07:05:35 -07:00
}
public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
{
if (format == ApiInteraction.SerializationFormats.Protobuf)
{
return ProtobufModelSerializer.Deserialize(stream, null, type);
2012-09-06 07:05:35 -07:00
}
2012-09-06 11:38:29 -07:00
else if (format == ApiInteraction.SerializationFormats.Jsv)
2012-09-06 07:05:35 -07:00
{
throw new NotImplementedException();
2012-09-06 07:05:35 -07:00
}
2012-09-06 11:38:29 -07:00
else if (format == ApiInteraction.SerializationFormats.Json)
{
2012-09-06 11:38:29 -07:00
using (StreamReader streamReader = new StreamReader(stream))
{
2012-09-06 11:38:29 -07:00
using (JsonReader jsonReader = new JsonTextReader(streamReader))
{
return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize(jsonReader, type);
}
}
}
2012-09-06 11:38:29 -07:00
throw new NotImplementedException();
2012-09-06 07:05:35 -07:00
}
public static void Configure()
{
}
public static bool CanDeSerializeJsv
{
get
{
return false;
}
}
}
}