Skip to content

Commit c1e7cbd

Browse files
committed
Adjust trailing comma recommendation using RFC2119's terminology
1 parent 7de1028 commit c1e7cbd

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

_config.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
title: JSONC
2222
23-
description: >- # this means to ignore newlines until "baseurl:"
24-
JSONC Specification
2523
baseurl: "" # the subpath of your site, e.g. /blog
2624
url: "https://jsonc.org" # the base hostname & protocol for your site, e.g. http://example.com
2725
github_username: DecimalTurn

index.markdown

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
layout: default
44
---
55

6+
# Specification
7+
68
**Notice:** This is a draft of the JSONC Specification and is subject to change.
79

810
## Introduction
@@ -13,6 +15,16 @@ The JSONC format was informally introduced by Microsoft to be used for VS Code's
1315

1416
[`jsonc-parser`]: https://www.npmjs.com/package/jsonc-parser
1517

18+
## Conventions and Terminology
19+
20+
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in [RFC 2119](#rfc2119)
21+
22+
The following terms are used throughout this specification:
23+
24+
- **Mode line**: A special comment at the beginning of a file that specifies the file type or mode, following conventions used by text editors like Emacs or Vim.
25+
- **Parser**: A software component that reads and interprets JSONC documents.
26+
- **Trailing comma**: A comma that appears after the last element in an array or the last property in an object, before the closing bracket or brace.
27+
1628
## Syntax
1729

1830
JSONC follows the same syntax rules as JSON with the addition of JavaScript style comments. Comments can be either single-line or multi-line.
@@ -46,7 +58,7 @@ Multi-line comments start with `/*` and end with `*/`. They can span multiple li
4658

4759
## Trailing commas
4860

49-
JSONC doesn't allow trailing commas. For more information regarding trailing commas, refer to the [trailing commas information page](/trailingcommas).
61+
JSONC parsers MAY support trailing commas. For more information regarding trailing commas, refer to [Appendix A](#appendix-a-trailing-commas-and-jsonc).
5062

5163
## Semantics
5264

@@ -56,7 +68,7 @@ Comments in JSONC are ignored during parsing, allowing developers to annotate th
5668

5769
The recommended file extension for JSONC documents is `.jsonc`.
5870

59-
The extension `.json` should be avoided, but is supported if a mode line is present at the start of the file:
71+
The extension `.json` SHOULD be avoided, but is supported if a mode line is present at the start of the file:
6072

6173
For instance:
6274
```jsonc
@@ -100,3 +112,24 @@ Here is a non-exhaustive list:
100112

101113
**Kotlin**
102114
- [kotlinx.serialization.json](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-builder/allow-comments.html)
115+
116+
117+
## APPENDIX A: Trailing Commas and JSONC
118+
119+
### Why Trailing Commas Are Not a Requirement?
120+
121+
Trailing commas are not a requirement because the reference implementation, [jsonc-parser](https://www.npmjs.com/package/jsonc-parser), does not allow them unless explicitly configured. The `allowTrailingComma` option is set to `false` by default, so any trailing comma will result in a parsing error.
122+
123+
Mandatory trailing commas support might be added to future versions of the jsonc.org Specification when trailing commas reaches sufficient adoption in the Javascript ecosystem.
124+
125+
### Trailing Commas in VS Code
126+
127+
The "JSON with Comments" mode in VS Code used to allow trailing commas without any warnings by default, but this was eventually changed ([source](https://github.com/microsoft/vscode/issues/102061)).
128+
129+
At the time of writing this document, the "JSON with Comments" mode still accepts trailing commas, but it discourages their usage by displaying a warning ([source](https://code.visualstudio.com/docs/languages/json#_json-with-comments)) unless the file is one of the VS Code official configuration files. The exclusion of those configuration files comes from the JSON schema used. The schema for these files explicitly allow trailing commas, which is why they are accepted without warnings in that specific context.
130+
131+
## References
132+
133+
### [RFC2119]
134+
135+
Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, DOI 10.17487/RFC2119, March 1997, <https://www.rfc-editor.org/info/rfc2119>.

trailingcommas.markdown

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,16 @@
33
layout: default
44
---
55

6-
# Trailing Commas and JSONC
7-
8-
## Why Trailing Commas Aren't Part of the JSONC Specification?
9-
10-
Trailing commas are not part of the JSONC Specification because the reference implementation, [jsonc-parser](https://www.npmjs.com/package/jsonc-parser), does not allow them unless explicitly configured. The `allowTrailingComma` option is set to `false` by default, so any trailing comma will result in a parsing error.
11-
12-
The reason why this specification chose the default behavior of the parser as the reference for the standard is to ensure that JSONC remains compatible with the broader JSON ecosystem, which does not allow trailing commas. This decision helps maintain consistency and predictability across different parsers and implementations. Namely, the [TSConfig](https://www.typescriptlang.org/tsconfig/) and [ESLint config](https://eslint.org/docs/latest/use/configure/configuration-files) files, which are widely used in the JavaScript ecosystem, do not allow trailing commas in their JSONC files.
13-
14-
The exclusion of trailing commas also facilitates the creation of tools and libraries that can parse JSONC without needing to handle additional syntax variations. This helps ensure that JSONC remains a lightweight and straightforward extension of JSON, primarily focused on adding comments without introducing significant complexity.
15-
16-
## Can a Parser That Chooses to Support Trailing Commas Still Be Considered a JSONC Parser?
17-
18-
Yes, however this is not part of the JSONC.org specification and such support would be considered an extension or variation of the standard JSONC format. This means that while a parser may allow trailing commas, it may not be compatible with all JSONC parsers or tools that strictly adhere to the JSONC specification without trailing commas.
19-
20-
## Trailing Commas in VS Code
21-
22-
The "JSON with Comments" mode in VS Code used to allow trailing commas without any warnings by default, but this was eventually changed to discourage their use and promote better compatibility with other JSONC parsers ([source](https://github.com/microsoft/vscode/issues/102061)).
23-
24-
At the time of writing this document, the "JSON with Comments" mode still accepts trailing commas, but it discourages their usage by displaying a warning ([source](https://code.visualstudio.com/docs/languages/json#_json-with-comments)) unless the file is one of the VS Code official configuration files. The exclusion of those configuration files comes from the JSON schema used. The schema for these files explicitly allow trailing commas, which is why they are accepted without warnings in that specific context.
25-
6+
<html lang="en">
7+
<head>
8+
<meta charset="UTF-8">
9+
<meta http-equiv="refresh" content="0; url=https://jsonc.org/#appendix-a-trailing-commas-and-jsonc">
10+
<link rel="canonical" href="https://jsonc.org/#appendix-a-trailing-commas-and-jsonc">
11+
<title>Redirecting...</title>
12+
</head>
13+
<body>
14+
<p>Redirecting...</p>
15+
</body>
16+
</html>
2617

2718

0 commit comments

Comments
 (0)