DataSerializer.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Newtonsoft.Json;
  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 ProtobufModelSerializer ProtobufModelSerializer = new ProtobufModelSerializer();
  15. public static T DeserializeFromStream<T>(Stream stream, SerializationFormats format)
  16. where T : class
  17. {
  18. if (format == ApiInteraction.SerializationFormats.Protobuf)
  19. {
  20. //return Serializer.Deserialize<T>(stream);
  21. return ProtobufModelSerializer.Deserialize(stream, null, typeof(T)) as T;
  22. }
  23. else if (format == ApiInteraction.SerializationFormats.Jsv)
  24. {
  25. throw new NotImplementedException();
  26. }
  27. else if (format == ApiInteraction.SerializationFormats.Json)
  28. {
  29. using (StreamReader streamReader = new StreamReader(stream))
  30. {
  31. using (JsonReader jsonReader = new JsonTextReader(streamReader))
  32. {
  33. return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize<T>(jsonReader);
  34. }
  35. }
  36. }
  37. throw new NotImplementedException();
  38. }
  39. public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
  40. {
  41. if (format == ApiInteraction.SerializationFormats.Protobuf)
  42. {
  43. //throw new NotImplementedException();
  44. return ProtobufModelSerializer.Deserialize(stream, null, type);
  45. }
  46. else if (format == ApiInteraction.SerializationFormats.Jsv)
  47. {
  48. throw new NotImplementedException();
  49. }
  50. else if (format == ApiInteraction.SerializationFormats.Json)
  51. {
  52. using (StreamReader streamReader = new StreamReader(stream))
  53. {
  54. using (JsonReader jsonReader = new JsonTextReader(streamReader))
  55. {
  56. return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize(jsonReader, type);
  57. }
  58. }
  59. }
  60. throw new NotImplementedException();
  61. }
  62. public static void Configure()
  63. {
  64. }
  65. public static bool CanDeSerializeJsv
  66. {
  67. get
  68. {
  69. return false;
  70. }
  71. }
  72. }
  73. }