antonmedv/fx · error

Comments are not allowed in strict mode

Error message

Comments are not allowed in strict mode

What it means

The lenient parser of this library allows // and /* */ comments while skipping whitespace, but strict mode (standard JSON) forbids them. When skipWhitespace encounters '/' and p.strict is true, it panics with this message. It means the document contains a comment, which is not part of the JSON specification.

Source

Thrown at internal/jsonx/json.go:587

	panic(fmt.Sprintf("Unexpected character %q", p.char))
}

func isEndOfValue(ch byte) bool {
	return isWhitespace(ch) || ch == ',' || ch == '}' || ch == ']' || ch == 0 // 0 is EOF
}

func isWhitespace(ch byte) bool {
	return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'
}

func (p *JsonParser) skipWhitespace() {
	for {
		switch p.char {
		case ' ', '\t', '\n', '\r':
			p.next()
		case '/':
			if p.strict {
				panic("Comments are not allowed in strict mode")
			}
			p.skipComment()
		default:
			return
		}
	}
}

func (p *JsonParser) skipComment() {
	p.next()
	switch p.char {
	case '/':
		for p.char != '\n' && p.char != 0 {
			p.next()
		}
	case '*':
		for {
			p.next()

View on GitHub (pinned to 4f31cd3a0c)

Solutions

  1. Remove all // and /* */ comments from the input before parsing.
  2. Parse with strict=false if the parser instance is under your control, since the lenient mode supports comments.
  3. Strip comments programmatically with a JSONC stripper before calling Parse.
  4. Rename/rehandle the file as JSONC and use a JSONC-aware tool.

Example fix

// before (input.json)
{
  // retry count
  "retries": 3
}
// after
{
  "retries": 3
}
Defensive patterns

Strategy: validation

Validate before calling

if strings.Contains(string(data), "//") || strings.Contains(string(data), "/*") {
    if strictRequired {
        return errors.New("comments present but strict JSON required; strip them first")
    }
    data = stripJSONCComments(data)
}

Try / catch

defer func() {
    if r := recover(); r != nil {
        if strings.Contains(fmt.Sprint(r), "Comments are not allowed") {
            err = errors.New("file is JSONC; strip comments or use lenient mode")
        }
    }
}()

Prevention

When it happens

Trigger: Calling Parse with strict=true on JSON containing // line comments or /* block comments */ anywhere whitespace is legal (between elements, after {, etc.).

Common situations: Hand-edited config files (VS Code jsonc, tsconfig.json-style files), .jsonc files saved as .json, tools that emit annotated JSON.

Related errors


AI-assisted analysis of antonmedv/fx@4f31cd3a0c (2026-09-02). Data as JSON: /api/errors/7304eea2cd53c775. Report an issue: GitHub.