0

I'm trying to set serialization options for System.Text.Json in a top-level statements project. I'm doing something wrong in that the callback passed to Configure doesn't appear to be called. Can someone please set me in the right direction? Thanks

 using System.Text.Json.Serialization;
    using Microsoft.AspNetCore.Mvc;
    
    var builder = WebApplication.CreateSlimBuilder(args);
    
    builder.Services.Configure<JsonOptions>(options =>
    {
        // This doesn't appear to be called.
        options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals;
    });
    
    var app = builder.Build();
    
    app.MapGet("/", () => Results.Ok(new{value=double.PositiveInfinity}));
    
    app.Run();
Manoj Agrawal
  • 429
  • 3
  • 10
Damien Sawyer
  • 5,323
  • 3
  • 44
  • 56
  • Does this answer your question? [How to globally set default options for System.Text.Json.JsonSerializer?](https://stackoverflow.com/questions/58331479/how-to-globally-set-default-options-for-system-text-json-jsonserializer) – Mohammad Aghazadeh Aug 16 '23 at 04:39

1 Answers1

0

This worked. The issue was using Microsoft.AspNetCore.Mvc instead of Microsoft.AspNetCore.Http.Json.

using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http.Json;

var builder = WebApplication.CreateBuilder(args);

builder.Services.Configure<JsonOptions>(options =>
{
    options.SerializerOptions.NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals;
});

var app = builder.Build();

app.MapGet("/", () => Results.Ok(new { value = double.PositiveInfinity }));

app.Run();
Damien Sawyer
  • 5,323
  • 3
  • 44
  • 56