Пример #1
0
        /// <summary>
        /// This fix the case
        /// </summary>
        public static IMvcCoreBuilder SetJsonFormatter(this IMvcCoreBuilder mvcCoreBuilder)
        {
            mvcCoreBuilder.AddJsonOptions(options =>
                                          options.SerializerSettings.ContractResolver = new DefaultContractResolver()).AddJsonFormatters();

            return(mvcCoreBuilder);
        }
Пример #2
0
 /// <summary>
 /// Format json response data.
 /// See https://docs.microsoft.com/es-es/aspnet/core/web-api/advanced/formatting
 /// </summary>
 /// <param name="builder"></param>
 /// <returns></returns>
 public static IMvcCoreBuilder AddCustomJsonOptions(this IMvcCoreBuilder builder) =>
 builder.AddJsonOptions(options =>
 {
     // Use the default property (Pascal) casing.
     options.JsonSerializerOptions.PropertyNamingPolicy = null;
     options.JsonSerializerOptions.IgnoreNullValues     = true;
 });
Пример #3
0
        private static IMvcCoreBuilder ConfigureJson(this IMvcCoreBuilder builder)
        {
            builder.AddFormatterMappings();

            builder.AddJsonFormatters(f =>
            {
                f.Formatting            = Formatting.Indented;
                f.NullValueHandling     = NullValueHandling.Ignore;
                f.MissingMemberHandling = MissingMemberHandling.Ignore;
            });

            builder.AddJsonOptions(options =>
            {
                options.SerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;

                options.SerializerSettings.Converters.Add(new StringEnumConverter());
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

                JsonConvert.DefaultSettings = () => new JsonSerializerSettings
                {
                    Formatting = Formatting.Indented,
                    Converters = new List <JsonConverter> {
                        new StringEnumConverter()
                    },
                    DateParseHandling = DateParseHandling.DateTimeOffset,
                    ContractResolver  = new CamelCasePropertyNamesContractResolver()
                };
            });

            return(builder);
        }
Пример #4
0
        public static IMvcCoreBuilder AddJson(this IMvcCoreBuilder mvc)
        {
#if NETCOREAPP2_2
            return(mvc.AddJsonFormatters(json => json.NullValueHandling = NullValueHandling.Ignore));
#else
            return(mvc.AddJsonOptions(json => { json.JsonSerializerOptions.IgnoreNullValues = true; }));
#endif
        }
Пример #5
0
 public static IMvcCoreBuilder AddCustomJsonOptions(this IMvcCoreBuilder builder)
 {
     return(builder
            .AddJsonOptions(options =>
     {
         options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
         options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
     }));
 }
Пример #6
0
 public static void ConfigureJson(this IMvcCoreBuilder mvc)
 {
     mvc.AddJsonFormatters();
     mvc.AddJsonOptions(options => {
         options.SerializerSettings.Converters.Add(new StringEnumConverter {
             CamelCaseText = true
         });
     });
 }
Пример #7
0
 public static IMvcCoreBuilder AddCustomJsonOptions(this IMvcCoreBuilder builder) =>
 builder.AddJsonOptions(
     options =>
 {
     options.SerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;
     options.SerializerSettings.Converters.Add(new StringEnumConverter());
     options.SerializerSettings.ContractResolver      = new CamelCasePropertyNamesContractResolver();
     options.SerializerSettings.NullValueHandling     = NullValueHandling.Ignore;
     options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Error;
 });
Пример #8
0
 public static IMvcCoreBuilder AddDefaultJsonOptions(this IMvcCoreBuilder builder)
 => builder.AddJsonOptions(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;
 });
Пример #9
0
 public static IMvcCoreBuilder AddJsonOptions(this IMvcCoreBuilder builder)
 {
     return(builder.AddJsonOptions(options =>
     {
         options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
         options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
         options.JsonSerializerOptions.WriteIndented = true;
         options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
     }));
 }
 /// <summary>
 /// Adds customized JSON serializer settings.
 /// </summary>
 public static IMvcCoreBuilder AddCustomJsonOptions(this IMvcCoreBuilder builder) =>
 builder.AddJsonOptions(
     options =>
 {
     // Parse dates as DateTimeOffset values by default. You should prefer using DateTimeOffset over
     // DateTime everywhere. Not doing so can cause problems with time-zones.
     options.SerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;
     // Output enumeration values as strings in JSON.
     options.SerializerSettings.Converters.Add(new StringEnumConverter());
 });
Пример #11
0
 public static IMvcCoreBuilder AddJsonCamelCaseOptions(this IMvcCoreBuilder builder)
 {
     return(builder.AddJsonOptions(options => {
         options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver
         {
             NamingStrategy = new SnakeCaseNamingStrategy()
         };
         options.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Include;
         options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
     }));
 }
Пример #12
0
        /// <summary>
        /// Adds customized JSON serializer settings.
        /// </summary>
        public static IMvcCoreBuilder AddCustomJsonOptions(this IMvcCoreBuilder builder, IHostingEnvironment hostingEnvironment) =>
        builder.AddJsonOptions(
            options =>
        {
            if (hostingEnvironment.IsDevelopment())
            {
                // Pretty print the JSON in development for easier debugging.
                options.SerializerSettings.Formatting = Formatting.Indented;
            }

            // Parse dates as DateTimeOffset values by default. You should prefer using DateTimeOffset over
            // DateTime everywhere. Not doing so can cause problems with time-zones.
            options.SerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;

            // Output enumeration values as strings in JSON.
            options.SerializerSettings.Converters.Add(new StringEnumConverter());
        });
Пример #13
0
        private static void AddServicesDebuggerApi(this IMvcCoreBuilder mvcBuilder,
                                                   Action <ServicesDebugOptions> configureAction = null)
        {
            if (configureAction != null)
            {
                mvcBuilder.Services.Configure(configureAction);
            }

            mvcBuilder.AddJsonOptions(o =>
            {
                o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            });

            mvcBuilder
            .AddActiveRoute <OperationsBuilder, ServicesDebugController, ServicesDebugFeature, ServicesDebugOptions
                             >();
        }
Пример #14
0
        private static void AddOptionsDebuggerApi(this IMvcCoreBuilder mvcBuilder,
                                                  Action <OptionsDebugOptions> configureAction = null)
        {
            if (configureAction != null)
            {
                mvcBuilder.Services.Configure(configureAction);
            }

            mvcBuilder.AddJsonOptions(o =>
            {
                o.JsonSerializerOptions.IgnoreNullValues = true;
                o.JsonSerializerOptions.Converters.Add(new IgnoreConverter());
                o.JsonSerializerOptions.Converters.Add(new EnumDictionaryConverter());
                o.JsonSerializerOptions.Converters.Add(new IpAddressConverter());
            });

            mvcBuilder
            .AddActiveRoute <OperationsBuilder, OptionsDebugController, OptionsDebugFeature, OptionsDebugOptions>();
        }
Пример #15
0
    internal static IMvcCoreBuilder AddNewtonsoftJson(this IMvcCoreBuilder mvc, Action <MvcJsonOptions> setupAction)
    {
        if (mvc == null)
        {
            throw new ArgumentNullException(nameof(mvc));
        }

        if (setupAction == null)
        {
            throw new ArgumentNullException(nameof(setupAction));
        }

        // Add JSON formatters that will be used as default ones if no specific formatters are asked for
        mvc.AddJsonFormatters();

        mvc.AddJsonOptions(setupAction);

        return(mvc);
    }
Пример #16
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddResponseCompression();
            services.AddCors(builder => builder.AddDefaultPolicy(policyBuilder => policyBuilder.AllowAnyOrigin()));
            IMvcCoreBuilder mvc = services.AddMvcCore();

            mvc.AddApiExplorer();
            mvc.SetCompatibilityVersion(CompatibilityVersion.Latest);
            mvc.AddFormatterMappings();
            mvc.AddJsonFormatters();
            mvc.AddJsonOptions(
                options => {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                options.SerializerSettings.Formatting       = Formatting.Indented;
            }
                );

            services.AddSignalR();
            services.AddSwaggerGen(c => {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Version     = "v1",
                    Title       = "Home assistant API",
                    Description = $"Documentation for {Core.AssistantName} api endpoints.",
                    Contact     = new OpenApiContact()
                    {
                        Name  = "Arun Prakash",
                        Email = "*****@*****.**",
                        Url   = new Uri("https://github.com/SynergYFTW/HomeAssistant")
                    },
                    License = new OpenApiLicense()
                    {
                        Name = "MIT License",
                        Url  = new Uri("https://github.com/SynergYFTW/HomeAssistant/blob/master/LICENSE")
                    }
                });
            });
        }
Пример #17
0
 public static IMvcCoreBuilder AddJson(this IMvcCoreBuilder mvc)
 {
     return(mvc.AddJsonOptions(json => json.JsonSerializerOptions.IgnoreNullValues = true));
 }
 protected override void ConfigureMvc(IMvcCoreBuilder builder)
 {
     builder.AddJsonOptions(
         options => { options.JsonSerializerOptions.CopyFrom(JsonDefaults.SerializerOptions); });
 }
Пример #19
0
 public static IMvcCoreBuilder AddFormatters(this IMvcCoreBuilder mvc)
 {
     return(mvc.AddJsonOptions(json => { json.JsonSerializerOptions.IgnoreNullValues = true; })
            .AddXmlDataContractSerializerFormatters());
 }
Пример #20
0
 public static IMvcCoreBuilder AddFormatters(this IMvcCoreBuilder mvc)
 {
     return(mvc.AddJsonOptions(ConfigureJson).AddXmlDataContractSerializerFormatters());
 }