private HtmlString SerializeInternal(JsonOutputFormatter jsonOutputFormatter, object value) { var stringWriter = new StringWriter(CultureInfo.InvariantCulture); jsonOutputFormatter.WriteObject(stringWriter, value); return new HtmlString(stringWriter.ToString()); }
protected override void PopulateStoreConfig(IDictionary <string, object> config) { using (var writer = new StringWriter()) { _jsonFormatter.WriteObject(writer, Items); config["data"] = new JRaw(writer.ToString()); } }
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) { if (context == null) { throw new ArgumentNullException(nameof(context)); } string callback; if (IsJsonpRequest(context.HttpContext.Request, _callbackQueryParameter, out callback)) { if (!CallbackValidator.IsValid(callback)) { throw new InvalidOperationException($"Callback '{callback}' is invalid!"); } using (var writer = context.WriterFactory(context.HttpContext.Response.Body, selectedEncoding)) { // the /**/ is a specific security mitigation for "Rosetta Flash JSONP abuse" // the typeof check is just to reduce client error noise var str = "/**/ typeof " + callback + " === 'function' && " + callback + "("; str += context.Object + ");"; writer.Write("/**/ typeof " + callback + " === 'function' && " + callback + "("); writer.Flush(); _jsonMediaTypeFormatter.WriteObject(writer, context.Object); writer.Write(");"); await writer.FlushAsync(); } } else { await _jsonMediaTypeFormatter.WriteResponseBodyAsync(context, selectedEncoding); } }
private IHtmlContent SerializeInternal(JsonOutputFormatter jsonOutputFormatter, object value) { var stringWriter = new StringWriter(CultureInfo.InvariantCulture); jsonOutputFormatter.WriteObject(stringWriter, value); return(new HtmlString(stringWriter.ToString())); }
public async Task CacheResult(string cacheKey, ApiResult result, int expire = 0) { if (cacheKey == null) { throw new ArgumentNullException(nameof(cacheKey)); } if (_connection == null) { return; } // Store the IActionResult object as a JSON string using (var writer = new StringWriter()) { _outputFormatter.WriteObject(writer, result); string storedValue = writer.ToString(); await _connection.GetDatabase().StringSetAsync( cacheKey, storedValue, expiry: TimeSpan.FromSeconds(expire) ); } }