using ProtoBuf; using ProtoBuf.Meta; using ServiceStack.Text; 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) { return JsonSerializer.DeserializeFromStream(type, stream); } throw new NotImplementedException(); } /// /// Serializes to json. /// /// The obj. /// System.String. public static string SerializeToJsonString(object obj) { return JsonSerializer.SerializeToString(obj, obj.GetType()); } /// /// Configures this instance. /// public static void Configure() { JsConfig.DateHandler = JsonDateHandler.ISO8601; JsConfig.ExcludeTypeInfo = true; JsConfig.IncludeNullValues = false; } } }