Beispiel #1
0
        /// <summary>Gets the default serializer settings representing System.Text.Json.</summary>
        /// <returns>The settings.</returns>
        public static JsonSerializerSettings GetSystemTextJsonSettings(IServiceProvider serviceProvider)
        {
            // If the ASP.NET Core website does not use Newtonsoft.JSON we need to provide a
            // contract resolver which reflects best the System.Text.Json behavior.
            // See https://github.com/RicoSuter/NSwag/issues/2243

            if (serviceProvider != null)
            {
                try
                {
                    var optionsAssembly = Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Mvc.Core"));
                    var optionsType     = typeof(IOptions <>).MakeGenericType(optionsAssembly.GetType("Microsoft.AspNetCore.Mvc.JsonOptions", true));

                    var options     = serviceProvider.GetService(optionsType);
                    var value       = optionsType.GetProperty("Value")?.GetValue(options);
                    var jsonOptions = value?.GetType().GetProperty("JsonSerializerOptions")?.GetValue(value);
                    if (jsonOptions != null && jsonOptions.GetType().FullName == "System.Text.Json.JsonSerializerOptions")
                    {
                        return(SystemTextJsonUtilities.ConvertJsonOptionsToNewtonsoftSettings(jsonOptions));
                    }
                }
                catch
                {
                }
            }

            return(new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }
        public async Task Invoke(HttpContext context)
        {
            var settings = new JsonSchemaGeneratorSettings();

            //settings.SerializerOptions = _jsonOpts;
            settings.SerializerSettings = SystemTextJsonUtilities.ConvertJsonOptionsToNewtonsoftSettings(_jsonOpts);
            var schema = JsonSchema.FromType <Core.WingmanMod>(settings);

            var schemeJson = schema.ToJson();

            context.Response.StatusCode = 200;
            await context.Response.WriteAsync(schemeJson);
        }
        public async Task SystemTextJson_WhenEnumsAreSerializedAsStrings_ThenGlobalConverterExists()
        {
            // Arrange
            var options = new JsonSerializerOptions
            {
                Converters =
                {
                    new JsonStringEnumConverter()
                }
            };

            // Act
            var settings = SystemTextJsonUtilities.ConvertJsonOptionsToNewtonsoftSettings(options);

            // Assert
            Assert.Contains(settings.Converters, c => c is StringEnumConverter);
        }