public IHtmlContent Serialize(object value) { JsonSerializeOption option = null; if (value is JsonResultWrapper wrapper) { option = wrapper.Option; } else { var hosting = context.RequestServices.GetService(typeof(JsonSerializeOptionHosting)) as JsonSerializeOptionHosting; if (hosting != null) { option = hosting.Option; } } if (option == null) { option = mvcOptions.JsonSerializeOption; } else { option.Reference(mvcOptions.JsonSerializeOption); } var serializer = new JsonSerializer(option); var json = serializer.Serialize(value); return(new HtmlString(json)); }
public override Task ExecuteAsync(ActionContext context, JsonResult result) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (result == null) { throw new ArgumentNullException(nameof(result)); } var response = context.HttpContext.Response; ResponseContentTypeHelper.ResolveContentTypeAndEncoding( result.ContentType, response.ContentType, DefaultContentType, out var resolvedContentType, out var resolvedContentTypeEncoding); response.ContentType = resolvedContentType; if (result.Value != null) { JsonSerializeOption option = null; if (result is JsonResultWrapper wrapper) { option = wrapper.Option; } else { var hosting = context.HttpContext.RequestServices.GetService <JsonSerializeOptionHosting>(); if (hosting != null) { option = hosting.Option; } } if (option == null) { option = mvcOptions.JsonSerializeOption; } else { option.Reference(mvcOptions.JsonSerializeOption); } var serializer = new JsonSerializer(option); var content = serializer.Serialize(result.Value); response.Body.WriteAsync(resolvedContentTypeEncoding.GetBytes(content)); } return(Task.CompletedTask); }
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) { var response = context.HttpContext.Response; var serviceProvider = context.HttpContext.RequestServices; JsonSerializeOption option = null; if (context.Object is JsonResultWrapper wrapper) { option = wrapper.Option; } else { var hosting = context.HttpContext.RequestServices.GetService <JsonSerializeOptionHosting>(); if (hosting != null) { option = hosting.Option; } } if (option == null) { option = _mvcOptions.JsonSerializeOption; } else { option.Reference(_mvcOptions.JsonSerializeOption); } var serializer = serviceProvider.TryGetService <ISerializer>(() => new JsonSerializer(option)); if (serializer is ITextSerializer txtSerializer) { var content = txtSerializer.Serialize(context.Object); response.Body.WriteAsync(selectedEncoding.GetBytes(content)); } else { response.Body.WriteAsync(serializer.Serialize(context.Object)); } return(Task.CompletedTask); }
public IHtmlContent Serialize(object value) { var serviceProvider = context.RequestServices; JsonSerializeOption option = null; if (value is JsonResultWrapper wrapper) { option = wrapper.Option; } else { if (context.RequestServices.GetService(typeof(JsonSerializeOptionHosting)) is JsonSerializeOptionHosting hosting) { option = hosting.Option; } } if (option == null) { option = mvcOptions.JsonSerializeOption; } else { option.Reference(mvcOptions.JsonSerializeOption); } var serializer = serviceProvider.TryGetService <ISerializer>(() => new JsonSerializer(option)); string json; if (serializer is ITextSerializer txtSerializer) { json = txtSerializer.Serialize(value); } else { json = Encoding.UTF8.GetString(serializer.Serialize(value)); } return(new HtmlString(json)); }
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) { var response = context.HttpContext.Response; using (var writer = context.WriterFactory(response.Body, selectedEncoding)) { JsonSerializeOption option = null; if (context.Object is JsonResultWrapper wrapper) { option = wrapper.Option; } else { var hosting = context.HttpContext.RequestServices.GetService <JsonSerializeOptionHosting>(); if (hosting != null) { option = hosting.Option; } } if (option == null) { option = mvcOptions.JsonSerializeOption; } else { option.Reference(mvcOptions.JsonSerializeOption); } var serializer = new JsonSerializer(option); using (var jsonWriter = new JsonWriter(writer)) { serializer.Serialize(context.Object, jsonWriter); } } return(Task.CompletedTask); }