/// <summary>
        /// Configures Newtonsoft.Json using OData Json converter.
        /// </summary>
        /// <param name="builder">The Mvc core builder.</param>
        /// <param name="mapperProvider">The mapper provider.</param>
        /// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
        public static IMvcCoreBuilder AddODataNewtonsoftJson(this IMvcCoreBuilder builder,
                                                             Func <IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder.AddNewtonsoftJson(BuildSetupAction(mapperProvider)));
        }
 public static IMvcCoreBuilder AddDefaultJsonOptions(this IMvcCoreBuilder builder)
 => builder.AddNewtonsoftJson(o => {
     o.SerializerSettings.ContractResolver           = new CamelCasePropertyNamesContractResolver();
     o.SerializerSettings.DateFormatHandling         = DateFormatHandling.IsoDateFormat;
     o.SerializerSettings.DateParseHandling          = DateParseHandling.DateTimeOffset;
     o.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
     o.SerializerSettings.ReferenceLoopHandling      = ReferenceLoopHandling.Ignore;
     o.SerializerSettings.Formatting = Formatting.Indented;
     o.SerializerSettings.Converters.Add(new StringEnumConverter());
 });
Example #3
0
 public static IMvcCoreBuilder AddNewtonsoftJson(this IMvcCoreBuilder builder)
 {
     return(builder.AddNewtonsoftJson(options =>
     {
         options.SerializerSettings.Formatting = Formatting.Indented;
         options.SerializerSettings.Converters.Add(new StringEnumConverter());
         options.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
         options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
         options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
     }));
 }
Example #4
0
        /// <summary>
        /// Конфигурация Json форматирования
        /// </summary>
        /// <param name="mvcCoreBuilder"></param>
        /// <returns></returns>
        public static IMvcCoreBuilder AddResponseFormatters(this IMvcCoreBuilder mvcCoreBuilder)
        {
            mvcCoreBuilder.AddNewtonsoftJson(opt =>
            {
                opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                opt.SerializerSettings.NullValueHandling     = NullValueHandling.Ignore;
                opt.SerializerSettings.Formatting            = Formatting.Indented;
                opt.SerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
                opt.SerializerSettings.Converters.Add(new StringEnumConverter(typeof(CamelCaseNamingStrategy)));
            })
            .AddJsonOptions(options =>
                            options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)));;

            return(mvcCoreBuilder);
        }
Example #5
0
    public static IMvcCoreBuilder AddAbpHybridJson(this IMvcCoreBuilder builder)
    {
        var abpJsonOptions = builder.Services.ExecutePreConfiguredActions <AbpJsonOptions>();

        if (!abpJsonOptions.UseHybridSerializer)
        {
            builder.Services.TryAddEnumerable(ServiceDescriptor.Transient <IConfigureOptions <MvcNewtonsoftJsonOptions>, AbpMvcNewtonsoftJsonOptionsSetup>());
            builder.AddNewtonsoftJson();
            return(builder);
        }

        builder.Services.TryAddTransient <DefaultObjectPoolProvider>();
        builder.Services.TryAddEnumerable(ServiceDescriptor.Transient <IConfigureOptions <JsonOptions>, AbpJsonOptionsSetup>());
        builder.Services.TryAddEnumerable(ServiceDescriptor.Transient <IConfigureOptions <MvcNewtonsoftJsonOptions>, AbpMvcNewtonsoftJsonOptionsSetup>());
        builder.Services.TryAddEnumerable(ServiceDescriptor.Transient <IConfigureOptions <MvcOptions>, AbpHybridJsonOptionsSetup>());
        return(builder);
    }
Example #6
0
        public static IMvcCoreBuilder AddGitLfs(this IMvcCoreBuilder builder)
        {
            builder.AddNewtonsoftJson();
            builder.AddApplicationPart(typeof(LfsConstants).GetTypeInfo().Assembly);
            builder.AddMvcOptions(options =>
            {
                options.Filters.Add(new ProducesAttribute(LfsConstants.LfsMediaType.MediaType.Buffer));
                options.Filters.Add(new TypeFilterAttribute(typeof(BasicAuthFilter)));

                foreach (InputFormatter input in options.InputFormatters.OfType <NewtonsoftJsonInputFormatter>())
                {
                    input.SupportedMediaTypes.Add(LfsConstants.LfsMediaType);
                }

                foreach (OutputFormatter output in options.OutputFormatters.OfType <NewtonsoftJsonOutputFormatter>())
                {
                    output.SupportedMediaTypes.Add(LfsConstants.LfsMediaType);
                }
            });
            return(builder);
        }
        internal static IMvcCoreBuilder AddCustomSerialization(this IMvcCoreBuilder builder)
        {
            builder.AddNewtonsoftJson(UpdateMvcNewtonsoftJsonOptions);

            return(builder);
        }
 /// <summary>
 /// Configures Newtonsoft.Json using OData Json converter.
 /// </summary>
 /// <param name="builder">The Mvc core builder.</param>
 /// <returns>The <see cref="IMvcCoreBuilder"/>.</returns>
 public static IMvcCoreBuilder AddODataNewtonsoftJson(this IMvcCoreBuilder builder)
 {
     return(builder.AddNewtonsoftJson(null));
 }
Example #9
0
        public static IMvcCoreBuilder AddAbpHybridJson(this IMvcCoreBuilder builder)
        {
            var abpJsonOptions = builder.Services.ExecutePreConfiguredActions <AbpJsonOptions>();

            if (!abpJsonOptions.UseHybridSerializer)
            {
                builder.Services.TryAddEnumerable(ServiceDescriptor.Transient <IConfigureOptions <MvcNewtonsoftJsonOptions>, AbpMvcNewtonsoftJsonOptionsSetup>());
                builder.AddNewtonsoftJson();
                return(builder);
            }

            //SystemTextJsonInputFormatter
            builder.Services.AddTransient(provider =>
            {
                var jsonOptions = provider.GetRequiredService <IOptions <JsonOptions> >();
                var logger      = provider.GetRequiredService <ILoggerFactory>().CreateLogger <SystemTextJsonInputFormatter>();
                return(new SystemTextJsonInputFormatter(jsonOptions.Value, logger));
            });

            builder.Services.TryAddTransient <DefaultObjectPoolProvider>();
            //NewtonsoftJsonInputFormatter
            builder.Services.AddTransient(provider =>
            {
                var jsonOptions = provider.GetRequiredService <IOptions <MvcNewtonsoftJsonOptions> >().Value;

                return(new NewtonsoftJsonInputFormatter(
                           provider.GetRequiredService <ILoggerFactory>().CreateLogger <NewtonsoftJsonInputFormatter>(),
                           jsonOptions.SerializerSettings,
                           provider.GetRequiredService <ArrayPool <char> >(),
                           provider.GetRequiredService <DefaultObjectPoolProvider>(),
                           provider.GetRequiredService <IOptions <MvcOptions> >().Value,
                           jsonOptions));
            });

            //SystemTextJsonOutputFormatter
            builder.Services.AddTransient(provider =>
            {
                var jsonSerializerOptions = provider.GetRequiredService <IOptions <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,
                    };
                }
                return(new SystemTextJsonOutputFormatter(jsonSerializerOptions));
            });

            //NewtonsoftJsonOutputFormatter
            builder.Services.AddTransient(provider =>
            {
                var jsonOptions = provider.GetRequiredService <IOptions <MvcNewtonsoftJsonOptions> >().Value;
                return(new NewtonsoftJsonOutputFormatter(
                           jsonOptions.SerializerSettings,
                           provider.GetRequiredService <ArrayPool <char> >(),
                           provider.GetRequiredService <IOptions <MvcOptions> >().Value));
            });

            builder.Services.TryAddEnumerable(ServiceDescriptor.Transient <IConfigureOptions <JsonOptions>, AbpJsonOptionsSetup>());
            builder.Services.TryAddEnumerable(ServiceDescriptor.Transient <IConfigureOptions <MvcNewtonsoftJsonOptions>, AbpMvcNewtonsoftJsonOptionsSetup>());

            builder.Services.Configure <MvcOptions>(options =>
            {
                options.InputFormatters.RemoveType <SystemTextJsonInputFormatter>();
                options.InputFormatters.RemoveType <NewtonsoftJsonInputFormatter>();
                options.InputFormatters.Add(new AbpHybridJsonInputFormatter());

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

            return(builder);
        }