aspnetcore/src/Microsoft.AspNetCore.Mvc.Fo.../JsonSerializerSettingsProvi...

41 lines
1.5 KiB
C#

// 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 Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Microsoft.AspNetCore.Mvc.Formatters
{
/// <summary>
/// Helper class which provides <see cref="JsonSerializerSettings"/>.
/// </summary>
public static class JsonSerializerSettingsProvider
{
private const int DefaultMaxDepth = 32;
/// <summary>
/// Creates default <see cref="JsonSerializerSettings"/>.
/// </summary>
/// <returns>Default <see cref="JsonSerializerSettings"/>.</returns>
public static JsonSerializerSettings CreateSerializerSettings()
{
return new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy(),
},
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,
};
}
}
}