ConfigureHttpJsonOptions
and AddJsonOptions
manage serilizer settings for model binding/response serialization, they will not affect the settings you use to deserialize manually. Provide settings for JsonSerializer.Deserialize
, for example:
var res = JsonSerializer.Deserialize<WeatherForecast[]>(responseContent, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });Notes
-
There is no need to explicitly configure JSON options for API to only set the
PropertyNameCaseInsensitive
totrue
sinceJsonSerializerDefaults.Web
defaults toPropertyNameCaseInsensitive = true
:The following options have different defaults for web apps:
PropertyNameCaseInsensitive = true
JsonNamingPolicy = CamelCase
NumberHandling = AllowReadingFromString
-
You can resolve configured JSON options from the DI. For example for minimal APIs (or you can configure your own and resolve them):
app.MapGet("/weatherforecast", (IOptions<Microsoft.AspNetCore.Http.Json.JsonOptions> opts) => { var serOpts = opts.Value.SerializerOptions; // ... });
-
You should not need to deserialize data to just serialize it back again (in the shown code at least). Returning content should do the trick:
return Results.Content(responseContent, "application/json");
-
You can't mutate default serialization options (see this github comment for example), at least ATM, but you can define helper/extension methods which will use some global statically defined ones.