private static void Main() { var person = new Person { Title = Title.Mr, FirstName = "Matt", Age = 99, Income = 1234 }; // Serialize using XmlDataContractSerializerOutputFormatter - works var xmlFormatter = new XmlDataContractSerializerOutputFormatter(); var result = Serialize(person, xmlFormatter, ApplicationXml); Console.WriteLine("Xml: " + result); // Serialize using NewtonsoftJsonOutputFormatter - also works var newtonsoftFormatter = new NewtonsoftJsonOutputFormatter(new Newtonsoft.Json.JsonSerializerSettings(), ArrayPool <char> .Shared, new MvcOptions()); result = Serialize(person, newtonsoftFormatter, ApplicationJson); Console.WriteLine("Newtonsoft Json: " + result); // Serialize using SystemTextJsonOutputFormatter - returns empty string :-( var systemTextJsonFormatter = new SystemTextJsonOutputFormatter(new System.Text.Json.JsonSerializerOptions()); result = Serialize(person, systemTextJsonFormatter, ApplicationJson); Console.WriteLine("System.Text.Json: " + result); Console.ReadLine(); }
public static IWebJobsBuilder AddSwashBuckle( this IWebJobsBuilder builder, Assembly assembly, Action <SwaggerDocOptions> configureDocOptionsAction = null) { builder.AddExtension <SwashbuckleConfig>() .BindOptions <SwaggerDocOptions>() .ConfigureOptions <SwaggerDocOptions>((configuration, section, options) => { configureDocOptionsAction?.Invoke(options); }); builder.Services.AddSingleton <IModelMetadataProvider>(new EmptyModelMetadataProvider()); builder.Services.AddSingleton(new SwashBuckleStartupConfig { Assembly = assembly }); var formatter = new SystemTextJsonOutputFormatter(new JsonSerializerOptions()); builder.Services.AddSingleton <IOutputFormatter>(formatter); builder.Services.AddSingleton <IApiDescriptionGroupCollectionProvider, FunctionApiDescriptionProvider>(); return(builder); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(options => { var jsonOpenHimFormatter = new SystemTextJsonOutputFormatter(new JsonSerializerOptions()); // Need to add support for the media type expected by openHIM otherwise dotnet will return a 406. jsonOpenHimFormatter.SupportedMediaTypes.Add("application/json+openhim"); options.OutputFormatters.Add(jsonOpenHimFormatter); }); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "OpenHim.Mediator.HL7Validator", Version = "v1" }); }); services.AddOpenHimMediator(Configuration.GetSection("mediatorconfig")); services.AddTransient <IHL7MessageProcessor, HL7MessageProcessor>(); services.AddTransient <IOpenHimOrchestrator, OpenHimOrchestrator>(); services.AddOptions(); services.Configure <HL7Config>(Configuration.GetSection("hl7Config")); }
public JsonapiOutputFormatter() { SupportedMediaTypes.Add("application/vnd.api+json"); SupportedEncodings.Add(Encoding.UTF8); SupportedEncodings.Add(Encoding.Unicode); _formatter = new SystemTextJsonOutputFormatter(new JsonSerializerOptions { IgnoreNullValues = true }); }
public void Configure(MvcOptions options) { // Set up ModelBinding options.ModelBinderProviders.Add(new BinderTypeModelBinderProvider()); options.ModelBinderProviders.Add(new ServicesModelBinderProvider()); options.ModelBinderProviders.Add(new BodyModelBinderProvider(options.InputFormatters, _readerFactory, _loggerFactory, options)); options.ModelBinderProviders.Add(new HeaderModelBinderProvider()); options.ModelBinderProviders.Add(new FloatingPointTypeModelBinderProvider()); options.ModelBinderProviders.Add(new EnumTypeModelBinderProvider(options)); options.ModelBinderProviders.Add(new DateTimeModelBinderProvider()); options.ModelBinderProviders.Add(new TryParseModelBinderProvider()); options.ModelBinderProviders.Add(new SimpleTypeModelBinderProvider()); options.ModelBinderProviders.Add(new CancellationTokenModelBinderProvider()); options.ModelBinderProviders.Add(new ByteArrayModelBinderProvider()); options.ModelBinderProviders.Add(new FormFileModelBinderProvider()); options.ModelBinderProviders.Add(new FormCollectionModelBinderProvider()); options.ModelBinderProviders.Add(new KeyValuePairModelBinderProvider()); options.ModelBinderProviders.Add(new DictionaryModelBinderProvider()); options.ModelBinderProviders.Add(new ArrayModelBinderProvider()); options.ModelBinderProviders.Add(new CollectionModelBinderProvider()); options.ModelBinderProviders.Add(new ComplexObjectModelBinderProvider()); // Set up filters options.Filters.Add(new UnsupportedContentTypeFilter()); // Set up default input formatters. options.InputFormatters.Add(new SystemTextJsonInputFormatter(_jsonOptions.Value, _loggerFactory.CreateLogger <SystemTextJsonInputFormatter>())); // Media type formatter mappings for JSON options.FormatterMappings.SetMediaTypeMappingForFormat("json", MediaTypeHeaderValues.ApplicationJson); // Set up default output formatters. options.OutputFormatters.Add(new HttpNoContentOutputFormatter()); options.OutputFormatters.Add(new StringOutputFormatter()); options.OutputFormatters.Add(new StreamOutputFormatter()); var jsonOutputFormatter = SystemTextJsonOutputFormatter.CreateFormatter(_jsonOptions.Value); options.OutputFormatters.Add(jsonOutputFormatter); // Set up ValueProviders options.ValueProviderFactories.Add(new FormValueProviderFactory()); options.ValueProviderFactories.Add(new RouteValueProviderFactory()); options.ValueProviderFactories.Add(new QueryStringValueProviderFactory()); options.ValueProviderFactories.Add(new JQueryFormValueProviderFactory()); options.ValueProviderFactories.Add(new FormFileValueProviderFactory()); // Set up metadata providers ConfigureAdditionalModelMetadataDetailsProviders(options.ModelMetadataDetailsProviders); // Set up validators options.ModelValidatorProviders.Add(new DefaultModelValidatorProvider()); }
public AbpHybridJsonOutputFormatter(SystemTextJsonOutputFormatter systemTextJsonOutputFormatter, NewtonsoftJsonOutputFormatter newtonsoftJsonOutputFormatter) { _systemTextJsonOutputFormatter = systemTextJsonOutputFormatter; _newtonsoftJsonOutputFormatter = newtonsoftJsonOutputFormatter; SupportedEncodings.Add(Encoding.UTF8); SupportedEncodings.Add(Encoding.Unicode); SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationJson); SupportedMediaTypes.Add(MediaTypeHeaderValues.TextJson); SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationAnyJsonSyntax); }
public override void OnActionExecuted(ActionExecutedContext ctx) { if (ctx.Result is JsonResult jsonResult) { ctx.Result = new ObjectResult(jsonResult.Value); } if (ctx.Result is ObjectResult objectResult) { var jsonOpt = ctx.HttpContext.RequestServices.GetRequiredService <IOptions <JsonOptions> >(); var formatter = new SystemTextJsonOutputFormatter(jsonOpt.Value.JsonSerializerOptions); objectResult.Formatters.Add(formatter); } }
public IonOutputFormatter(SystemTextJsonOutputFormatter jsonOutputFormatter) { if (jsonOutputFormatter == null) { throw new ArgumentNullException(nameof(jsonOutputFormatter)); } _jsonOutputFormatter = jsonOutputFormatter; SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/ion+json")); SupportedEncodings.Add(Encoding.UTF8); SupportedEncodings.Add(Encoding.Unicode); }
private static IServiceProvider CreateServices() { var options = Options.Create(new MvcOptions()); options.Value.OutputFormatters.Add(new StringOutputFormatter()); options.Value.OutputFormatters.Add(SystemTextJsonOutputFormatter.CreateFormatter(new JsonOptions())); var services = new ServiceCollection(); services.AddSingleton <IActionResultExecutor <ObjectResult> >(new ObjectResultExecutor( new DefaultOutputFormatterSelector(options, NullLoggerFactory.Instance), new TestHttpResponseStreamWriterFactory(), NullLoggerFactory.Instance, options)); return(services.BuildServiceProvider()); }
/// <summary> /// Initializes a new <see cref="JsonpOutputFormatter"/> instance. /// </summary> /// <param name="jsonSerializerOptions">The <see cref="JsonSerializerOptions"/>.</param> public JsonpOutputFormatter(JsonSerializerOptions jsonSerializerOptions) { this.jsonFormatter = new SystemTextJsonOutputFormatter(jsonSerializerOptions); foreach (var supportedEncoding in this.jsonFormatter.SupportedEncodings) { base.SupportedEncodings.Add(supportedEncoding); } foreach (var supportedMediaTypes in this.jsonFormatter.SupportedMediaTypes) { base.SupportedMediaTypes.Add(supportedMediaTypes); } SupportedMediaTypes.Add(ApplicationJavascriptMediaType); SupportedMediaTypes.Add(TextJavascriptMediaType); SupportedMediaTypes.Add(ApplicationXJavascriptMediaType); }
public static IWebJobsBuilder AddSwashBuckle(this IWebJobsBuilder builder, Assembly assembly) { builder.AddExtension <SwashbuckleConfig>() .BindOptions <Option>() .Services.AddSingleton(new SwashBuckleStartupConfig { Assembly = assembly }) ; var formatter = new SystemTextJsonOutputFormatter(new JsonSerializerOptions()); builder.Services.AddSingleton <IModelMetadataProvider>(new EmptyModelMetadataProvider()); builder.Services.AddSingleton <IOutputFormatter>(formatter); builder.Services.AddSingleton <IApiDescriptionGroupCollectionProvider, FunctionApiDescriptionProvider>(); return(builder); }
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)); }
private void Initialize( Mock <HttpContext> httpContext, string format = null, FormatSource?place = null) { OptionsManager = Options.Create(new MvcOptions()); // Setup options on mock service provider MvcOptions = OptionsManager.Value; // Set up default output formatters. MvcOptions.OutputFormatters.Add(new HttpNoContentOutputFormatter()); MvcOptions.OutputFormatters.Add(new StringOutputFormatter()); MvcOptions.OutputFormatters.Add(SystemTextJsonOutputFormatter.CreateFormatter(new JsonOptions())); // Set up default mapping for json extensions to content type MvcOptions.FormatterMappings.SetMediaTypeMappingForFormat( "json", MediaTypeHeaderValue.Parse("application/json")); // Setup MVC services on mock service provider MockActionContext = CreateMockActionContext(httpContext, format, place); }
protected override TextOutputFormatter GetOutputFormatter() { return(SystemTextJsonOutputFormatter.CreateFormatter(new JsonOptions())); }
private static void RestoreDefaultJsonOutputFormatter(FormatterCollection <IOutputFormatter> formatters, SystemTextJsonOutputFormatter jsonOutputFormatter) { if (jsonOutputFormatter != null) { formatters.Add(jsonOutputFormatter); } }
/// <summary> /// 注册Workflow Rest服务 /// </summary> /// <param name="mvcBuilder"></param> /// <param name="config"></param> /// <returns></returns> public static IMvcBuilder AddProcessEngineRestServices(this IMvcBuilder mvcBuilder, IConfiguration config) { IServiceCollection services = mvcBuilder.Services; services.AddAuthorization(opts => { opts.AddPolicy(WorkflowConstants.WORKFLOW_AUTHORIZE_POLICY, policy => { policy.Requirements.Add(new InternaWorkflowAuthorizationRequirement()); }); }); services.AddSingleton <IAuthorizationHandler, InternalWorkflowAuthorizationHandler>(); services.UseInMemoryBus(); mvcBuilder.AddMvcOptions(opts => { #if !NETCORE3 JsonOutputFormatter jsonFormatter = opts.OutputFormatters.FirstOrDefault(x => x.GetType() == typeof(JsonOutputFormatter)) as JsonOutputFormatter; if (jsonFormatter != null) { jsonFormatter.PublicSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; } #endif opts.ModelBinderProviders.Insert(0, new PageableModelBinderProvider()); #if NETCORE3 SystemTextJsonOutputFormatter jsonFormatter = opts.OutputFormatters.FirstOrDefault(x => x.GetType() == typeof(SystemTextJsonOutputFormatter)) as SystemTextJsonOutputFormatter; opts.EnableEndpointRouting = false; #endif }); #if NETCORE3 mvcBuilder.AddNewtonsoftJson(opts => { opts.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; }); #endif services.AddTransient <ProcessInstanceSortApplier>(); services.AddSingleton <PageRetriever>(); services.AddTransient <HistoricInstanceConverter>(); services.AddTransient <HistoryInstanceSortApplier>(); services.AddTransient <PageableProcessHistoryRepositoryService>(); services.AddTransient <PageableProcessInstanceRepositoryService>(sp => { IProcessEngine engine = sp.GetService <IProcessEngine>(); return(new PageableProcessInstanceRepositoryService(sp.GetService <PageRetriever>(), engine.RuntimeService, sp.GetService <ProcessInstanceSortApplier>(), sp.GetService <ProcessInstanceConverter>(), sp.GetService <ILoggerFactory>())); }); services.AddTransient <ListConverter>(); services.AddTransient <TaskConverter>(sp => new TaskConverter(sp.GetService <ListConverter>())); services.AddTransient <HistoricTaskInstanceConverter>(sp => new HistoricTaskInstanceConverter(sp.GetService <ListConverter>())); services.AddTransient <TaskSortApplier>(); services.AddTransient <HistoryTaskSortApplier>(); //services.AddTransient<MessageProducerActivitiEventListener>(); services.AddTransient <PageableTaskRepositoryService>(sp => { IProcessEngine engine = sp.GetService <IProcessEngine>(); return(new PageableTaskRepositoryService( engine.TaskService, sp.GetService <TaskConverter>(), sp.GetService <HistoricTaskInstanceConverter>(), sp.GetService <IHistoryService>(), sp.GetService <PageRetriever>(), sp.GetService <TaskSortApplier>(), sp.GetService <HistoryTaskSortApplier>())); }); services.AddTransient <ProcessInstanceConverter>(sp => { IProcessEngine engine = sp.GetService <IProcessEngine>(); return(new ProcessInstanceConverter(sp.GetService <ListConverter>())); }); services.AddTransient <ProcessInstanceResourceAssembler>(); services.AddScoped <ProcessEngineWrapper>(sp => { IProcessEngine engine = sp.GetService <IProcessEngine>(); IHttpContextAccessor httpContext = sp.GetService <IHttpContextAccessor>(); return(new ProcessEngineWrapper(sp.GetService <ProcessInstanceConverter>(), sp.GetService <PageableProcessInstanceRepositoryService>(), sp.GetService <TaskConverter>(), sp.GetService <PageableTaskRepositoryService>(), null, sp.GetService <SecurityPoliciesApplicationService>(), null, sp.GetService <IApplicationEventPublisher>(), engine, sp.GetService <HistoricInstanceConverter>(), sp.GetService <ILoggerFactory>())); }); services.AddScoped <SecurityPoliciesApplicationService>(); services .AddTransient <PageRetriever>() .AddTransient <ProcessDefinitionConverter>() .AddTransient <ProcessDefinitionSortApplier>() .AddTransient <ProcessDefinitionResourceAssembler>() .AddTransient <ProcessDefinitionMetaResourceAssembler>() .AddTransient <DeploymentConverter>() .AddTransient <DeploymentSortApplier>() .AddTransient <PageableProcessDefinitionRepositoryService>() .AddTransient <PageableDeploymentRespositoryService>(); services.AddTransient <TaskVariableResourceAssembler>(); services.AddTransient <TaskResourceAssembler>(); services.AddTransient <ProcessInstanceVariableResourceAssembler>(); services.AddTransient <AuthenticationWrapper>(); services.AddTransient <IMvcControllerDiscovery, MvcControllerDiscovery>(); mvcBuilder.AddApplicationPart(typeof(ProcessEngineRestExtention).Assembly); return(mvcBuilder); }