jellyfin-server/MediaBrowser.ApiInteraction/DataSerializer.cs

78 lines
2.7 KiB
C#
Raw Normal View History

using ServiceStack.Text;
using System;
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>
2012-09-11 18:20:12 +00:00
private static readonly ProtobufModelSerializer ProtobufModelSerializer = new ProtobufModelSerializer();
/// <summary>
/// Deserializes an object using generics
/// </summary>
2012-09-06 14:00:44 +00:00
public static T DeserializeFromStream<T>(Stream stream, SerializationFormats format)
where T : class
{
2012-09-11 18:20:12 +00:00
if (format == SerializationFormats.Protobuf)
2012-09-06 14:00:44 +00:00
{
//return Serializer.Deserialize<T>(stream);
return ProtobufModelSerializer.Deserialize(stream, null, typeof(T)) as T;
2012-09-06 14:00:44 +00:00
}
2012-09-11 19:37:14 +00:00
if (format == SerializationFormats.Jsv)
2012-09-06 14:00:44 +00:00
{
return TypeSerializer.DeserializeFromStream<T>(stream);
}
2012-09-11 19:37:14 +00:00
if (format == SerializationFormats.Json)
2012-09-06 18:38:29 +00:00
{
return JsonSerializer.DeserializeFromStream<T>(stream);
}
2012-09-06 18:38:29 +00:00
throw new NotImplementedException();
}
/// <summary>
/// Deserializes an object
/// </summary>
2012-09-06 14:00:44 +00:00
public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
{
2012-09-11 19:37:14 +00:00
if (format == SerializationFormats.Protobuf)
2012-09-06 14:00:44 +00:00
{
//throw new NotImplementedException();
return ProtobufModelSerializer.Deserialize(stream, null, type);
2012-09-06 14:00:44 +00:00
}
2012-09-11 19:37:14 +00:00
if (format == SerializationFormats.Jsv)
2012-09-06 14:00:44 +00:00
{
return TypeSerializer.DeserializeFromStream(type, stream);
}
2012-09-11 19:37:14 +00:00
if (format == SerializationFormats.Json)
2012-09-06 18:38:29 +00:00
{
return JsonSerializer.DeserializeFromStream(type, stream);
}
2012-09-06 18:38:29 +00:00
throw new NotImplementedException();
}
public static void Configure()
{
2012-09-11 19:37:14 +00:00
JsConfig.DateHandler = JsonDateHandler.ISO8601;
JsConfig.ExcludeTypeInfo = true;
JsConfig.IncludeNullValues = false;
}
2012-09-06 14:00:44 +00:00
public static bool CanDeSerializeJsv
{
get
{
return true;
}
}
}
}