AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Official stance on comments in appsettings.json

Open kimjamia opened this issue 2 years ago • 4 comments

Given that comments work in appsettings.json and it's a pretty common question too, it would be useful to clarify the official stance on this page.

One very helpful possibility would be to instruct how to configure the JSON configuration provider to explicitly support comments.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

kimjamia avatar Jun 29 '22 08:06 kimjamia

Hello @kimjamia ... This comes up every few years, and it is a good time to hear if there are any updates from the team. I recall back in 2015 👴😄 the following discussion: https://github.com/aspnet/Announcements/issues/24#issuecomment-101431570

Reminder on the history (from Wikipedia):

Comments were intentionally excluded from JSON. In 2012, Douglas Crockford described his design decision thus: "I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability."

@JamesNK ... What's the latest news on comments in JSON files? Are the concerns still that they aren't spec and external parsers may choke (per Eilon's remark 7 years ago) ... so the team still generally frowns on the idea?

guardrex avatar Jun 29 '22 09:06 guardrex

Comments are intentionally allowed in appsettings.json. You can add that to the docs.

JamesNK avatar Jun 29 '22 09:06 JamesNK

What are the rules for use so that the parser doesn't choke?

  1. Must they be on their own line? ... or can they appear at the ends of lines, too?
  2. Is /* ... */ syntax also supported? ... or only //? Does the // need to be at any specific position (e.g., are leading spaces ok)?
  3. Should they avoid JSON delimiters (e.g., {, }, [, ], commas)? ... and/or any other chars?

guardrex avatar Jun 29 '22 09:06 guardrex

JSON comments behave like JS (or C#). Both styles are supported.

https://www.w3schools.com/js/js_comments.asp

JamesNK avatar Jun 29 '22 12:06 JamesNK

for vscode add below to settings.json

"files.associations": {
    "appsettings*.json": "jsonc"
}

MoishyS avatar Mar 23 '23 15:03 MoishyS

Comments are intentionally allowed in appsettings.json. You can add that to the docs.

BTW... to get Visual Studio Code to stop complaining, do this: https://stackoverflow.com/questions/47834825/in-vs-code-disable-error-comments-are-not-permitted-in-json/47834826#47834826

JamieLaing avatar Apr 19 '23 17:04 JamieLaing

Another option is to rename your appsettings.json to appsettings.jsonc (and your environment specific config files as well, so appsettings.Development.json becomes appsettings.Development.jsonc, and so on).

Then add this near the top of Program.cs:

var builder = WebApplication.CreateBuilder(args);
IHostEnvironment env = builder.Environment;

builder.Configuration
    .AddJsonFile("appsettings.jsonc", true, true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.jsonc", true, true);

Then right click on each appsettings file and make sure to set Copy to output directory to Copy if newer, as otherwise these files no longer automatically get copied if you change the extension.

Now you can add comments to all of your jsonc files, and not have to instruct other developers to change their editor settings to get the file to show up without syntax highlighting errors.

lukas-shawford avatar Jun 28 '24 22:06 lukas-shawford