DataSerializer.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.IO;
  3. using Newtonsoft.Json;
  4. using ProtoBuf;
  5. namespace MediaBrowser.ApiInteraction
  6. {
  7. public static class DataSerializer
  8. {
  9. public static T DeserializeFromStream<T>(Stream stream, SerializationFormats format)
  10. {
  11. if (format == ApiInteraction.SerializationFormats.Protobuf)
  12. {
  13. return Serializer.Deserialize<T>(stream);
  14. }
  15. else if (format == ApiInteraction.SerializationFormats.Jsv)
  16. {
  17. throw new NotImplementedException();
  18. }
  19. else if (format == ApiInteraction.SerializationFormats.Json)
  20. {
  21. using (StreamReader streamReader = new StreamReader(stream))
  22. {
  23. using (JsonReader jsonReader = new JsonTextReader(streamReader))
  24. {
  25. return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize<T>(jsonReader);
  26. }
  27. }
  28. }
  29. throw new NotImplementedException();
  30. }
  31. public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
  32. {
  33. if (format == ApiInteraction.SerializationFormats.Protobuf)
  34. {
  35. throw new NotImplementedException();
  36. }
  37. else if (format == ApiInteraction.SerializationFormats.Jsv)
  38. {
  39. throw new NotImplementedException();
  40. }
  41. else if (format == ApiInteraction.SerializationFormats.Json)
  42. {
  43. using (StreamReader streamReader = new StreamReader(stream))
  44. {
  45. using (JsonReader jsonReader = new JsonTextReader(streamReader))
  46. {
  47. return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize(jsonReader, type);
  48. }
  49. }
  50. }
  51. throw new NotImplementedException();
  52. }
  53. public static void Configure()
  54. {
  55. }
  56. public static bool CanDeSerializeJsv
  57. {
  58. get
  59. {
  60. return false;
  61. }
  62. }
  63. }
  64. }