using System; using System.IO; namespace MediaBrowser.Common.Serialization { /// /// Provides a wrapper around third party json serialization. /// public class JsonSerializer { /// /// Serializes to stream. /// /// /// The obj. /// The stream. /// obj public static void SerializeToStream(T obj, Stream stream) where T : class { if (obj == null) { throw new ArgumentNullException("obj"); } if (stream == null) { throw new ArgumentNullException("stream"); } Configure(); ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), stream); } /// /// Serializes to file. /// /// /// The obj. /// The file. /// obj public static void SerializeToFile(T obj, string file) where T : class { if (obj == null) { throw new ArgumentNullException("obj"); } if (string.IsNullOrEmpty(file)) { throw new ArgumentNullException("file"); } Configure(); using (Stream stream = File.Open(file, FileMode.Create)) { SerializeToStream(obj, stream); } } /// /// Deserializes from file. /// /// The type. /// The file. /// System.Object. /// type public static object DeserializeFromFile(Type type, string file) { if (type == null) { throw new ArgumentNullException("type"); } if (string.IsNullOrEmpty(file)) { throw new ArgumentNullException("file"); } Configure(); using (Stream stream = File.OpenRead(file)) { return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream); } } /// /// Deserializes from file. /// /// /// The file. /// ``0. /// file public static T DeserializeFromFile(string file) where T : class { if (string.IsNullOrEmpty(file)) { throw new ArgumentNullException("file"); } Configure(); using (Stream stream = File.OpenRead(file)) { return ServiceStack.Text.JsonSerializer.DeserializeFromStream(stream); } } /// /// Deserializes from stream. /// /// /// The stream. /// ``0. /// stream public static T DeserializeFromStream(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } Configure(); return ServiceStack.Text.JsonSerializer.DeserializeFromStream(stream); } /// /// Deserializes from string. /// /// /// The text. /// ``0. /// text public static T DeserializeFromString(string text) { if (string.IsNullOrEmpty(text)) { throw new ArgumentNullException("text"); } Configure(); return ServiceStack.Text.JsonSerializer.DeserializeFromString(text); } /// /// Deserializes from stream. /// /// The stream. /// The type. /// System.Object. /// stream public static object DeserializeFromStream(Stream stream, Type type) { if (stream == null) { throw new ArgumentNullException("stream"); } if (type == null) { throw new ArgumentNullException("type"); } Configure(); return ServiceStack.Text.JsonSerializer.DeserializeFromStream(type, stream); } /// /// The _is configured /// private static bool _isConfigured; /// /// Configures this instance. /// internal static void Configure() { if (!_isConfigured) { ServiceStack.Text.JsConfig.DateHandler = ServiceStack.Text.JsonDateHandler.ISO8601; ServiceStack.Text.JsConfig.ExcludeTypeInfo = true; ServiceStack.Text.JsConfig.IncludeNullValues = false; _isConfigured = true; } } /// /// Deserializes from string. /// /// The json. /// The type. /// System.Object. /// json public static object DeserializeFromString(string json, Type type) { if (string.IsNullOrEmpty(json)) { throw new ArgumentNullException("json"); } if (type == null) { throw new ArgumentNullException("type"); } Configure(); return ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type); } /// /// Serializes to string. /// /// /// The obj. /// System.String. /// obj public static string SerializeToString(T obj) where T : class { if (obj == null) { throw new ArgumentNullException("obj"); } Configure(); return ServiceStack.Text.JsonSerializer.SerializeToString(obj, obj.GetType()); } /// /// Serializes to bytes. /// /// /// The obj. /// System.Byte[][]. /// obj public static byte[] SerializeToBytes(T obj) where T : class { if (obj == null) { throw new ArgumentNullException("obj"); } using (var stream = new MemoryStream()) { SerializeToStream(obj, stream); return stream.ToArray(); } } } }