Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Official stance on comments in appsettings.json #26254

Closed
kimjamia opened this issue Jun 29, 2022 · 7 comments · Fixed by #27805
Closed

Official stance on comments in appsettings.json #26254

kimjamia opened this issue Jun 29, 2022 · 7 comments · Fixed by #27805
Assignees
Labels
doc-enhancement Pri1 Pri---0.5 Don't delete without permission seQUESTered Identifies that an issue has been imported into Quest. Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

@kimjamia
Copy link

kimjamia commented Jun 29, 2022

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.


Associated WorkItem - 33225

@dotnet-bot dotnet-bot added ⌚ Not Triaged Source - Docs.ms Docs Customer feedback via GitHub Issue labels Jun 29, 2022
@guardrex
Copy link
Collaborator

guardrex commented Jun 29, 2022

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: aspnet/Announcements#24 (comment)

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?

@JamesNK
Copy link
Member

JamesNK commented Jun 29, 2022

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

@guardrex
Copy link
Collaborator

guardrex commented Jun 29, 2022

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?

@JamesNK
Copy link
Member

JamesNK commented Jun 29, 2022

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

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

@wadepickett wadepickett added Pri1 and removed P0.5 labels Oct 31, 2022
@Rick-Anderson Rick-Anderson added the Pri---0.5 Don't delete without permission label Nov 2, 2022
@Rick-Anderson Rick-Anderson self-assigned this Nov 2, 2022
@Rick-Anderson Rick-Anderson added the reQUEST Triggers an issue to be imported into Quest label Nov 3, 2022
@github-actions github-actions bot added seQUESTered Identifies that an issue has been imported into Quest. and removed reQUEST Triggers an issue to be imported into Quest labels Nov 8, 2022
@MoishyS
Copy link

MoishyS commented Mar 23, 2023

for vscode add below to settings.json

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

@JamieLaing
Copy link

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

@lukas-shawford
Copy link

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.

pseale added a commit to pseale/twitch-bot that referenced this issue Oct 19, 2024
pseale added a commit to pseale/twitch-bot that referenced this issue Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Pri1 Pri---0.5 Don't delete without permission seQUESTered Identifies that an issue has been imported into Quest. Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants