NewtonsoftJsonSerializer.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using MediaBrowser.Model.Serialization;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.IO;
  5. namespace MediaBrowser.ApiInteraction
  6. {
  7. /// <summary>
  8. /// Class NewtonsoftJsonSerializer
  9. /// </summary>
  10. public class NewtonsoftJsonSerializer : IJsonSerializer
  11. {
  12. /// <summary>
  13. /// Serializes to stream.
  14. /// </summary>
  15. /// <param name="obj">The obj.</param>
  16. /// <param name="stream">The stream.</param>
  17. /// <exception cref="System.NotImplementedException"></exception>
  18. /// <exception cref="System.ArgumentNullException">obj</exception>
  19. public void SerializeToStream(object obj, Stream stream)
  20. {
  21. throw new NotImplementedException();
  22. }
  23. /// <summary>
  24. /// Deserializes from stream.
  25. /// </summary>
  26. /// <param name="stream">The stream.</param>
  27. /// <param name="type">The type.</param>
  28. /// <returns>System.Object.</returns>
  29. public object DeserializeFromStream(Stream stream, Type type)
  30. {
  31. using (var streamReader = new StreamReader(stream))
  32. {
  33. using (var jsonReader = new JsonTextReader(streamReader))
  34. {
  35. return JsonSerializer.Create(new JsonSerializerSettings()).Deserialize(jsonReader, type);
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// Deserializes from stream.
  41. /// </summary>
  42. /// <typeparam name="T"></typeparam>
  43. /// <param name="stream">The stream.</param>
  44. /// <returns>``0.</returns>
  45. public T DeserializeFromStream<T>(Stream stream)
  46. {
  47. return (T)DeserializeFromStream(stream, typeof(T));
  48. }
  49. /// <summary>
  50. /// Deserializes from string.
  51. /// </summary>
  52. /// <typeparam name="T"></typeparam>
  53. /// <param name="text">The text.</param>
  54. /// <returns>``0.</returns>
  55. /// <exception cref="System.NotImplementedException"></exception>
  56. public T DeserializeFromString<T>(string text)
  57. {
  58. throw new NotImplementedException();
  59. }
  60. /// <summary>
  61. /// Deserializes from string.
  62. /// </summary>
  63. /// <param name="json">The json.</param>
  64. /// <param name="type">The type.</param>
  65. /// <returns>System.Object.</returns>
  66. /// <exception cref="System.NotImplementedException"></exception>
  67. public object DeserializeFromString(string json, Type type)
  68. {
  69. throw new NotImplementedException();
  70. }
  71. /// <summary>
  72. /// Serializes to string.
  73. /// </summary>
  74. /// <param name="obj">The obj.</param>
  75. /// <returns>System.String.</returns>
  76. public string SerializeToString(object obj)
  77. {
  78. using (var streamWriter = new StringWriter())
  79. {
  80. using (var jsonWriter = new JsonTextWriter((streamWriter)))
  81. {
  82. JsonSerializer.Create(new JsonSerializerSettings()).Serialize(jsonWriter, obj);
  83. }
  84. return streamWriter.ToString();
  85. }
  86. }
  87. /// <summary>
  88. /// Serializes to bytes.
  89. /// </summary>
  90. /// <param name="obj">The obj.</param>
  91. /// <returns>System.Byte[][].</returns>
  92. /// <exception cref="System.NotImplementedException"></exception>
  93. public byte[] SerializeToBytes(object obj)
  94. {
  95. throw new NotImplementedException();
  96. }
  97. /// <summary>
  98. /// Serializes to file.
  99. /// </summary>
  100. /// <param name="obj">The obj.</param>
  101. /// <param name="file">The file.</param>
  102. /// <exception cref="System.NotImplementedException"></exception>
  103. /// <exception cref="System.ArgumentNullException">obj</exception>
  104. public void SerializeToFile(object obj, string file)
  105. {
  106. throw new NotImplementedException();
  107. }
  108. /// <summary>
  109. /// Deserializes from file.
  110. /// </summary>
  111. /// <param name="type">The type.</param>
  112. /// <param name="file">The file.</param>
  113. /// <returns>System.Object.</returns>
  114. /// <exception cref="System.NotImplementedException"></exception>
  115. public object DeserializeFromFile(Type type, string file)
  116. {
  117. throw new NotImplementedException();
  118. }
  119. /// <summary>
  120. /// Deserializes from file.
  121. /// </summary>
  122. /// <typeparam name="T"></typeparam>
  123. /// <param name="file">The file.</param>
  124. /// <returns>``0.</returns>
  125. /// <exception cref="System.NotImplementedException"></exception>
  126. public T DeserializeFromFile<T>(string file) where T : class
  127. {
  128. throw new NotImplementedException();
  129. }
  130. }
  131. }