コード例 #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());
     }));
 }
コード例 #10
0
 /// <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
ファイル: Services.cs プロジェクト: mac160382/product-api
 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
ファイル: Startup.cs プロジェクト: Prodject/HomeAssistant
        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));
 }
コード例 #18
0
 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());
 }