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
    }
}
