0

Is it possible to specify unique JSON serialization settings on an individual ASP.NET Core API endpoint, that differs from the default settings one has set at the project (default) level?

So for instance: if globally DefaultValueHandling.Include is specified (so all properties always get serialized), but on a specific endpoint I want instead DefaultValueHandling.Ignore, what are the easiest ways to do this?

One route that I'm not happy about is: to decorate (pollute!) every single property with [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]. It works, but in this case I have a huge DTO, it's far more desirable if all that pollution could be replaced with essentially a single setting either decorating that DTO class, and / or specifying this within the API endpoint somehow.

Another route is to directly serialize the object within the endpoint, and then to return json directly, but I'm also hoping to avoid this, it ruins Swagger / obscures what the return type is, for one, and so forth.

I'm working with Newtonsoft.Json in this case, but answers are just as welcome applying to System.Text.Json. on .NET 6. Thanks!

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Nicholas Petersen
  • 8,458
  • 7
  • 55
  • 67
  • Does [ASP.NET Core API JSON serializersettings per request](https://stackoverflow.com/q/44828302) help? – dbc Apr 22 '22 at 22:59

0 Answers0