JsonContent.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Text;
  2. using System.Text.Json;
  3. namespace System.Net.Http
  4. {
  5. /// <summary>
  6. /// JsonContent,預設application/json,使用System.Text.Json效能最佳化
  7. /// </summary>
  8. public class JsonContent : StringContent
  9. {
  10. #region Fields
  11. private const string JsonContentType = "application/json";
  12. #endregion Fields
  13. #region Constructors
  14. /// <summary>
  15. /// Initializes a new instance of the System.Net.Http.JsonContent class
  16. /// with default settings.
  17. /// </summary>
  18. /// <param name="content">The content to used to initialize the instance.</param>
  19. public JsonContent(object content) : base(JsonSerializer.Serialize(content), Encoding.UTF8, JsonContentType)
  20. {
  21. }
  22. /// <summary>
  23. /// Initializes a new instance of the System.Net.Http.JsonContent class
  24. /// with given serialization settings.
  25. /// </summary>
  26. /// <param name="content">The content to used to initialize the instance.</param>
  27. /// <param name="settings">The settings to used to serialize the content.</param>
  28. public JsonContent(object content, JsonSerializerOptions settings) : base(JsonSerializer.Serialize(content, settings), Encoding.UTF8, JsonContentType)
  29. {
  30. }
  31. /// <summary>
  32. /// Initializes a new instance of the System.Net.Http.JsonContent class
  33. /// with given json content and default settings.
  34. /// </summary>
  35. /// <param name="json">The json content to used to initialize the instance.</param>
  36. public JsonContent(string json) : base(json, Encoding.UTF8, JsonContentType)
  37. {
  38. }
  39. /// <summary>
  40. /// Initializes a new instance of the System.Net.Http.JsonContent class
  41. /// with given parameters.
  42. /// </summary>
  43. /// <param name="json">The json content to used to initialize the instance.</param>
  44. /// <param name="encoding">The encoding to used to encode the content.</param>
  45. public JsonContent(string json, Encoding encoding) : base(json, encoding, JsonContentType)
  46. {
  47. }
  48. #endregion Constructors
  49. }
  50. }