瀏覽代碼

提交 JsonContent,預設application/json,使用System.Text.Json效能最佳化

CrazyIter 5 年之前
父節點
當前提交
e0a31ec9d1
共有 1 個文件被更改,包括 59 次插入0 次删除
  1. 59 0
      TEAMModelOS.SDK/Helper/Network/HttpHelper/JsonContent.cs

+ 59 - 0
TEAMModelOS.SDK/Helper/Network/HttpHelper/JsonContent.cs

@@ -0,0 +1,59 @@
+using System.Text;
+using System.Text.Json;
+
+namespace System.Net.Http
+{
+    /// <summary>
+    /// JsonContent,預設application/json,使用System.Text.Json效能最佳化
+    /// </summary>
+    public class JsonContent : StringContent
+    {
+        #region Fields
+
+        private const string JsonContentType = "application/json";
+
+        #endregion Fields
+
+        #region Constructors
+
+        /// <summary>
+        /// Initializes a new instance of the System.Net.Http.JsonContent class
+        /// with default settings.
+        /// </summary>
+        /// <param name="content">The content to used to initialize the instance.</param>
+        public JsonContent(object content) : base(JsonSerializer.Serialize(content), Encoding.UTF8, JsonContentType)
+        {
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the System.Net.Http.JsonContent class
+        /// with given serialization settings.
+        /// </summary>
+        /// <param name="content">The content to used to initialize the instance.</param>
+        /// <param name="settings">The settings to used to serialize the content.</param>
+        public JsonContent(object content, JsonSerializerOptions settings) : base(JsonSerializer.Serialize(content, settings), Encoding.UTF8, JsonContentType)
+        {
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the System.Net.Http.JsonContent class
+        /// with given json content and default settings.
+        /// </summary>
+        /// <param name="json">The json content to used to initialize the instance.</param>
+        public JsonContent(string json) : base(json, Encoding.UTF8, JsonContentType)
+        {
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the System.Net.Http.JsonContent class
+        /// with given parameters.
+        /// </summary>
+        /// <param name="json">The json content to used to initialize the instance.</param>
+        /// <param name="encoding">The encoding to used to encode the content.</param>
+        public JsonContent(string json, Encoding encoding) : base(json, encoding, JsonContentType)
+        {
+        }
+
+        #endregion Constructors
+    }
+}