Ejemplo n.º 1
0
    public AbpHybridJsonInputFormatter(SystemTextJsonInputFormatter systemTextJsonInputFormatter, NewtonsoftJsonInputFormatter newtonsoftJsonInputFormatter)
    {
        _systemTextJsonInputFormatter = systemTextJsonInputFormatter;
        _newtonsoftJsonInputFormatter = newtonsoftJsonInputFormatter;

        SupportedEncodings.Add(UTF8EncodingWithoutBOM);
        SupportedEncodings.Add(UTF16EncodingLittleEndian);

        SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationJson);
        SupportedMediaTypes.Add(MediaTypeHeaderValues.TextJson);
        SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationAnyJsonSyntax);
    }
Ejemplo n.º 2
0
        public void Configure(MvcOptions options)
        {
            var systemTextJsonInputFormatter = new SystemTextJsonInputFormatter(
                _jsonOptions.Value,
                _loggerFactory.CreateLogger <SystemTextJsonInputFormatter>());

            var newtonsoftJsonInputFormatter = new NewtonsoftJsonInputFormatter(
                _loggerFactory.CreateLogger <NewtonsoftJsonInputFormatter>(),
                _mvcNewtonsoftJsonOptions.Value.SerializerSettings,
                _charPool,
                _objectPoolProvider,
                options,
                _mvcNewtonsoftJsonOptions.Value);

            options.InputFormatters.RemoveType <SystemTextJsonInputFormatter>();
            options.InputFormatters.RemoveType <NewtonsoftJsonInputFormatter>();
            options.InputFormatters.Add(new AbpHybridJsonInputFormatter(systemTextJsonInputFormatter, newtonsoftJsonInputFormatter));

            var jsonSerializerOptions = _jsonOptions.Value.JsonSerializerOptions;

            if (jsonSerializerOptions.Encoder is null)
            {
                // If the user hasn't explicitly configured the encoder, use the less strict encoder that does not encode all non-ASCII characters.
                jsonSerializerOptions = new JsonSerializerOptions(jsonSerializerOptions)
                {
                    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
                };
            }

            var systemTextJsonOutputFormatter = new SystemTextJsonOutputFormatter(jsonSerializerOptions);
            var newtonsoftJsonOutputFormatter = new NewtonsoftJsonOutputFormatter(
                _mvcNewtonsoftJsonOptions.Value.SerializerSettings,
                _charPool,
                options);

            options.OutputFormatters.RemoveType <SystemTextJsonOutputFormatter>();
            options.OutputFormatters.RemoveType <NewtonsoftJsonOutputFormatter>();
            options.OutputFormatters.Add(new AbpHybridJsonOutputFormatter(systemTextJsonOutputFormatter, newtonsoftJsonOutputFormatter));
        }