diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs
index 3318b2c5d6..faa96e6cc9 100644
--- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/JsonResult.cs
@@ -43,7 +43,7 @@ namespace Microsoft.AspNet.Mvc
/// The to be used by
/// the formatter.
public JsonResult(object value, [NotNull] JsonSerializerSettings serializerSettings)
- : this(value, formatter: new JsonOutputFormatter { SerializerSettings = serializerSettings })
+ : this(value, formatter: new JsonOutputFormatter(serializerSettings))
{
}
diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonContractResolver.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonContractResolver.cs
deleted file mode 100644
index d46c85f265..0000000000
--- a/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonContractResolver.cs
+++ /dev/null
@@ -1,43 +0,0 @@
-// Copyright (c) .NET Foundation. All rights reserved.
-// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
-
-using System;
-using System.ComponentModel.DataAnnotations;
-using System.Reflection;
-using Newtonsoft.Json;
-using Newtonsoft.Json.Serialization;
-
-namespace Microsoft.AspNet.Mvc
-{
- ///
- /// The default for .
- /// It determines if a value type member has and sets the appropriate
- /// JsonProperty settings.
- ///
- public class JsonContractResolver : DefaultContractResolver
- {
- protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
- {
- var property = base.CreateProperty(member, memberSerialization);
-
- var required = member.GetCustomAttribute(typeof(RequiredAttribute), inherit: true);
- if (required != null)
- {
- var propertyType = ((PropertyInfo)member).PropertyType;
-
- // DefaultObjectValidator does required attribute validation on properties based on the property
- // value being null. Since this is not possible in case of value types, we depend on the formatters
- // to handle value type validation.
- // With the following settings here, if a value is not present on the wire for value types
- // like primitive, struct etc., Json.net's serializer would throw exception which we catch
- // and add it to model state.
- if (propertyType.GetTypeInfo().IsValueType && !TypeHelper.IsNullableValueType(propertyType))
- {
- property.Required = Required.AllowNull;
- }
- }
-
- return property;
- }
- }
-}
\ No newline at end of file
diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonInputFormatter.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonInputFormatter.cs
index 0d56574795..3498331fef 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonInputFormatter.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonInputFormatter.cs
@@ -5,6 +5,7 @@ using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
+using Microsoft.AspNet.Mvc.Core.Internal;
using Microsoft.Framework.Internal;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
@@ -13,31 +14,22 @@ namespace Microsoft.AspNet.Mvc
{
public class JsonInputFormatter : InputFormatter
{
- private const int DefaultMaxDepth = 32;
- private JsonSerializerSettings _jsonSerializerSettings;
+ private JsonSerializerSettings _serializerSettings;
public JsonInputFormatter()
+ : this(SerializerSettingsProvider.CreateSerializerSettings())
{
+ }
+
+ public JsonInputFormatter([NotNull] JsonSerializerSettings serializerSettings)
+ {
+ _serializerSettings = serializerSettings;
+
SupportedEncodings.Add(Encodings.UTF8EncodingWithoutBOM);
SupportedEncodings.Add(Encodings.UTF16EncodingLittleEndian);
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/json"));
-
- _jsonSerializerSettings = new JsonSerializerSettings
- {
- MissingMemberHandling = MissingMemberHandling.Ignore,
-
- // Limit the object graph we'll consume to a fixed depth. This prevents stackoverflow exceptions
- // from deserialization errors that might occur from deeply nested objects.
- MaxDepth = DefaultMaxDepth,
-
- // Do not change this setting
- // Setting this to None prevents Json.NET from loading malicious, unsafe, or security-sensitive types
- TypeNameHandling = TypeNameHandling.None
- };
-
- _jsonSerializerSettings.ContractResolver = new JsonContractResolver();
}
///
@@ -45,15 +37,14 @@ namespace Microsoft.AspNet.Mvc
///
public JsonSerializerSettings SerializerSettings
{
- get { return _jsonSerializerSettings; }
+ get
+ {
+ return _serializerSettings;
+ }
+ [param: NotNull]
set
{
- if (value == null)
- {
- throw new ArgumentNullException(nameof(value));
- }
-
- _jsonSerializerSettings = value;
+ _serializerSettings = value;
}
}
diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonOutputFormatter.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonOutputFormatter.cs
index 1b4ab69cf9..e6f9a47bd9 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonOutputFormatter.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonOutputFormatter.cs
@@ -4,6 +4,7 @@
using System;
using System.IO;
using System.Threading.Tasks;
+using Microsoft.AspNet.Mvc.Core.Internal;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.Framework.Internal;
using Microsoft.Net.Http.Headers;
@@ -16,13 +17,18 @@ namespace Microsoft.AspNet.Mvc
private JsonSerializerSettings _serializerSettings;
public JsonOutputFormatter()
+ : this(SerializerSettingsProvider.CreateSerializerSettings())
{
+ }
+
+ public JsonOutputFormatter([NotNull] JsonSerializerSettings serializerSettings)
+ {
+ _serializerSettings = serializerSettings;
+
SupportedEncodings.Add(Encodings.UTF8EncodingWithoutBOM);
SupportedEncodings.Add(Encodings.UTF16EncodingLittleEndian);
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json"));
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("text/json"));
-
- _serializerSettings = new JsonSerializerSettings();
}
///
@@ -30,14 +36,13 @@ namespace Microsoft.AspNet.Mvc
///
public JsonSerializerSettings SerializerSettings
{
- get { return _serializerSettings; }
+ get
+ {
+ return _serializerSettings;
+ }
+ [param: NotNull]
set
{
- if (value == null)
- {
- throw new ArgumentNullException(nameof(value));
- }
-
_serializerSettings = value;
}
}
diff --git a/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonPatchInputFormatter.cs b/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonPatchInputFormatter.cs
index 2c5ccdf7ce..bcdd9f5a1a 100644
--- a/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonPatchInputFormatter.cs
+++ b/src/Microsoft.AspNet.Mvc.Core/Formatters/JsonPatchInputFormatter.cs
@@ -6,12 +6,20 @@ using System.Threading.Tasks;
using Microsoft.AspNet.JsonPatch;
using Microsoft.Framework.Internal;
using Microsoft.Net.Http.Headers;
+using Microsoft.AspNet.Mvc.Core.Internal;
+using Newtonsoft.Json;
namespace Microsoft.AspNet.Mvc
{
public class JsonPatchInputFormatter : JsonInputFormatter
{
public JsonPatchInputFormatter()
+ : this(SerializerSettingsProvider.CreateSerializerSettings())
+ {
+ }
+
+ public JsonPatchInputFormatter([NotNull] JsonSerializerSettings serializerSettings)
+ : base(serializerSettings)
{
// Clear all values and only include json-patch+json value.
SupportedMediaTypes.Clear();
@@ -23,7 +31,7 @@ namespace Microsoft.AspNet.Mvc
public async override Task