DataSerializer.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using ServiceStack.Text;
  2. using System;
  3. using System.IO;
  4. namespace MediaBrowser.ApiInteraction
  5. {
  6. public static class DataSerializer
  7. {
  8. /// <summary>
  9. /// This is an auto-generated Protobuf Serialization assembly for best performance.
  10. /// It is created during the Model project's post-build event.
  11. /// This means that this class can currently only handle types within the Model project.
  12. /// If we need to, we can always add a param indicating whether or not the model serializer should be used.
  13. /// </summary>
  14. private static readonly ProtobufModelSerializer ProtobufModelSerializer = new ProtobufModelSerializer();
  15. /// <summary>
  16. /// Deserializes an object using generics
  17. /// </summary>
  18. public static T DeserializeFromStream<T>(Stream stream, SerializationFormats format)
  19. where T : class
  20. {
  21. if (format == SerializationFormats.Protobuf)
  22. {
  23. //return Serializer.Deserialize<T>(stream);
  24. return ProtobufModelSerializer.Deserialize(stream, null, typeof(T)) as T;
  25. }
  26. if (format == SerializationFormats.Jsv)
  27. {
  28. return TypeSerializer.DeserializeFromStream<T>(stream);
  29. }
  30. if (format == SerializationFormats.Json)
  31. {
  32. return JsonSerializer.DeserializeFromStream<T>(stream);
  33. }
  34. throw new NotImplementedException();
  35. }
  36. /// <summary>
  37. /// Deserializes an object
  38. /// </summary>
  39. public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
  40. {
  41. if (format == SerializationFormats.Protobuf)
  42. {
  43. //throw new NotImplementedException();
  44. return ProtobufModelSerializer.Deserialize(stream, null, type);
  45. }
  46. if (format == SerializationFormats.Jsv)
  47. {
  48. return TypeSerializer.DeserializeFromStream(type, stream);
  49. }
  50. if (format == SerializationFormats.Json)
  51. {
  52. return JsonSerializer.DeserializeFromStream(type, stream);
  53. }
  54. throw new NotImplementedException();
  55. }
  56. public static void Configure()
  57. {
  58. JsConfig.DateHandler = JsonDateHandler.ISO8601;
  59. JsConfig.ExcludeTypeInfo = true;
  60. JsConfig.IncludeNullValues = false;
  61. }
  62. public static bool CanDeSerializeJsv
  63. {
  64. get
  65. {
  66. return true;
  67. }
  68. }
  69. }
  70. }