Commonizing JSON writer/serializer code
This doesn't add any new extensibility, and will likely change again in the future when we add extensibility. For now this is a stopgap to reduce duplication between JsonResult and JsonViewComponentResult.
This commit is contained in:
parent
469414c419
commit
6da448fb8e
|
|
@ -22,7 +22,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
}
|
}
|
||||||
|
|
||||||
_returnValue = returnValue;
|
_returnValue = returnValue;
|
||||||
_jsonSerializerSettings = CreateSerializerSettings();
|
_jsonSerializerSettings = JsonOutputFormatter.CreateDefaultSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonSerializerSettings SerializerSettings
|
public JsonSerializerSettings SerializerSettings
|
||||||
|
|
@ -70,45 +70,11 @@ namespace Microsoft.AspNet.Mvc
|
||||||
response.ContentType = "application/json";
|
response.ContentType = "application/json";
|
||||||
}
|
}
|
||||||
|
|
||||||
using (JsonWriter jsonWriter = CreateJsonWriter(writeStream, Encoding))
|
using (var writer = new StreamWriter(writeStream, Encoding, 1024, leaveOpen: true))
|
||||||
{
|
{
|
||||||
jsonWriter.CloseOutput = false;
|
var formatter = new JsonOutputFormatter(SerializerSettings, Indent);
|
||||||
|
formatter.WriteObject(writer, _returnValue);
|
||||||
JsonSerializer jsonSerializer = CreateJsonSerializer();
|
|
||||||
jsonSerializer.Serialize(jsonWriter, _returnValue);
|
|
||||||
|
|
||||||
jsonWriter.Flush();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonSerializerSettings CreateSerializerSettings()
|
|
||||||
{
|
|
||||||
return new JsonSerializerSettings()
|
|
||||||
{
|
|
||||||
MissingMemberHandling = MissingMemberHandling.Ignore,
|
|
||||||
|
|
||||||
// Do not change this setting
|
|
||||||
// Setting this to None prevents Json.NET from loading malicious, unsafe, or security-sensitive types.
|
|
||||||
TypeNameHandling = TypeNameHandling.None
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private JsonSerializer CreateJsonSerializer()
|
|
||||||
{
|
|
||||||
JsonSerializer jsonSerializer = JsonSerializer.Create(SerializerSettings);
|
|
||||||
|
|
||||||
return jsonSerializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
private JsonWriter CreateJsonWriter(Stream writeStream, Encoding effectiveEncoding)
|
|
||||||
{
|
|
||||||
JsonWriter jsonWriter = new JsonTextWriter(new StreamWriter(writeStream, effectiveEncoding));
|
|
||||||
if (Indent)
|
|
||||||
{
|
|
||||||
jsonWriter.Formatting = Formatting.Indented;
|
|
||||||
}
|
|
||||||
|
|
||||||
return jsonWriter;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
|
||||||
|
using System.IO;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNet.Mvc
|
||||||
|
{
|
||||||
|
public class JsonOutputFormatter
|
||||||
|
{
|
||||||
|
private readonly JsonSerializerSettings _settings;
|
||||||
|
private readonly bool _indent;
|
||||||
|
|
||||||
|
public JsonOutputFormatter([NotNull] JsonSerializerSettings settings, bool indent)
|
||||||
|
{
|
||||||
|
_settings = settings;
|
||||||
|
_indent = indent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonSerializerSettings CreateDefaultSettings()
|
||||||
|
{
|
||||||
|
return new JsonSerializerSettings()
|
||||||
|
{
|
||||||
|
MissingMemberHandling = MissingMemberHandling.Ignore,
|
||||||
|
|
||||||
|
// Do not change this setting
|
||||||
|
// Setting this to None prevents Json.NET from loading malicious, unsafe, or security-sensitive types.
|
||||||
|
TypeNameHandling = TypeNameHandling.None
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WriteObject([NotNull] TextWriter writer, object value)
|
||||||
|
{
|
||||||
|
using (var jsonWriter = CreateJsonWriter(writer))
|
||||||
|
{
|
||||||
|
var jsonSerializer = CreateJsonSerializer();
|
||||||
|
jsonSerializer.Serialize(jsonWriter, value);
|
||||||
|
|
||||||
|
// We're explicitly calling flush here to simplify the debugging experience because the
|
||||||
|
// underlying TextWriter might be long-lived. If this method ends up being called repeatedly
|
||||||
|
// for a request, we should revisit.
|
||||||
|
jsonWriter.Flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonWriter CreateJsonWriter([NotNull] TextWriter writer)
|
||||||
|
{
|
||||||
|
var jsonWriter = new JsonTextWriter(writer);
|
||||||
|
if (_indent)
|
||||||
|
{
|
||||||
|
jsonWriter.Formatting = Formatting.Indented;
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonWriter.CloseOutput = false;
|
||||||
|
|
||||||
|
return jsonWriter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonSerializer CreateJsonSerializer()
|
||||||
|
{
|
||||||
|
var jsonSerializer = JsonSerializer.Create(_settings);
|
||||||
|
return jsonSerializer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
|
@ -15,7 +14,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
public JsonViewComponentResult([NotNull] object value)
|
public JsonViewComponentResult([NotNull] object value)
|
||||||
{
|
{
|
||||||
_value = value;
|
_value = value;
|
||||||
_jsonSerializerSettings = CreateSerializerSettings();
|
_jsonSerializerSettings = JsonOutputFormatter.CreateDefaultSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonSerializerSettings SerializerSettings
|
public JsonSerializerSettings SerializerSettings
|
||||||
|
|
@ -38,46 +37,10 @@ namespace Microsoft.AspNet.Mvc
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool Indent { get; set; }
|
public bool Indent { get; set; }
|
||||||
|
|
||||||
private JsonSerializerSettings CreateSerializerSettings()
|
|
||||||
{
|
|
||||||
return new JsonSerializerSettings()
|
|
||||||
{
|
|
||||||
MissingMemberHandling = MissingMemberHandling.Ignore,
|
|
||||||
|
|
||||||
// Do not change this setting
|
|
||||||
// Setting this to None prevents Json.NET from loading malicious, unsafe, or security-sensitive types.
|
|
||||||
TypeNameHandling = TypeNameHandling.None
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private JsonSerializer CreateJsonSerializer()
|
|
||||||
{
|
|
||||||
var jsonSerializer = JsonSerializer.Create(SerializerSettings);
|
|
||||||
return jsonSerializer;
|
|
||||||
}
|
|
||||||
|
|
||||||
private JsonWriter CreateJsonWriter([NotNull] TextWriter writer)
|
|
||||||
{
|
|
||||||
var jsonWriter = new JsonTextWriter(writer);
|
|
||||||
if (Indent)
|
|
||||||
{
|
|
||||||
jsonWriter.Formatting = Formatting.Indented;
|
|
||||||
}
|
|
||||||
|
|
||||||
return jsonWriter;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Execute([NotNull] ViewComponentContext context)
|
public void Execute([NotNull] ViewComponentContext context)
|
||||||
{
|
{
|
||||||
using (var jsonWriter = CreateJsonWriter(context.Writer))
|
var formatter = new JsonOutputFormatter(SerializerSettings, Indent);
|
||||||
{
|
formatter.WriteObject(context.Writer, _value);
|
||||||
jsonWriter.CloseOutput = false;
|
|
||||||
|
|
||||||
var jsonSerializer = CreateJsonSerializer();
|
|
||||||
jsonSerializer.Serialize(jsonWriter, _value);
|
|
||||||
|
|
||||||
jsonWriter.Flush();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task ExecuteAsync([NotNull] ViewComponentContext context)
|
public async Task ExecuteAsync([NotNull] ViewComponentContext context)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue