DataSerializer.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. if (format == ApiInteraction.SerializationFormats.Jsv)
  16. {
  17. throw new NotImplementedException();
  18. }
  19. using (StreamReader streamReader = new StreamReader(stream))
  20. {
  21. using (JsonReader jsonReader = new JsonTextReader(streamReader))
  22. {
  23. return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize<T>(jsonReader);
  24. }
  25. }
  26. }
  27. public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
  28. {
  29. if (format == ApiInteraction.SerializationFormats.Protobuf)
  30. {
  31. throw new NotImplementedException();
  32. }
  33. if (format == ApiInteraction.SerializationFormats.Jsv)
  34. {
  35. throw new NotImplementedException();
  36. }
  37. using (StreamReader streamReader = new StreamReader(stream))
  38. {
  39. using (JsonReader jsonReader = new JsonTextReader(streamReader))
  40. {
  41. return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize(jsonReader, type);
  42. }
  43. }
  44. }
  45. public static void Configure()
  46. {
  47. }
  48. public static bool CanDeSerializeJsv
  49. {
  50. get
  51. {
  52. return false;
  53. }
  54. }
  55. }
  56. }