// 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.Collections;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Newtonsoft.Json;
namespace Microsoft.AspNetCore.Mvc
{
///
/// Provides programmatic configuration for JSON in the MVC framework.
///
public class MvcJsonOptions : IEnumerable
{
private readonly CompatibilitySwitch _allowInputFormatterExceptionMessages;
private readonly ICompatibilitySwitch[] _switches;
///
/// Creates a new instance of .
///
public MvcJsonOptions()
{
_allowInputFormatterExceptionMessages = new CompatibilitySwitch(nameof(AllowInputFormatterExceptionMessages));
_switches = new ICompatibilitySwitch[]
{
_allowInputFormatterExceptionMessages,
};
}
///
/// Gets or sets a flag to determine whether error messages from JSON deserialization by the
/// will be added to the . The default
/// value is false, meaning that a generic error message will be used instead.
///
///
///
/// Error messages in the are often communicated to clients, either in HTML
/// or using . In effect, this setting controls whether clients can receive
/// detailed error messages about submitted JSON data.
///
///
/// This property is associated with a compatibility switch and can provide a different behavior depending on
/// the configured compatibility version for the application. See for
/// guidance and examples of setting the application's compatibility version.
///
///
/// Configuring the desired of the value compatibility switch by calling this property's setter will take precedence
/// over the value implied by the application's .
///
///
/// If the application's compatibility version is set to then
/// this setting will have value false unless explicitly configured.
///
///
/// If the application's compatibility version is set to or
/// higher then this setting will have value true unless explicitly configured.
///
///
public bool AllowInputFormatterExceptionMessages
{
get => _allowInputFormatterExceptionMessages.Value;
set => _allowInputFormatterExceptionMessages.Value = value;
}
///
/// Gets the that are used by this application.
///
public JsonSerializerSettings SerializerSettings { get; } = JsonSerializerSettingsProvider.CreateSerializerSettings();
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_switches).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() => _switches.GetEnumerator();
}
}