C#C
C#8mo ago
Angius

Issues using `.json5` files for `appsettings.json`

I wanted support for trailing commas and comments in my appsettings.json, so I decided to go with JSON5.
But here's the weird thing.
I can add trailing commas and comments to appsettings.json and everything works perfectly fine. So far so good, but Rider complains that you can't have them in a json file.
No biggie, Rider supports .json5 so I renamed the file to appsettings.json5 and changed
Configuration = new ConfigurationBuilder()
    .AddConfiguration(configuration)
    .AddJsonFile("appsettings.json")

to
Configuration = new ConfigurationBuilder()
    .AddConfiguration(configuration)
    .AddEnvironmentVariables("ogma_")
    .AddJsonFile("appsettings.json5")

And... now I'm getting NRE on one of the segments of the config. Everything starts working again as soon as I rename it back to .json. I tried to be sneaky and use .5.json with a file association in Rider, but no cigar, also getting NREs.
image.png
Was this page helpful?