コード例 #1
0
    public async Task WriteAsync(OutputFormatterWriteContext context)
    {
        var response = context.HttpContext.Response;

        var contentType = GetNonEmptyString(context.ContentType.Value, response.ContentType, JSONContentType);

        var encoding = MediaType.GetEncoding(contentType) ?? jsonFormatter.Encoding;

        if (JSONContentType.Equals(contentType, StringComparison.InvariantCultureIgnoreCase))
        {
            contentType = $"{JSONContentType};charset={encoding.HeaderName}";
        }

        response.ContentType = contentType;

        if (encoding == jsonFormatter.Encoding || encoding.Equals(jsonFormatter.Encoding))
        {
            var s = response.Body;

            await jsonFormatter.SerializeAsync(context.Object, s);

            await s.FlushAsync();
        }
        else
        {
            var tw = context.WriterFactory(response.Body, encoding);

            await jsonFormatter.SerializeAsync(context.Object, tw);

            await tw.FlushAsync();
        }
    }
コード例 #2
0
    /// <summary>
    /// 调用 Swifter.Json 快速序列化程序。
    /// </summary>
    /// <param name="context">控制器上下文</param>
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        var response = context.HttpContext.Response;

        var contentType = GetNonEmptyString(ContentType, response.ContentType, JSONContentType);
        var encoding    = ContentEncoding ?? response.ContentEncoding ?? Encoding.UTF8;

        if (JSONContentType.Equals(contentType, StringComparison.InvariantCultureIgnoreCase))
        {
            contentType = $"{JSONContentType};charset={encoding.HeaderName}";
        }

        response.ContentType = contentType;

        if (Data == null)
        {
            return;
        }

        JsonFormatter.SerializeObject(Data, response.OutputStream, encoding, Options);
    }
コード例 #3
0
    public async Task ExecuteAsync(ActionContext context, JsonResult result)
    {
        var response = context.HttpContext.Response;

        var jsonFormatter =

#if NETCOREAPP
            (result.SerializerSettings as JsonFormatter) ??
#endif
            DefaultJsonFormatter;

        var contentType = GetNonEmptyString(result.ContentType, response.ContentType, JSONContentType);
        var encoding    = MediaType.GetEncoding(contentType) ?? jsonFormatter.Encoding;

        if (JSONContentType.Equals(contentType, StringComparison.InvariantCultureIgnoreCase))
        {
            contentType = $"{JSONContentType};charset={encoding.HeaderName}";
        }

        response.ContentType = contentType;

        if (result.StatusCode != null)
        {
            response.StatusCode = result.StatusCode.Value;
        }

        if (encoding == jsonFormatter.Encoding || encoding.Equals(jsonFormatter.Encoding))
        {
            var s = response.Body;

            await jsonFormatter.SerializeAsync(result.Value, s);

            await s.FlushAsync();
        }
        else
        {
            var sw = new StreamWriter(response.Body, encoding);

            await jsonFormatter.SerializeAsync(result, sw);

            await sw.FlushAsync();
        }
    }