mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
## Summary This PR implements a new `Bun.JSONC.parse()` API that allows parsing JSONC (JSON with Comments) files. It addresses the feature request from issue #16257 by providing a native API for parsing JSON with comments and trailing commas. The implementation follows the same pattern as `Bun.YAML` and `Bun.TOML`, leveraging the existing `TSConfigParser` which already handles JSONC parsing internally. ## Features - **Parse JSON with comments**: Supports both `//` single-line and `/* */` block comments - **Handle trailing commas**: Works with trailing commas in objects and arrays - **Full JavaScript object conversion**: Returns native JavaScript objects/arrays - **Error handling**: Proper error throwing for invalid JSON - **TypeScript compatibility**: Works with TypeScript config files and other JSONC formats ## Usage Example ```javascript const result = Bun.JSONC.parse(`{ // This is a comment "name": "my-app", "version": "1.0.0", // trailing comma is allowed "dependencies": { "react": "^18.0.0", }, }`); // Returns native JavaScript object ``` ## Implementation Details - Created `JSONCObject.zig` following the same pattern as `YAMLObject.zig` and `TOMLObject.zig` - Uses the existing `TSConfigParser` from `json.zig` which already handles comments and trailing commas - Added proper C++ bindings and exports following Bun's established patterns - Comprehensive test suite covering various JSONC features ## Test Plan - [x] Basic JSON parsing works - [x] Single-line comments (`//`) are handled correctly - [x] Block comments (`/* */`) are handled correctly - [x] Trailing commas in objects and arrays work - [x] Complex nested structures parse correctly - [x] Error handling for invalid JSON - [x] Empty objects and arrays work - [x] Boolean and null values work correctly 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com>