using Newtonsoft.Json;
using ProtoBuf;
using ProtoBuf.Meta;
using System;
using System.IO;
namespace MediaBrowser.ApiInteraction
{
///
/// Class DataSerializer
///
public static class DataSerializer
{
///
/// Gets or sets the dynamically created serializer.
///
/// The dynamic serializer.
public static TypeModel DynamicSerializer { get; set; }
///
/// Deserializes an object
///
/// The stream.
/// The format.
/// The type.
/// System.Object.
///
public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
{
if (format == SerializationFormats.Protobuf)
{
if (DynamicSerializer != null)
{
return DynamicSerializer.Deserialize(stream, null, type);
}
return Serializer.NonGeneric.Deserialize(type, stream);
}
if (format == SerializationFormats.Json)
{
using (var streamReader = new StreamReader(stream))
{
using (var jsonReader = new JsonTextReader(streamReader))
{
return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize(jsonReader, type);
}
}
}
throw new NotImplementedException();
}
///
/// Serializes to json.
///
/// The obj.
/// System.String.
public static string SerializeToJsonString(object obj)
{
using (var streamWriter = new StringWriter())
{
using (var jsonWriter = new JsonTextWriter((streamWriter)))
{
JsonSerializer.Create(new JsonSerializerSettings()).Serialize(jsonWriter, obj);
}
return streamWriter.ToString();
}
}
///
/// Configures this instance.
///
public static void Configure()
{
}
}
}