You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I had a few issues configuring json api serialiser to work on core 3.1. I think it is working now but not sure if there is a better or more supported way? If not this might help others while docs/code gets updated.
usingSystem;usingSystem.Buffers;usingJsonApiSerializer;usingMicrosoft.AspNetCore.Mvc;usingMicrosoft.AspNetCore.Mvc.Formatters;usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Extensions.DependencyInjection.Extensions;usingMicrosoft.Extensions.Logging;usingMicrosoft.Extensions.ObjectPool;usingMicrosoft.Extensions.Options;usingNewtonsoft.Json;namespaceMyApp{// ReSharper disable once UnusedMember.GlobalpublicstaticclassJsonApiExtensions{publicstaticIMvcBuilderAddJsonApi(thisIMvcBuilderbuilder,Action<MvcNewtonsoftJsonOptions>setupAction=null){if(builder==null)thrownewArgumentNullException(nameof(builder));builder.Services.TryAddSingleton<ObjectPoolProvider,DefaultObjectPoolProvider>();builder.Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>,ConfigureMvcOptionsForJsonApi>());builder.Services.AddSingleton<IConfigureOptions<MvcOptions>,ConfigureMvcOptionsForJsonApi>();builder.Services.Configure(setupAction??(options =>{}));returnbuilder;}}publicclassConfigureMvcOptionsForJsonApi:IConfigureOptions<MvcOptions>{privatereadonlyILoggerFactoryloggerFactory;privatereadonlyObjectPoolProviderobjectPoolProvider;privatereadonlyIOptions<MvcNewtonsoftJsonOptions>newtonsoftOptions;publicConfigureMvcOptionsForJsonApi(ILoggerFactoryloggerFactory,ObjectPoolProviderobjectPoolProvider,IOptions<MvcNewtonsoftJsonOptions>newtonsoftOptions){this.loggerFactory=loggerFactory;this.objectPoolProvider=objectPoolProvider;this.newtonsoftOptions=newtonsoftOptions;}publicvoidConfigure(MvcOptionsoptions){varjsonApiSerializerSettings=newJsonApiSerializerSettings{NullValueHandling=NullValueHandling.Ignore,DateParseHandling=DateParseHandling.None,ReferenceLoopHandling=ReferenceLoopHandling.Serialize};ConfigureInputFormatter(options,jsonApiSerializerSettings,newtonsoftOptions.Value);ConfigureOutputFormatter(options,jsonApiSerializerSettings);}privatevoidConfigureInputFormatter(MvcOptionsoptions,JsonApiSerializerSettingsjsonApiSerializerSettings,MvcNewtonsoftJsonOptionsmvcNewtonsoftJsonOptions){varjsonApiInputFormatter=newNewtonsoftJsonInputFormatter(loggerFactory.CreateLogger<NewtonsoftJsonInputFormatter>(),jsonApiSerializerSettings,ArrayPool<char>.Shared,objectPoolProvider,options,mvcNewtonsoftJsonOptions);jsonApiInputFormatter.SupportedMediaTypes.Clear();jsonApiInputFormatter.SupportedMediaTypes.Add(WebConstants.JsonApiContent);options.InputFormatters.RemoveType<NewtonsoftJsonInputFormatter>();options.InputFormatters.Insert(0,jsonApiInputFormatter);}privatestaticvoidConfigureOutputFormatter(MvcOptionsoptions,JsonApiSerializerSettingsjsonApiSerializerSettings){varjsonApiOutputFormatter=newNewtonsoftJsonOutputFormatter(jsonApiSerializerSettings,ArrayPool<char>.Shared,options);jsonApiOutputFormatter.SupportedMediaTypes.Clear();jsonApiOutputFormatter.SupportedMediaTypes.Add(WebConstants.JsonApiContent);options.OutputFormatters.RemoveType<NewtonsoftJsonOutputFormatter>();options.OutputFormatters.Insert(0,jsonApiOutputFormatter);}}}
And then the Json API config can be applied using an extension method in startup.cs, with the option of applying additional config to newtonsoft.json:
publicvoidConfigureServices(IServiceCollectionservices){services.AddOptions();services.AddMvc().AddJsonApi(opt =>opt.AllowInputFormatterExceptionMessages=true);// or just .AddJsonApi()}
The text was updated successfully, but these errors were encountered:
Hello
I had a few issues configuring json api serialiser to work on core 3.1. I think it is working now but not sure if there is a better or more supported way? If not this might help others while docs/code gets updated.
And then the Json API config can be applied using an extension method in startup.cs, with the option of applying additional config to newtonsoft.json:
The text was updated successfully, but these errors were encountered: