-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: add JSONC support for comments and trailing commas #8862
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
Conversation
- 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.
There was a problem hiding this 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 '/' :
DmitriyLewen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
LGTM.
|
@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. |
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
ToRFC8259()function that converts JSONC to standard JSON while preserving exact character offsets and line numbersUnmarshalJSONC()for parsing JSONC data directly into Go structsTechnical Details
The implementation:
//) and multi-line (/* */) commentsUsage Example
Testing
All tests are passing and cover various edge cases including:
Related Issues
Related issues
Checklist