lundi 25 février 2019

Access JSON serializer configured in ASP.NET Core

I have an API controller that returns instance of a class ExampleDataType. The default behavior is that the object is serialized to JSON and that's fine.

API controller snippet:

[Route("api/[controller]")]
[ApiController]
public class ExampleController : ControllerBase
{
    [HttpGet]
    public ExampleDataType Get()
    {
        return new ExampleDataType();
    }
}

I would like to slightly change the way of object serialization (property casing). I configured JSON serializer built in ASP.NET Core application in following way:

Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddJsonOptions(options =>
            {
                options.UseMemberCasing();
            });
    }

So far so good. Now, I would like to create a (unit?) test that would document intended behavior. My idea for such test is:

  1. Access the JSON serializer with applied customizations in Server.
  2. Use the serializer on a test class instance.
  3. Assert the results.

I already know how to spawn a test, in-memory server instance with WebApplicationFactory. Alas, I don't know how to access the serializer configured in server.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire