Skip to content

Conversation

@knqyf263
Copy link
Collaborator

Overview

This PR adds support for JSONC (JSON with Comments) format by implementing a simple parser that converts JSONC to RFC 8259 compliant JSON. JSONC is widely used in configuration files (like in VS Code) and allows single-line (//) and multi-line (/* */) comments as well as trailing commas in objects and arrays.

Key Features

  • Added ToRFC8259() function that converts JSONC to standard JSON while preserving exact character offsets and line numbers
  • Implemented UnmarshalJSONC() for parsing JSONC data directly into Go structs
  • Maintained line position information for the correct locations

Technical Details

The implementation:

  1. Preserves character positions by replacing comments and trailing commas with spaces
  2. Handles both single-line (//) and multi-line (/* */) comments
  3. Properly processes comments within string literals
  4. Manages escaped characters and nested structures
  5. Ensures line numbers are preserved for accurate error reporting

Usage Example

type Config struct {
    Name    string `json:"name"`
    Version string `json:"version"`
    xjson.Location  // Embed Location to get line number info
}

var config Config
if err := xjson.UnmarshalJSONC(data, &config); err != nil {
    return err
}

Testing

All tests are passing and cover various edge cases including:

  • Comments within string literals
  • Nested objects with trailing commas
  • Complex escaped characters
  • Real-world configuration examples

Related Issues

Related issues

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

- Implemented ToRFC8259 function to convert JSONC to valid JSON while preserving character offsets.
- Added UnmarshalJSONC function to parse JSONC data into Go structs, maintaining line number information.
- Introduced tests for ToRFC8259 and UnmarshalJSONC to ensure correct handling of various JSONC scenarios, including single-line and multi-line comments, trailing commas, and complex strings.
- Created a new jsonc_test.go file for comprehensive testing of JSONC functionalities.
@knqyf263 knqyf263 self-assigned this May 13, 2025
@knqyf263 knqyf263 requested a review from Copilot May 13, 2025 08:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for JSONC by implementing a parser that strips comments and trailing commas from JSONC data while preserving the original character offsets and line numbers.

  • Introduces a ToRFC8259 function to convert JSONC input to valid JSON.
  • Adds an UnmarshalJSONC function for directly parsing JSONC into Go structs while preserving error location information.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/x/json/jsonc_test.go Test cases covering comment removal and trailing comma handling
pkg/x/json/jsonc.go Implementation of a JSONC parser that converts to RFC8259 JSON
Comments suppressed due to low confidence (1)

pkg/x/json/jsonc.go:167

  • When processing a '/' character in processNormalToken, consider using a peek-like method (if available) instead of reading and then unreading a byte. This could improve clarity and avoid potential side effects in stream handling.
case '/' :

@knqyf263 knqyf263 changed the title feat(json): add JSONC support for comments and trailing commas feat: add JSONC support for comments and trailing commas May 13, 2025
@knqyf263 knqyf263 mentioned this pull request May 13, 2025
6 tasks
@knqyf263 knqyf263 requested a review from DmitriyLewen May 13, 2025 09:15
Copy link
Contributor

@DmitriyLewen DmitriyLewen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!
LGTM.

@DmitriyLewen DmitriyLewen added this pull request to the merge queue May 13, 2025
@nikpivkin
Copy link
Contributor

@knqyf263 It looks great! By the way, there is a popular package hujson for converting JSONC to JSON.

Merged via the queue into aquasecurity:main with commit 0b0e406 May 13, 2025
12 of 13 checks passed
@knqyf263 knqyf263 deleted the jsonc branch May 13, 2025 11:01
@knqyf263
Copy link
Collaborator Author

@nikpivkin Thanks for sharing. I actually looked into the following libraries before I started writing it by myself.

Since it looks like a simple script is enough for our purpose, I decided to implement it myself rather than adding a new dependency (I didn't want to add several libraries just for JSON parsing). If we need more extension supports in the future, we can switch to hujson or something like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants